Re: epoch time
by ikegami (Patriarch) on Jan 31, 2007 at 15:19 UTC
|
A regexp, a lookup table and (core module) Time::Local.
use Time::Local qw( timelocal );
my %month_num_by_name = (
JAN => 1,
FEB => 2,
...
);
my ($d, $m, $y) = split(/-/, $date);
$m = $month_num_by_name{$m};
my $time = timelocal(0, 0, 0, $d, $m-1, $y);
Update: Added code.
| [reply] [d/l] |
|
I think timelocal() wants the year to be years since 1900 so code shoud perhaps be
my $time = timelocal(0, 0, 0, $d, $m-1, $y - 1900);
Also, for this particular application, why not set the hash up with the subtraction by one already done?
my %month_num_by_name = (
JAN => 0,
FEB => 1,
...
);
Cheers, JohnGG
Update: davorg and ikegami have pointed out that timelocal() does accept dates like 2007 without first subtracting 1900, although it does the right thing if you do the subtraction as well. ikegami also explained his excellent reason for doing the month the way he does. | [reply] [d/l] [select] |
|
| [reply] |
|
|
| [reply] [d/l] [select] |
|
Re: epoch time
by jbert (Priest) on Jan 31, 2007 at 17:36 UTC
|
The first rule of coding club is: "never implement your own date/time/timezone handling code".
The above advice about Time::Local is good, but if it weren't, your best option would be to effectively copy the code you need from one of the modules you listed.
If you were to consider doing it yourself, don't forget to think about whether you need to take account of: timezones (numeric), timezones (named), leap years, leap seconds, daylight savings time (in all the different timezones, changing over the years) and how comprehensive your test suite needs to be. I think timezones possibly also move on a scale of decades.
Also don't forget to think about whether someone else will be calling your code for similar purposes but who does need to worry about such things.
(The second rule of coding club is "never implement your own character escaping/unicode/charset conversion code").
| [reply] |
|
| [reply] |
|
Don't forget, "don't write your own encryption/hashing code"
| [reply] |
Re: epoch time
by spatterson (Pilgrim) on Jan 31, 2007 at 15:22 UTC
|
| [reply] |
|
Don't forget leap years...
| [reply] |
|
| [reply] |
Re: epoch time
by logie17 (Friar) on Jan 31, 2007 at 16:22 UTC
|
#! /usr/local/bin/perl
use Time::Local;
my $timestring = timelocal(0,0,0,31,0,2007);
print $timestring,"\n";
Outputs:
1170230400
Thanks,
s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;
| [reply] [d/l] [select] |
Re: epoch time
by Corion (Patriarch) on Jan 31, 2007 at 15:21 UTC
|
If you have Time::Piece, just use its strptime function to parse a time string and return a Time::Piece object.
Update: I missed the "without" in your query. If you don't want to use Time::Piece, go with ikegami's advice or install DateTime and DateTime::Format::Strptime.
| [reply] [d/l] |
Re: epoch time
by klekker (Pilgrim) on Jan 31, 2007 at 18:05 UTC
|
$ date --date=31-JAN-2007
Wed Jan 31 00:00:00 CET 2007
And converted:
$ date --date=31-JAN-2007 +%s
1170198000
(If you mean seconds since `00:00:00 1970-01-01 UTC' with "epoch seconds" and don't mind using backticks.) | [reply] [d/l] [select] |
Re: epoch time
by tubaandy (Deacon) on Jan 31, 2007 at 19:39 UTC
|
Out of curiosity, is there a reason to not use these modules? I have found Date::Manip to be exceedingly helpful. (OK, it's for a script that reminds people that it's their turn to bring bagels and coffee into the office. You'd be surprised how irritated folks get when they don't show up on time.)
| [reply] |
|
| [reply] |
|
| [reply] |
|
Hi,
Really it was very useful. Have a method in Date::Manip ie; $date = Date_NextWorkDay($date,$off ,$time); very useful. Its easy.
But had a problem here. Consider if I am adding 10 to a date that falls on saturday or sunday then the next working day must be similar to that when I add 10 working days with a date that falls on Friday.
But it doesnt happens like that with Date::Manip
I am not sure whether I have some mistake in understanding. But anyway fixed the problem by checing whether is a working day if yes then add 10 else add 9
Thanks
| [reply] |
Re: epoch time
by peters (Sexton) on Feb 01, 2007 at 21:16 UTC
|
Uh, there's always Date::Parse:
str2time("31-JAN-2007");
| [reply] |