Perl Time and Date
This section introduces how Perl handles time and date.
There are several functions in Perl for time manipulation:
- 1. time() function: Returns the number of seconds since January 1, 1970.
- 2. localtime() function: Gets the local timezone time.
- 3. gmtime() function: Gets the Greenwich Mean Time.
Current Time and Date
Next, let's look at the localtime() function, which returns the current time and date without any parameters.
The following 9 symbols represent different time and date parameters:
sec, # Seconds, 0 to 61
min, # Minutes, 0 to 59
hour, # Hours, 0 to 24
mday, # Day, 1 to 31
mon, # Month, 0 to 11
year, # Year, starting from 1900
wday, # Day of the week, 0-6, 0 being Sunday
yday, # Day of the year, 0-364, 365
isdst # True if daylight saving time is in effect
Here is an example:
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 in the system's current timezone, as shown in the following example:
Example
#!/usr/bin/perl
$datestring = localtime();
print "The time and date is: $datestring\n";
The output of the above example is:
The time and date is: Sun Jun 12 11:27:31 2016
Greenwich Mean Time (GMT)
The gmtime() function is similar to localtime(), but it returns the standard Greenwich Mean Time.
Example
#!/usr/bin/perl
$local_datestring = localtime();
print "Local time and date is: $local_datestring\n";
$gmt_datestring = gmtime();
print "GMT time and date is: $gmt_datestring\n";
The output of the above example is:
Local time and date is: Sun Jun 12 11:32:14 2016
GMT time and date is: 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 the time in the desired format. Formatting is done using the printf() function:
Example
#!/usr/bin/perl
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
printf("Formatted time: HH:MM:SS\n");
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 since January 1, 1970. Here is an example:
Example
#!/usr/bin/perl
$epoc = time();
print "The number of seconds since January 1, 1970 is: $epoc\n";
The output of the above example is:
The number of seconds since January 1, 1970 is: 1465702883
We can also output a time format we want:
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; # Seconds for one day ago
The output of the above example is:
Current time and date: 2023-6-12 11:35:23
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($epoc);
print "Yesterday's date and time: ";
printf("%d-%d-%d %d:%d:%d", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
print "\n";
Example output:
Current date and time: 2017-3-15 12:47:54
Yesterday's date and time: 2017-3-14 12:47:54
POSIX Function strftime()
The strftime() function can format the time in the way we want.
The following table lists some formatting symbols, with an asterisk (*) indicating that the format is dependent 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 | Year divided by 100 and truncated (00-99) | 20 |
%d | Day of the month (01-31) | 23 |
%D | Date, MM/DD/YY equivalent to %m/%d/%y | 08/23/01 |
%e | Day of the month, with a space padding for single digits (1-31) | 23 |
%F | YYYY-MM-DD shorthand equivalent to %Y-%m-%d | 2001-08-23 |
%g | Last two digits of the year (00-99) | 01 |
%G | Year | 2001 |
%h | Abbreviated month name * (same as %b) | Aug |
%H | 24-hour format (00-23) | 14 |
%I | 12-hour format (01-12) | 02 |
%j | Day of the year (001-366) | 235 |
%m | Month (01-12) | 08 |
%M | Minute (00-59) | 55 |
%n | Newline ( '\n') | |
%p | AM or PM | PM |
%r | Time (hh:mm:ss AM or PM), 12-hour * | 02:55:02 pm |
%R | 24-hour HH:MM time format, equivalent to %H:%M | 14:55 |
%S | Seconds (00-61) | 02 |
%t | Horizontal tab ( '\t') | |
%T | Time (24-hour) (hh:mm:ss), equivalent to %H:%M:%S | 14:55 |
%u | ISO 8601 weekday format, Monday is 1 (1-7) | 4 |
%U | Week number of the year, Sunday as the first day (00-53) | 33 |
%V | ISO 8601 week number (00-53) | 34 |
%w | Day of the week (0-6, 0 is Sunday) | 4 |
%W | Week number of the year, Monday as the first day (00-53) | 34 |
%x | Date format (mm/dd/yy) * | 08/23/01 |
%X | Time format * | 14:55:02 |
%y | Year, two digits (00-99) | 01 |
%Y | Year | 2001 |
%z | ISO 8601 time zone offset from UTC (1 minute=1, 1 hour=100) | +100 |
%Z | Time zone name * | CDT |
%% | Percent sign | % |
Example
#!/usr/bin/perl
use POSIX qw(strftime);
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
printf("Date and Time - $datestring\n");
# GMT formatted date and time
$datestring = strftime "%Y-%m-%d %H:%M:%S", gmtime;
printf("Date and Time - $datestring\n");
Output:
Date and Time - 2016-06-12 12:15:13
Date and Time - 2016-06-12 04:15:13