Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

DATETIME to UNIX time

Quote Reply
DATETIME to UNIX time
Convert DATETIME to UNIX time

Okay...I've been playing around with this all day and I am admitting defeat...I've looked through the Local.pm and am lost as how to take a DATETIME data type (year-mon-day 00:00:00) and convert it to UNIX time.

Here are the codes I am attempting to use:

Code:

sub _datetime_to_unix {
# --------------------------------------------------------
# Private method to translate a date into a unix time value.
#
my $date = shift;
my $timevar = shift;
my $time = 0;
my ($year, $mon, $day) = split /-/, $date;
my ($hour, $minute, $second) = split /:/, $timevar;
$year = $year - 1900;
$mon = $mon - 1;

require Time::Local;
eval {
$time = &Time::Local::timelocal(0,0,0,0,0,0, $year, $mon, $day, $hour, $minute, $second);
};
carp "DBSQL: Bad datetime: $date $timevar (parsed as '$year', '$mon', '$day', '$hour', '$minute', '$second'.\n" if ($DEBUG);
return $time;
}


I am referencing this sub in my forum script with the following codes:

Code:

my $lpost = $FORUMSDB->_datetime_to_unix($rec->{Last_Updated});


The error message I receive is the following:

Code:

DBSQL (2526): Fatal Error: Unable to load the DBI module:
'Day '0' out of range 1..31 at
/path/to/DBSQL.pm line 2048 ' at index.cgi line 103


Lines near 103 in index.cgi:

Code:

my $lpost = $FORUMSDB->_datetime_to_unix($rec->{Last_Updated});
$forums->{ForumNewPosts} = 1 if $lpost > $LAST;
$moderator = $forums->{ModeratorID};
undef $Links::DBSQL::DBH;
my $user = $USERDB->get_record ($moderator, 'HASH');


Line 2048 in DBSQL.pm:

Code:

$time = &Time::Local::timelocal(0,0,0,0,0,0, $year, $mon, $day, $hour, $minute, $second);


And even though that this error is thrown, the cookie is still
saved:

Code:

ATFORUMLogin
2001-08-11 20:23:21
anthrotech.com/
0974860928
29508007
3004037024
29434581
*


The "cookie" codes I am using are taken from jerrysu's forum.cgi
script:

Code:

my $flogin = $in->cookie('ATFORUMLogin');
($LAST) = $flogin =~ m,(\d+)$,;
my $cvalue = $LAST . "." . time();
$COOKIE = $in->cookie( -name => 'ATFORUMLogin', -value => $cvalue, -expires => '+1y', -path => '/', -domain => '.anthrotech.com' );


Of course, the problem is with the sub _datetime_to_unix...

Any suggestions would be great!

Thanks in advance.

Regards,

Eliot Lee Wink
http://anthrotech.com/
Quote Reply
Re: DATETIME to UNIX time In reply to
Hi Eliot,

I would strongly recommend storing all dates as int's in the database. It makes handling them much easier.

However, you may want to use GT::Date for this:

use GT::Date qw/:all/;
my $time = timelocal(parse_format($date_string));

Would do the trick. It assumes a format of yyyy-mm-dd, but you can specify different formats.

Cheers,

Alex

--
Gossamer Threads Inc.