In this chapter, we introduce how the Perl language handles dates and times.
Perl has the following functions for handling time:
- 1. time() function: Returns the number of seconds elapsed since January 1, 1970.
- 2. localtime() function: Gets the local timezone time.
- 3. gmtime() function: Gets Greenwich Mean Time (GMT).
Current Time and Date
Next, let's look at the localtime() function. When called without arguments, this function returns the current time and date.
The following 9 symbols represent different date and time parameters:
sec, # Seconds, 0 to 61 min, # Minutes, 0 to 59 hour, # Hours, 0 to 24 mday, # Day of the month, 1 to 31 mon, # Month, 0 to 11 year, # Year, since 1900 wday, # Day of the week, 0-6, where 0 represents Sunday yday, # Day of the year, 0-364, 365 isdst # True if Daylight Saving Time is in effect
An example is shown below:
Example
#!/usr/bin/perl @months = qw( January February March April May June July August September October November December ); @days = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); print "$mday $months[$mon] $days[$wday]n";
The output of the above example is:
12 June Sunday
If you call localtime() directly, it returns the time set in the system's current timezone. An example is as follows:
Example
#!/usr/bin/perl $datestring = localtime(); print "Time and date: $datestringn";
The output of the above example is:
Time and date: Sun Jun 12 11:27:31 2016
Greenwich Mean Time (GMT)
The function gmtime() is similar to localtime(), but it returns standard Greenwich Mean Time.
Example
#!/usr/bin/perl $local_datestring = localtime(); print "Local time and date: $local_datestringn"; $gmt_datestring = gmtime(); print "GMT time and date: $gmt_datestringn";
The output of the above example is:
Local time and date: Sun Jun 12 11:32:14 2016 GMT time and date: Sun Jun 12 03:32:14 2016
From the example, we can see that the time in China is 8 hours ahead of Greenwich Mean Time.
Formatting Date and Time
We can use the 9 time elements from the localtime() function to output time in a specified format. Formatted output uses the printf() function:
Example
#!/usr/bin/perl
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
printf("Formatted time: HH:MM:SSn");
printf("%02d:%02d:%02d", $hour, $min, $sec);
The output of the above example is:
Formatted time: HH:MM:SS 11:35:23
Epoch Time
We can use the time() function to get the Epoch time, which returns the number of seconds elapsed since January 1, 1970. An example is as follows:
Example
#!/usr/bin/perl $epoc = time(); print "Seconds elapsed since January 1, 1970: $epocn";
The output of the above example is:
Seconds elapsed since January 1, 1970: 1465702883
We can output the time in a desired format:
Example
#!/usr/bin/perl
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
print "Current time and date: ";
printf("%d-%d-%d %d:%d:%d",$year+1900,$mon+1,$mday,$hour,$min,$sec);
print "n";
$epoc = time();
$epoc = $epoc - 24 * 60 * 60;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($epoc);
print "Yesterday's time and date: ";
printf("%d-%d-%d %d:%d:%d",$year+1900,$mon+1,$mday,$hour,$min,$sec);
print "n";
The output of the above example is:
Current time and date: 2017-3-15 12:47:54 Yesterday's time and date: 2017-3-14 12:47:54
POSIX Function strftime()
The strftime() function can format time into our desired format.
The following table lists some formatting symbols. The * indicates that the symbol depends on the local time:
| Symbol | Description | Example |
|---|---|---|
%a |
Abbreviated weekday name (Sun..Sat) * | Thu |
%A |
Full weekday name (Sunday..Saturday) * | Thursday |
%b |
Abbreviated month name (Jan..Dec) * | Aug |
%B |
Full month name (January..December) * | August |
%c |
Date and time * | Thu Aug 23 14:55:02 2001 |
%C |
Century divided by 100 and truncated to integer (00-99) |
20 |
%d |
Day of the month, zero-padded (01-31) |
23 |
%D |
Date format MM/DD/YY, equivalent to %m/%d/%y |
08/23/01 |
%e |
Day of the month, space-padded (1-31) |
23 |
%F |
ISO 8601 date format YYYY-MM-DD, similar to %Y-%m-%d |
2001-08-23 |
%g |
Two-digit year of the ISO 8601 week-based year (00-99) |
01 |
%G |
ISO 8601 week-based year | 2001 |
%h |
Abbreviated month name * (same as %b) |
Aug |
%H |
Hour in 24-hour format (00-23) |
14 |
%I |
Hour in 12-hour format (01-12) |
02 |
%j |
Day of the year (001-366) |
235 |
%m |
Month as a decimal number (01-12) |
08 |
%M |
Minute (00-59) |
55 |
%n |
Newline character ('n') |
|
%p |
AM or PM designation | PM |
%r |
12-hour clock time (hh:mm:ss AM/PM) * |
02:55:02 pm |
%R |
24-hour time format HH:MM, equivalent to %H:%M |
14:55 |
%S |
Second (00-61) |
02 |
%t |
Horizontal tab character ('t') |
|
%T |
24-hour time format (hh:mm:ss), equivalent to %H:%M:%S |
14:55 |
%u |
ISO 8601 weekday number (1-7, where Monday is 1) |
4 |
%U |
Week number of the year (Sunday as the first day of the week) (00-53) |
33 |
%V |
ISO 8601 week number (00-53) |
34 |
%w |
Day of the week (0-6, where Sunday is 0) |
4 |
%W |
Week number of the year (Monday as the first day of the week) (00-53) |
34 |
%x |
Date representation * | 08/23/01 |
%X |
Time representation * | 14:55:02 |
%y |
Year without century (00-99) |
01 |
%Y |
Year with century | 2001 |
%z |
ISO 8601 UTC offset from UTC (+hhmm or -hhmm) |
+100 |
%Z |
Timezone name or abbreviation * | CDT |
%% |
The literal % character |
% |
Example
#!/usr/bin/perl
use POSIX qw(strftime);
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
printf("Time and date - $datestringn");
$datestring = strftime "%Y-%m-%d %H:%M:%S", gmtime;
printf("Time and date - $datestringn");
The output of the above example is:
Time and date - 2016-06-12 12:15:13 Time and date - 2016-06-12 04:15:13
YouTip