Perl | Date and Time

21 Nov, 2019
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() function in Perl returns the current date and time of the system, if called without passing any argument.
#!/usr/local/bin/perl  $datetime = localtime();  print "Local Time of the System : $datetime\n"; 
Local Time of the System : Fri Nov 15 07:21:05 2019

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.
#!/usr/bin/perluse DateTime;  # Creation of Timestampmy $datetime = DateTime->now;  print "$datetime\n";  
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‘.
#!/usr/bin/perluse DateTime;   # Assigning values to variable$datetime = DateTime->new(      day        => 18,      month      => 7,      year       => 2003,      hour       => 12,);    print"$datetime\n";  
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.
#!/usr/local/bin/perl  # Using function gmtime()$datestring = gmtime();  # Printing GMT timeprint "Date and time in GMT: $datestring\n";
Date and time in GMT: Fri Nov 15 11:10:05 2019

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.
#!/usr/local/bin/perl  # Assigning values to variables($sec, $min, $hour) = localtime();  printf("Time Format - HH:MM:SS\n");  # Formatting the time representationprintf("%02d:%02d:%02d",         $hour, $min, $sec);
Time Format - HH:MM:SS
11:38:06

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.
#!/usr/local/bin/perl    # Calculating epoch time$epoch = time();    print "$epoch\n";  
1573818532

Picked
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.