Updated April 4, 2023
Introduction to Perl time
The time represented as a number of seconds since the epoch that is from 00 hours 00 minutes and 00 seconds UTC, 1st of January of the year 1970 in most of the systems and from 00 hours 00 minutes and 00 seconds UTC, 1st of January of the year 1904 in MAC operating systems is obtained in Perl by using a function called time function and this function returns the time represented in seconds since the epoch and this time function in Perl is very much suitable to feed local time and general meridian time.
Syntax to declare time() function in Perl:
time();
Working of time() Function in Perl
- The time represented as a number of seconds since the epoch that is from 00 hours 00 minutes and 00 seconds UTC, 1st of January of the year 1970 in most of the systems and from 00 hours 00 minutes and 00 seconds UTC, 1st of January of the year 1904 in MAC operating systems is obtained in Perl by using a function called time function.
- The time() function returns the time represented in seconds since the epoch.
- Perl’s time() function is very much suitable to feed local time and general meridian time.
Examples of Perl time
Given below are the examples of Perl time:
Example #1
Perl program to obtain the time elapsed since epoch in seconds as the output by making use of time() function and then passing the time elapsed since epoch in seconds as a parameter to the gmtime function to obtain today’s data and time as the output.
Code:
#we are making use of time() function to obtain the time elapsed since epoch
$timeelapsed = time();
#we are displaying the time elapsed since epoch as the output on the screen
print "The time elapsed since the epoch given by the time() function is $timeelapsed\n";
#the time elapsed since epoch is passed as a parameter to the gmtime function to obtain today's date and time
$todaysdateandtime = gmtime( $timeelapsed);
#Today's date and time obtained using gmtime() function is displayed as the output on the screen
print "Today's date and time obtained using gmtime() function by passing the time elapsed since epoch as the parameter to the function is $todaysdateandtime\n";
Output:
In the above program, we are making use of the time() function to obtain the time elapsed since the epoch and storing it in a variable called timeelapsed. Then we are displaying the time elapsed since the epoch obtained by using the time() function as the output on the screen. Then the time elapsed since epoch obtained by using the time() function is passed as a parameter to the gmtime function to obtain today’s date and time. Then Today’s date and time obtained using the gmtime() function by passing the time elapsed since epoch in seconds as the parameter to the function is displayed as the output on the screen.
Example #2
Perl program to obtain the time elapsed since epoch in seconds as the output by making use of time() function and then passing the time elapsed since epoch in seconds as a parameter to the gmtime function to obtain today’s date and time and then display the date and time next year.
Code:
#we are making use of time() function to obtain the time elapsed since epoch
$timeelapsed = time();
#the time elapsed since epoch is passed as a parameter to the gmtime function to obtain today's date and time
$todaysdateandtime = gmtime( $timeelapsed);
#Today's date and time obtained using gmtime() function is displayed as the output on the screen
print "Today's date and time obtained using gmtime() function by passing the time elapsed since epoch as the parameter to the function is $todaysdateandtime\n";
#calculating the date and time on the same day next year
($seconds,$minutes,$hours,$monthday,$month,$year,$weekday,$yearday,$isdst) = gmtime(time);
$year = $year + 1901;
$month += 1;
#displaying the date and time on the same date and time next year
print "The Formated date and time on the next year at the same time is = $monthday/$month/$year $hours:$minutes:$seconds $weekday[$weekday]\n";
Output:
In the above program, we are making use of the time() function to obtain the time elapsed since the epoch and storing it in a variable called timeelapsed. Then the time elapsed since epoch obtained by using the time() function is passed as a parameter to the gmtime function to obtain today’s date and time. Then Today’s date and time obtained using the gmtime() function by passing the time elapsed since epoch in seconds as the parameter to the function is displayed as the output on the screen. Then calculations are done to display the same date and time from the next year and displayed as the output on the screen.
Example #3
Perl program to display the date and time exactly after two years at the same time from now using time() function and gmtime() function.
Code:
#calculating the date and time on the same day after two years
($seconds,$minutes,$hours,$monthday,$month,$year,$weekday,$yearday,$isdst) = gmtime(time);
$year = $year + 1902;
$month += 1;
#displaying the date and time on the same date and time after two years
print "The Formated date and time after two years at the same time is = $monthday/$month/$year $hours:$minutes:$seconds $weekday[$weekday]\n";
Output:
In the above program, we are making use of the time() function to obtain the time elapsed since epoch and then passing it as a parameter to the gmtime function to obtain today’s date and time. Then we are doing calculations to obtain the date and time exactly after two years from the obtained date and time, which is displayed as the output on the screen.
Recommended Articles
This is a guide to Perl time. Here we discuss the introduction, working of time() function in Perl, and examples. You may also have a look at the following articles to learn more –