I'm struggling to figure out how to compare MySQL dates with current system time using perl.
I have a script that runs on a cron job that will send notification if the current system date/time is past the date/time of a returned record:
An application displays a table view:
EventId Device Location
CC: 123 something BFE
TT: 456 anotherthing BFE
How the script works is it finds values in the field EventID, parses the type (CC:, TT:, etc) from the ID (the numeric portion). The ID is a Unique ID in another database/table which contains an end time field. EventID itself is not Unique and may have duplicate items in the table. Sub routines for each "type" exist since each type has a different database and/or table.
The goal is that the script runs every minute and toggles the value of Expired. It is possible for something to expire, a change to be made and subsequently unexpire.
The script works fine save for 1 issue which appears to be related to timezones based off of the feed back I've recieved here so far. If I don't implicitly set the timezone to 'America/New_York'($now
) for the current system time , it is off by a number of hours ($notz
). therefore I need to find a way to make the date returned from MySQL be compared with the current system time accurately.
$now->set_time_zone('America/New_York')
does not seem to work either.
I'm unsure of how to do this, or even if the code I have thus far is the best approach (Still fairly new to Perl):
#!/usr/bin/perl
use DBI;
use DateTime;
use DateTime::Format::MySQL;
use Switch;
my $now = DateTime->now(time_zone => 'America/New_York');
my $notz = DateTime->now();
my $maindb = DBI=>connect(database);
my $seteventsql = qq { select * from view where EventId like 'IE:' or EventId like 'TT:' or EventId like 'CC:';};
my $commit = $livedb->prepare($seteventsql);
$commit->execute() || die "could not set event: $DBI::errstr";
while(@events = $commit->fetchrow_array()) {
(my $type, my $id) = split (/ /,$events[0]);
$id =~ s|\D||g;
switch ($type) {
case ('CC:') {check_expired_case($id);}
case ('TT:') {check_expired_task($id);}
case ('IE:') {check_expired_event($id);}
}
}
sub check_expired_case {
my $id = shift; #id = 123
my $sql = qq { select id, status_id, item_enddate from item where id = ?; };
my $exec = $itemdb->prepare($sql);
$exec->execute($id);
while(my @row = $exec->fetchrow_array()) {
my $status = $row[1];
my $end = DateTime::Format::MySQL->parse_datetime($row[2]);
if ($now > $end || $status ne 3 || $status ne 6) {
$sql = qq { update item set Expired = 1 where EventId = '$eventid';};
$maindb->do($sql)
}else{
$sql = qq { update item set Expired = 0 where EventId = '$eventid';};
$maindb->do($sql)
}
}
$exec->finish();
}
NoTZ: 2010-09-10T01:27:19
Now: 2010-09-09T21:27:19
End: 2010-09-10T17:00:00
Thanks in advance. I hope I've explained this well enough, its hard difficult to explain how everything relates.