get unix time for a given date


D

daveyand

Hey guys,

I'm wracking my brain trying to get this working but cant seem to
fathom it out.

I have a date like: 03/14/06 and want to get the unixtime from it.

I have tried to use time local like so:

my ($mon, $day, $year) = ($date =~ m!(\d\d)/(\d\d)/(\d\d)!);
my $unixtime = timelocal(0, 0,0, $day, ($mon - 1), ($year - 1900));

And i get the following output:

Setting 03/14/06 to be unixtime
Day too small - -717264 > -24856
Sec too small - -717264 < 74752
Cannot handle date (0, 0, 0, 14, 2, 6) at ./look_for_mod.pl line 156


What am i doing wrong? I cant work it out....

Cheers for any help.
 
Ad

Advertisements

A

A. Sinan Unur

Hey guys,

I'm wracking my brain trying to get this working but cant seem to
fathom it out.

I have a date like: 03/14/06 and want to get the unixtime from it.

I have tried to use time local like so:

my ($mon, $day, $year) = ($date =~ m!(\d\d)/(\d\d)/(\d\d)!);
my $unixtime = timelocal(0, 0,0, $day, ($mon - 1), ($year - 1900));

$year is 6, so you are passing -1884 to the routine above.

Sinan
 
T

takarov2003

daveyand said:
Hey guys,

I'm wracking my brain trying to get this working but cant seem to
fathom it out.

I have a date like: 03/14/06 and want to get the unixtime from it.

I have tried to use time local like so:

my ($mon, $day, $year) = ($date =~ m!(\d\d)/(\d\d)/(\d\d)!);
my $unixtime = timelocal(0, 0,0, $day, ($mon - 1), ($year - 1900));

And i get the following output:

Setting 03/14/06 to be unixtime

Is your year in your input date really only two digits? If so, you may
just want to adjust for epoch year by adding 100 instead of subtracting
1900.
 
T

Tad McClellan

daveyand said:
I have a date like: 03/14/06 and want to get the unixtime from it.
my ($mon, $day, $year) = ($date =~ m!(\d\d)/(\d\d)/(\d\d)!);
my $unixtime = timelocal(0, 0,0, $day, ($mon - 1), ($year - 1900));


What are you passing to timelocal as the year value?

6 - 1900 = -1894

Day too small - -717264 > -24856
Sec too small - -717264 < 74752
Cannot handle date (0, 0, 0, 14, 2, 6) at ./look_for_mod.pl line 156


What am i doing wrong?


Passing a year value of -1894, instead of 106.
 
D

Dr.Ruud

daveyand schreef:
I have a date like: 03/14/06 and want to get the unixtime from it.

I have tried to use time local like so:

my ($mon, $day, $year) = ($date =~ m!(\d\d)/(\d\d)/(\d\d)!);
my $unixtime = timelocal(0, 0,0, $day, ($mon - 1), ($year - 1900));

Alternative:
perl -MDate::parse -le "print str2time('03/14/06')"

Beware: the returned value is in timezone +0000.


Tests:
perl -MDate::parse -le "print scalar gmtime(str2time('03/14/06'))"
perl -MDate::parse -le "print scalar localtime(str2time('03/14/06'))"
 
Ad

Advertisements

R

robic0

No, no. That's the 6th day of the 14th month in the year 3.

jue
Its March 14, 2006 in America. 3/14/6. Whats the date in your coutries?
Sweeden basackwards..
 
G

Gunnar Hjalmarsson

Tad said:
What are you passing to timelocal as the year value?

6 - 1900 = -1894


Passing a year value of -1894, instead of 106.

Or instead of 06. Two digit years don't need to be adjusted before being
passed to timelocal().

perldoc Time::Local
 
J

Jürgen Exner

robic0 said:
Its March 14, 2006 in America.

Really? I will have to ask my Brazilian friend tomorrow.
3/14/6. Whats the date in your coutries?

Well, the third day of the 14th month in the year 6.

However, because this is a global NG and the OP didn't make any statement
about which date format he was using it is only fair to assume that he was
using ISO format which is year-month-day.

jue
 
Ad

Advertisements

R

robic0

Really? I will have to ask my Brazilian friend tomorrow.


Well, the third day of the 14th month in the year 6.

However, because this is a global NG and the OP didn't make any statement
about which date format he was using it is only fair to assume that he was
using ISO format which is year-month-day.

jue
ISO? Well, blame it on the Catholic nuns when in gradeschool,
mm/dd/yy
 
Ad

Advertisements

G

Gunnar Hjalmarsson

Jürgen Exner said:
because this is a global NG and the OP didn't make any statement
about which date format he was using

But he did.
it is only fair to assume that he was
using ISO format which is year-month-day.

Not really.
 

Top