The most suitable way to handle date and time in P erl is by using DateTime module.
Before every operation of date and time, we need to load DateTime in memory through our script.
The localtime() function if used without any argument returns the current date and time according to the system.
#!/usr/local/bin/perl $datetime = localtime(); print "Current date and time according to the system : $datetime\n";
A DateTime object displaying current date and time can be created by calling now constructor.
use DateTime; my $datetime = DateTime->now; print "$datetime\n";
We can also create a DateTime object by supplying all the details part wise like date, minute, second, etc. The perl will assume data as '0' where no detail will be passed.
use DateTime; $datetime = DateTime->new( day => 18, month => 7, year => 2003, hour => 12, ); print"$datetime\n";
In the above output, we haven't passed any detail for minute and second part. Hence it is assumed to be zero by Perl.
This function works similar to localtime() function with only difference that the gmtime() returned value is localized for the standard Greenwich time zone.
#!/usr/local/bin/perl $gmt = gmtime(); print "$gmt\n"; $local = localtime(); print "$local\n";
The epoch time refers to the number of seconds after a specific date and time. This specific date and time varies for different operating systems. For example, for Unix it is January 1, 1970. As all the operating systems have different epoch time, you can't assume epoch time for one system from another system.
#!/usr/local/bin/perl $epoch = time(); print "$epoch\n";
The Perl POSIX strftime() function is used to format date and time with the specifiers preceded with (%) sign. There are two types of specifiers, one is for local time and other is for gmt time zone.
|%a||Represents weekday name in short||Sun|
|%A||Represents full weekday name||Sunday|
|%b||Represents month name in short||Jul|
|%B||Represents full month name||July|
|%c||Represents date and time||Fri Jan 6 12:34:07 2017|
|%h||Represents month name in short, same as %b||Jul|
|%r||12-hour format clock time||6:15:30 pm|
|%Z||Represents time zone|
|%C||Year divided by 100 and written in integers (00-99)||34|
|%d||Day of the month, zero padded (01-31)||33|
|%e||Day of the month, space padded (1-31)||33|
|%g||Week based year, last two digit (00-99)||05|
|%g||Week based year||2015|
|%H||Hour in 24 hours format||17|
|%I||Hour in 12hours format||05|
|%J||Day of year (001-366)||365|
|%m||Month in decimal number (01-12)||07|
|%n||New line character|
|%p||AM or PM||AM|
|%R||HH:MM time in 24 hour format||17:55|
|% t||Horizontal tab character|
|%T||ISO 8601 time format (HH:MM:SS)||21:45|
|%u||ISO 8601 weekday as number starting with Monday (1-7)|
|%U||week number with first Sunday as first day of week one (00-53)|
|%V||ISO 8601week number (00-53)|
|%w||Weekday as decimal number starting with Sunday(0-6)|
|% W||Week number with first Monday as first day of week one (00-53)||17|
|%y||Last two digits of a year (00-99)||2017|
|% Y||Full year|
|%z||ISO 8601 offset from UTC in time zone (1min =1, 1 hour = 100)||+100|
|%%||A % sign||%|
Sometimes we need to display a date or time in different formats. To do this, we can change the format in Perl as shown below.
#!/usr/bin/env perl use strict; use warnings; use 5.010; use DateTime; my $datetime = DateTime->new( day => 18, month => 7, year => 2003, hour => 12, minute => 00, second => 00, ); say $datetime; say $datetime->ymd; say $datetime->ymd('_'); say $datetime->hms; say $datetime->epoch; say $datetime->year; say $datetime->month; say $datetime->day; say $datetime->strftime( '%Y-%m-%d-%H-%M-%S' );