3.8. Printing a DateProblemYou need to print a date and time shown in Epoch seconds format in human-readable form. Solution
Simply call $STRING = localtime($EPOCH_SECONDS);
Alternatively, the use POSIX qw(strftime); $STRING = strftime($FORMAT, $SECONDS, $MINUTES, $HOUR, $DAY_OF_MONTH, $MONTH, $YEAR, $WEEKDAY, $YEARDAY, $DST);
The CPAN module Date::Manip has a use Date::Manip qw(UnixDate); $STRING = UnixDate($DATE, $FORMAT); Discussion
The simplest solution is built into Perl already: the
This makes for simple code, although it restricts the format of the string:
use Time::Local;
$time = timelocal(50, 45, 3, 18, 0, 73);
print "Scalar localtime gives: ", scalar(localtime($time)), "\n";
Of course,
use POSIX qw(strftime);
use Time::Local;
$time = timelocal(50, 45, 3, 18, 0, 73);
print "strftime gives: ", strftime("%A %D", localtime($time)), "\n";
All values are shown in their national representation when using POSIX::strftime. So, if you run it in France, your program would print
If you don't have access to POSIX's use Date::Manip qw(ParseDate UnixDate); $date = ParseDate("18 Jan 1973, 3:45:50"); $datestr = UnixDate($date, "%a %b %e %H:%M:%S %z %Y"); # as scalar print "Date::Manip gives: $datestr\n"; See Also
The Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|