In this chapter, we introduce to you the processing of time and date in Perl language.
There are several functions for processing time in Perl:
1. time() function: returns the accumulated number of seconds since January 1, 1970
2. localtime() function: get the local time zone time
3. gmtime() function: Get Greenwich Mean Time
Next let's look at the localtime() function, which returns the current time and date without arguments.
The following 9 symbols represent different time and date parameters:
sec, # seconds, 0 to 61min, # minutes, 0 to 59hour, # hours, 0 to 24mday, # days, 1 to 31mon, # months, 0 to 11year, # years, starting from 1900 wday, # day of the week, 0 -6,0 means Sunday yday, # The day of the year, 0-364,365isdst # True if daylight saving time is in effect
Example demonstration is as follows:
The execution output of the above example is:
Sunday 12 June
If localtime() is called directly, it returns the time of the system's current time zone setting. The example is as follows:
The execution output of the above example is:
The date and time is: Sun Jun 12 11:27:31 2016
The function gmtime() is similar to localtime(), but it returns standard Greenwich Time.
The execution output of the above example is:
The local time and date is: Sun Jun 12 11:32:14 2016GMT The time and date is: Sun Jun 12 03:32:14 2016
We can see from the example that the time in China is 8 hours different from Greenwich Mean Time.
We can use the 9 time elements of the localtime() function to output the format time that needs to be formulated. Formatted output uses the printf() function:
The execution output of the above example is:
Formatting time: HH:MM:SS11:35:23
We can get the epoch time using the time() function, which returns the number of seconds since January 1, 1970. Examples are as follows:
The execution output of the above example is:
The number of seconds accumulated since January 1, 1970 is: 1465702883
We can output a time format we want:
The execution 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
The function strftime() can format the time into the format we want.
The following table lists some formatting symbols. The * symbol indicates that you want to rely on local time:
symbol | describe | Example |
---|---|---|
%a | Abbreviation of day of the week (Sun..Sat) * | Thu |
%A | The full name of the day of the week (Sunday..Saturday) * | Thursday |
%b | Abbreviation of month (Jan..Dec) * | Aug |
%B | The full name of the month (January..December) * | August |
%c | Date and time* | Thu Aug 23 14:55:02 2001 |
%C | The year is divided by 100 and rounded ( 00-99 ) | 20 |
%d | The day of the month ( 01-31 ) | 23 |
%D | Date, MM/DD/YY is equal to %m/%d/%y | 08/23/01 |
%e | The day of the month, using spaces to fill single digits ( 1-31 ) | 23 |
%F | The abbreviation of YYYY-MM-DD is similar to %Y-%m-%d | 2001-08-23 |
%g | Last two digits of year ( 00-99 ) | 01 |
%g | Year | 2001 |
%h | Month abbreviation* (same as %b option) | 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 | Minutes ( 00-59 ) | 55 |
%n | new line ( 'n' ) |
|
%p | Show AM or PM | PM |
%r | Time (hh:mm:ss AM or PM), 12 hours* | 02:55:02 pm |
%R | 24-hour HH:MM time format, equivalent to %H:%M | 14:55 |
%S | Number of seconds ( 00-61 ) | 02 |
%t | Horizontal tab ( 't' ) |
|
%T | Time (24-hour format) (hh:mm:ss), equivalent to %H:%M:%S | 14:55 |
%u | ISO 8601 day of the week format, Monday is 1 ( 1-7 ) | 4 |
%U | Week number of the year, Sunday is the first day ( 00-53 ) | 33 |
%V | ISO 8601 week ( 00-53 ) | 34 |
%w | Day of the week (0 represents Sunday) ( 0-6 ) | 4 |
%W | Week number of the year, Monday is the first day ( 00-53 ) | 34 |
%x | Date display format (mm/dd/yy) * | 08/23/01 |
%X | Display time format* | 14:55:02 |
%y | Year, two digits ( 00-99 ) | 01 |
%Y | Year | 2001 |
%z | Time zone offset between ISO 8601 and UTC (1 minute=1, 1 hour=100) | +100 |
%Z | The name of the current time zone, such as "China Standard Time" * | CDT |
%% | % symbol | % |
The execution output of the above example is:
Time and date - 2016-06-12 12:15:13 Time and date - 2016-06-12 04:15:13