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:
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:
my $lpost = $FORUMSDB->_datetime_to_unix($rec->{Last_Updated});
The error message I receive is the following:
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:
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:
$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:
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:
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
http://anthrotech.com/
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
http://anthrotech.com/