Updated April 4, 2023
Introduction to Perl localtime
In Perl, localtime is defined as a time function which displays the date and time for the local time zones that usually converts a specified time into a certain list of time elements which are numerical data types for all the elements in the list returned by this localtime() function in Perl. In general, the localtime() function is defined as a function for returning the elements list containing the elements such as seconds, hours, minutes, etc., which are usually 9 elements in the list that are returned by this localtime() function to display information related to date and time of the system and this function does not take any argument as a compulsion.
Working of localtime() Function in Perl with Examples
In this article, we will see the Perl time function known as localtime(), which is defined as a function for returning the time elements related to the date and time of the system or program. In Perl, date and time codes usually need a Perl module known as the DateTime module, which contains various combinations of representation of date and time where; this module uses a New Style calendar, which is known as a geographic calendar.
In Perl, to display date and time that has analyzed time of the current time zone which is done by converting the given expression of time into a set of time elements which is an array of time elements and contains at least 9 element in this array which includes the time and date of the current time zone such as second, minute, hour, day, month, year, day of the week, day of the year, isdst.
Now let us see syntax and examples of localtime() function:
Syntax:
Use DateTime;
localtime();
In this, if there is no argument passed, then the localtime() function returns the current date and time of the timezone.
or with expression :
locatime expr;
In this, we can write expr anything which can display result related to time elements only.
In the above syntax, both can be used where the first one displays the set of time elements having 9 different elements defining the time of time zone. Then the second syntax can display only those time elements which are specified as expression (expr) in the syntax as seen in the above syntax section.
Example #1
Code:
#!/usr/local/bin/perl
use DateTime;
print "Demonstration of localtime() function in Perl";
print "\n";
print "\n";
print "The use of localtime() function without any arguments ";
print "\n";
$sys_dt = localtime();
print "The date and time of the system of the timezone which is used is :";
print "\n";
print $sys_dt;
print "\n";
print "The localtime() function to display particular format of date and time";
print "\n";
@mon_name = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
@day_name = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
print "The current day name and month name is: ";
print "\n";
print "$mday $mon_name[$mon] ";
print "$day_name[$wday] ";
Output:
In the above program, we can see we have first imported a Perl module for date and time as “use DateTime” so that we can use locatime() inbuilt function of Perl. In the above code, we are declaring a variable that stores the current date and time of the system where we are assigning this variable to store the value of the locatime() function and is displayed using the print command. In this, we have not passed any argument to the function, so it will just print the current day date and the current time in an hour, minutes and seconds along with the current year.
Then we are trying to use the localtime() function to display a particular date and time format, which we specify in print command where in the code we are printing only the current date with day and current month using localtime() function. Therefore we can use this function in both ways. We have written the month and day name in which format we want to display, and these are stored in the variables “mon_name” and “day_name” so that we can display in the same name which is defined here; we can also use full name to display here. The output can be seen in the above screenshot, which displays the system’s current date and time.
Now let us see another example where we are passing some argument to the locatime() function.
Example #2
Code:
#!/usr/local/bin/perl
use DateTime;
print "Demonstration of localtime() fucntion with argument in Perl";
print "\n";
print "\n";
$cur_dt = localtime();
print "The current date and time is as follows: ";
print "\n";
print $cur_dt;
print "The elapsed time and date can be represented using time() function";
print "\n";
$epoc = time();
$epoc = $epoc - 24 * 60 * 60;
print "The elapsed time in seconds to display yesterday's date and time is ";
print "\n";
print $epoc;
print "\n";
$cur_dt = localtime($epoc);
print "The epoc passed as argument for displayng yesterday's date and time:";
print "\n";
print $cur_dt;
Output:
In the above code, first, we import the “use DateTime” module for using the localtime() function. In this code, first, we have declared a variable to display the current date and time, which displays the current date and time of the system and then we are using another variable, “$epoc,” in which we are storing the number of seconds that are elapsed until the current day and time using time() function. Therefore we can see we have then passed this $epoc to the localtime() function, which displays yesterday’s date and time. The output for this code can be seen in the above screenshot.
Conclusion
In this article, we conclude that the localtime() function is a time function used to display the date and time of the time zone the system is using. This function returns the set or array of time elements which has 9 different elements of date and time such as hour, minutes, seconds, etc. In the article, we saw a simple example of using localtime() without argument and to display the particular date and time elements and also we saw another example where we are passing the elapsed time to display the elapsed date that is passed as an argument to the localtime() function.
Recommended Articles
This is a guide to Perl localtime. Here we discuss the introduction and working of the localtime() function in Perl with examples, respectively. You may also have a look at the following articles to learn more –