Copyright © tutorialspoint.com

PERL localtime Function


Syntax

localtime EXPR


Definition and Usage

In a list context, converts the time specified by EXPR, 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;

Return Value

  • In scalar context it returns a string of the form: Thu Sep 21 14:52:52 2000

  • In list context it returns the individual time component values (seconds, minutes, hours, day of month, month, year, day of week, day of year, daylight savings time).

Example

Try out following example:

#!/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";

It will produce following results:

51, 58, 10, 19, 2, 2007, 1, 77, 0
Mon Mar 19 10:58:51 2007
Mon Mar 19 10:58:51 2007

Copyright © tutorialspoint.com