Time::Piece - A new way to handle dates and times
Starting with Perl 5.10, a module called Time::Piece has been added to the standard module to handle time conveniently. If you're using Perl 5.10 or higher, the Time::Piece module is useful if you want to work with dates and times.
In traditional Perl, standard modules didn't have an intuitive way to handle dates and times. It took a little tedious work to handle dates and times. Time::Piece provides intuitive manipulation of dates and times. It also has a date/time formatting function and an analysis function.
Perl has a module called DateTime, which is essentially the standard for date-handling modules. However, this module must be installed from CPAN, not the standard module. Also, since it is a huge module, it takes a lot of time to load the module. You may be a little dissatisfied with the execution speed except in an environment where modules can be expanded in memory such as mod_perl.
Think of Time::Piece as a fast, lightweight module with some functionality.
For general information on dates and times, please refer to the following.
Acquisition of time/date information
Use the Time::Piece module as follows: When used, the Time::Piece module overwrites the core localtime and gmtime.
use Time::Piece; # Get Time::Piece object my $t = localtime; # Get date and time information my $year = $t->year; my $month = $t->mon; my $mday = $t->mday; my $hour = $t->hour; my $minute = $t->minute; my $second = $t->sec;
You can get the Time::Piece object by calling localtime, and you can get the date and time information from now on.
The case where the time information is got by the builtin functions localtime is described for comparison.
# When time information is got by localtime of builtin functions my ($second, $minute, $hour, $mday, $month, $year) = localtime; # Month starts from 0, so add 1 $month += 1; # Year is obtained by subtracting 1900, so add 1900 $year += 1900;
It is more intuitive to use localtime of Time::Piece to handle date and time information.
List of information that can be obtained with Time::Piece
A list of information that can be obtained with Time::Piece.
$t->year | Year |
$t->_ year | Year-1900 (same as localtime for builtin functions) = "aaa" |
$t->yy | Last two digits of the year |
$t->mon | Month (starting from 1) |
$t->_mon | Month (starting at 0, same as the builtin functions localtime) |
$t->monname | Abbreviated name of month (Feb, etc.) |
$t->month | Same as $t->monname |
$t->fullmonth | Month name (February, etc.) |
$t->mday | Day |
$t->day_of_month | Same as $t->mday |
$t->hour | 24 hour |
$t->min | Minutes |
$t->minute | Same as $t->min |
$t->sec | Seconds |
$t->second | Same as $t->sec |
$t->wday | Week number (1 is Sunday) |
$t->_wday | Week number (0 is Sunday) |
$t->day_of_week | $t->_wday |
$t->wdayname | Weekly abbreviation (Tue, etc.) |
$t->day | $t->wdayname |
$t->fullday | Week name (Tuesday, etc.) |
$t->yday | How many days in the year |
$t->day_of_year | Same as $t->yday |
$t->isdst | Whether it is daylight saving time |
$t->daylight_savings | Same as $t->isdst |
$t->epoch | Seconds from epoch |
Judgment of date
You can use Time::Piece to determine if it is a leap year and if it is the last day of the month.
# Is it a leap year? $t->is_leap_year # Get the last day of the month (return the number 28-31) $t->month_last_day
Format date/time
You may want to format the date/time and output it. Time::Piece provides several fixed formats. You can also format it freely using the strftime method.
[A] Formats provided by default
The formats provided by default are as follows.
$t->hms | 12:34:56 |
$t->hms(".") | 12.34.56 |
$t->time | Same as $t->hms |
$t->ymd | 2000-02-29 |
$t->date | Same as $t->ymd |
$t->mdy | 02-29-2000 |
$t->mdy("/") | 02/29/2000 |
$t->dmy | 29-02-2000 |
$t->dmy(".") | 29.02.2000 |
$t->datetime | 2000-02-29T12:34:56 (ISO 8601) |
$t->cdate | Tue Feb 29 12:34:56 2000 |
"$t" | Same as $t->cdate |
[B] Format customization
If you want to customize the format freely, use the strftime method. Specify the time representation in $format.
$t->strftime($format)
This is an example format of date and time.
$t->strftime('%Y-%m-%d%H:%M:%S'); # 2009-11-34 12:14:15
A list of formats that can be used with strftime.
%a: Abbreviated name of the day of the week %A: Day of the week name %b: month abbreviation %B: Month name %c: Default format %C: First two digits of the year %d: days (01 to 31) %D: Same as%m /%d /%y. Month day year %e: day (1 to 31) %F: Same as%Y-%m-%d (for example, 2008-11-31) %G: 4 digits per year. If the year and week of ISO 8608 format is the previous year or the next year, that year. %g: Last two digits of the year. If the year and week of ISO 8608 format is the previous year or the next year, that year. %h: Same as%b. Month abbreviation %H: Hour (00 to 23) %I: Hour (01 to 12) Time in 12-hour notation %j: What day is it for the first time from the beginning of the year? (001 to 366) %k: Hour (0 to 23) The second digit of the one-digit number (0 to 9) is a space. %l: Hour (1 to 12) Time in 12-hour notation The second digit of the one-digit number (0 to 9) is a space. %m: Month number (01-12) %M: Minutes (00 to 59) %n: Newline character %N: Milliseconds (Write%3N displays milliseconds in 3 digits,%6N displays milliseconds in 6 digits) %p: am or pm %P: AM or PM Same as%r:%I:%M:%S%p. (11:55:23 PM etc.) %R: hour and minute (for example, 15:16) %s: Seconds from epoch %S: seconds (00 to 61) %t: tab character Same as%T:%H:%M:%S. Hours, minutes and seconds (such as 23:14:03) %u: Day of the week number (1 to 7. Monday is 1) %U: How many weeks from the beginning of the year? Count the first Sunday of the year (00-53) as the beginning of the week. The first Sunday week is 01. The week before that was 00. %V: How many weeks from the beginning of the year? (01-53) A week with at least 4 days is the first week of the year. Count Monday as the beginning of the week. Year and week in ISO 8608 format. %w: Day of the week number (0 to 6. Sunday is 0) %W: Weeks counting from the beginning of the year (00 to 53) Count the first Monday of the year as the beginning of the week. The first Monday week is 01. The week before that was 00. %x: Default format for dates %X: Default format for time %y: last two digits of the year %Y: year %z: Time zone time shift from UTC (Coordinated Universal Time)) %Z: Timezone name %%:%
Precautions when handling Japanese
Note that Time::Piece's strftime method always returns a byte string, whether the input is a byte string or an decoded string.
# Byte string is always returned my $bytes = $tp->strftime("%Y year%m month");
Parse the string that expresses the date/time
You can parse date and time representations to create Time::Piece objects. Use the strptime method to parse dates and times.
my $t = Time::Piece->strptime(string representing date/time, format);
As an example, let's parse the MySQL date representation and create a Time::Piece object. Use the above strftime format for the format.
my $datetime_mysql = '2009-12-31 23:59:59'; my $t = Time::Piece->strptime($datetime_mysql, '%Y-%m-%d%H:%M:%S');
Localization
By default, month and week names are in English. If you want to get the Japanese week name, pass an array of day names to the method to get the week name.
my @week_names = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'); my $t = Time::Piece::localtime; # Get week name in Japanese my $wday = $t->wdayname(@week_names);
Calculation of date/time information
Tempe::Piece object subtraction
Time::Piece can also calculate dates and times. If you subtract between Time::Piece objects, the return value will be a Time::Seconds object. This object is in seconds when evaluated as a string.
my $sec = $t2- $t1;
You can also convert Time::Soconds objects to days and years.
$sec->seconds; $sec->minutes; $sec->hours; $sec->days; $sec->weeks; $sec->months; # 30 days $sec->financial_months;
The conversion to days and years is done according to the following rules. One day is converted to 24 hours. One week is converted to 7 days. One year is converted to 365.24225 days. One year is converted to 12 months.
Addition of Time::Piece objects
Time::Piece objects cannot be added to each other. You can add seconds or a Time::Seconds object to the Timpe::Piece object.
$t + 533; $t + Time::Seconds->new(300);
Time::Seconds also provides constants that can be added to the Time::Piece object.
ONE_DAY ONE_WEEK ONE_HOUR ONE_MINUTE ONE_MONTH ONE_YEAR ONE_FINANCIAL_MONTH LEAP_YEAR NON_LEAP_YEAR
To add 5 days: The Time::Seconds module must be loaded to take advantage of these constants.
use Time::Seconds; $t + ONE_DAY * 5
Date comparison
You can use comparison operators to compare dates. You can use "<", ">", "<=", " >= ", "<=>", "==" and " != " .
$t1 < $t2
Create Time::Piece object
As far as I read the document, it seems that there is no choice but to use the strptime method to create a Time::Piece object at any date and time.
my $t = Time::Piece->strptime('2009-12-31 23:59:59', '%Y-%m-%d%H:%M:%S');
(When I read the source code, there is a parse method, but when I tried it, it seems to be broken ... It seems that it is private because it is not in the document.)
Get an object that represents today's date
To use the Time::Piece module to get an object that represents today:
use Time::Piece; my $today = today (); sub today { # Today (YYYY-mm-dd HH: MM: SS) my $today_