Perl | Date and Time

Perl is one of the most feature-rich programming languages. Perl is portable and cross-platform. Perl can run on over 100 platforms with highly integrated text processing ability. Perl contains the features of different languages like C, sed, awk, and sh etc. which makes the Perl more useful and productive.

Perl is an easy-to-use language. It is intended to be efficient and complete rather than elegant and minimal. Perl supports some major programming paradigms together with object-oriented, procedural, and practical.
Date and Time in Perl can be handled by using a predefined module of Perl called DateTime module. DateTime is a class for the representation of various combinations of dates and times. DateTime module in Perl represents the New Style calendar, extended backward in time before its creation (in 1582).
This is typically referred to as the “proleptic Gregorian calendar”.
In this calendar, the first day of the calendar (the epoch), is the first day of year 1, which corresponds to the date which was (incorrectly) believed to be the birth of Jesus Christ.

Syntax: use DateTime;

localtime()

localtime() function in Perl returns the current date and time of the system, if called without passing any argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/local/bin/perl  
$datetime = localtime();  
print "Local Time of the System : $datetime\n"

chevron_right


Output:



Local Time of the System : Fri Nov 15 07:21:05 2019

Creating a Timestamp

A timestamp in Perl is used to display the current Date and Time of the day. Data displayed by timestamp is sometimes accurate to a fraction of seconds.
Timestamp can be created by creating a DateTime object and then calling the now constructor.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
use DateTime;
  
# Creation of Timestamp
my $datetime = DateTime->now;  
print "$datetime\n";  

chevron_right


A DateTime object can also be created by providing all the details part wise like date, hour, minute, second, etc. Default value for variables with no value passed is ‘0‘.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
use DateTime; 
  
# Assigning values to variable
$datetime = DateTime->new(  
    day        => 18,  
    month      => 7,  
    year       => 2003,  
    hour       => 12,
);  
  
print"$datetime\n";  

chevron_right


Finding GMT Time

GMT stands for Greenwich Mean Time, which is the mean solar time at the Royal Observatory in Greenwich, London. GMT is the same during all the year and is not affected by Daylight Saving Time(Summer Time) clock changes.
Perl provides a predefined function for calculation and representation of GMT, which is gmtime(). This function works similar to localtime() function but the only difference is that the time values are localized for the Greenwich time zone only.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/local/bin/perl
  
# Using function gmtime()
$datestring = gmtime();
  
# Printing GMT time
print "Date and time in GMT: $datestring\n";

chevron_right


Output:

Date and time in GMT: Fri Nov 15 11:10:05 2019

Formatting Date and Time

localtime() function can also be used to print date and time in various formats as per the user’s requirement. This formatting can be easily done by using printf() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/local/bin/perl
  
# Assigning values to variables
($sec, $min, $hour) = localtime();
  
printf("Time Format - HH:MM:SS\n");
  
# Formatting the time representation
printf("%02d:%02d:%02d"
        $hour, $min, $sec);

chevron_right


Output:

Time Format - HH:MM:SS
11:38:06

Epoch Time

Epoch time refers to the number of seconds passed after a specific date and time. The specific date and time used to calculate epoch time vary from OS to OS. For example, for POSIX or UNIX systems, this date is January 1, 1970. Since this time varies from system to system, one cannot assume epoch time for any system.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/local/bin/perl  
  
# Calculating epoch time
$epoch = time();  
  
print "$epoch\n";  

chevron_right


Output:

1573818532



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.