Perl localtime Function

Advertisements


Description

This function converts the time specified by EXPR in a list context, returning a nine-element array with the time analyzed for the current local time zone. The elements of the array are

 # 0  1    2     3     4    5     6     7     8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

If EXPR is omitted, uses the value returned by time.

$mday is the day of the month, and $mon is the month itself, in the range 0..11 with 0 indicating January and 11 indicating December.

$year is the number of years since 1900, not just the last two digits of the year. That is, $year is 123 in year 2023. The proper way to get a complete 4-digit year is simply: $year += 1900;

Syntax

Following is the simple syntax for this function:

localtime EXPR

Return Value

This function returns a string of the form: Thu Sep 21 14:52:52 2000 in scalar context and the individual time component values (seconds, minutes, hours, day of month, month, year, day of week, day of year, daylight savings time) in list context.

Example

Following is the example code showing its basic usage:

#!/usr/bin/perl -w
use POSIX;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                          localtime(time);
$year += 1900;
print "$sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst\n";
$now_string = localtime; 
print "$now_string\n";

$now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
print "$now_string\n";

When above code is executed, it produced following result

19, 58, 14, 1, 8, 2013, 0, 243, 0
Sun Sep  1 14:58:19 2013
Sun Sep  1 14:58:19 2013


Advertisements
Advertisements