Re: Convert epoch seconds to date
by lhoward (Vicar) on Mar 19, 2001 at 19:20 UTC
|
There are several perl modules that can do this for you.
My favorite for this task is
Time::CTime (check out the strftime function). This
task is also real easy to write yourself using perl's built-in localtime function.
Of course, this all assumes that your Epoch is Jan 1, 1970 (which is
a pretty safe bet)
If that isn't your epoch you'll need to do more conversion first.
| [reply] [Watch: Dir/Any] |
|
@time = (localtime())[3..5];
$time[1]++;
$time[2] += 1900;
print join ('/', @time);
can't get it shorter :( | [reply] [Watch: Dir/Any] [d/l] |
|
Update: I thought I was in a different thread so much of this is incorrect. My apologies to slayven. ):
Note that this does d-m-yyyy which isn't what was asked for (the order is wrong and you don't pad with zeros).
use mapcar;
print join '-', mapcar {sprintf "%02d",pop()+pop}
[(localtime())[5,4,3]],[1900,1,0];
Is my contribution (which does yyyy-mm-dd since that is simply a better format).
See mapcar -- map for more than one list for one needed part.
-
tye
(but my friends call me "Tye") | [reply] [Watch: Dir/Any] [d/l] |
|
my $epoch_seconds = "?";
my $date = scalar localtime($epoch_seconds);
| [reply] [Watch: Dir/Any] |
Re: Convert epoch seconds to date
by davorg (Chancellor) on Mar 19, 2001 at 20:34 UTC
|
That's what the localtime function does.
Pass it a number of epoch seconds and it'll return an
array of values that you can use to build a human-readable
date.
Or, even easier, use localtime in conjunction with the
POSIX::strftime function.
use POSIX 'strftime';
my $epoch = 985015768;
print strftime('%d/%m/%Y', localtime($epoch));
--
<http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
| [reply] [Watch: Dir/Any] [d/l] |
|
use POSIX 'strftime';
$epoch = $escrowClose_e;
$epoch_o = $escrowOpen_e;
$redate = strftime('%m/%d/%Y', localtime($epoch));
$opendate = strftime('%m/%d/%Y|%H:%M', localtime($epoch_o));
| [reply] [Watch: Dir/Any] [d/l] |
(tye)Re: Convert epoch seconds to date
by tye (Sage) on Mar 19, 2001 at 21:49 UTC
|
| [reply] [Watch: Dir/Any] |
Re: Convert epoch seconds to date
by lachoy (Parson) on Mar 19, 2001 at 22:30 UTC
|
perl -MDate::Format -e 'print time2str( "%Y-%m-%e", 192948519 )'
perl -MDate::Format -e 'print time2str( "%Y-%m-%e", 19294851 )'
Respectively returns:
1976-02-11
1970-08-12
Chris
M-x auto-bs-mode | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Convert epoch seconds to date
by frankus (Priest) on Mar 20, 2001 at 18:20 UTC
|
There are lots of good answers here, my answer is more generic: the Perl Cook Book has a section on this, and when you understand that bit of the book there are loads more examples that can help you everyday ;-)
--
Brother Frankus. | [reply] [Watch: Dir/Any] |
Re: Convert epoch seconds to date
by belize (Deacon) on Aug 04, 2001 at 23:58 UTC
|
How about this subroutine. I'm new to Perl, but think this works OK?:
sub unix_to_date {
my ($date) = $_[0]; # reads in variable passed to subroutine
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug sep Oct Nov Dec!; #c
+reates array of month names
@time = (localtime($date))[3,4,5]; #creates array of date from unix ti
+me
$time[2] += 1900; # converts year to proper format
return "$time[0]-$months[$time[1]]-$time[2]";
}
You now call this with:
$date_from_unix = &unix_to_date(epochtime);
| [reply] [Watch: Dir/Any] [d/l] [select] |