We’re currently using a 3rd party API that provides datetime in the following format:
Sat Mar 06 09:00:00 ICST 2010
Fri Feb 19 19:30:00 JDT 2010
Fri Feb 19 19:30:00 PST 2010
However, we want to store these datetime objects in MySQL in a standard datetime field which requires the following format:
YYYY-MM-DD HH:MM:SS
Right now we’re using the following code which is reporting errors for certain timezones such as KDT, JDT, and ICST:
use Date::Manip;
use DateTime;
use DateTime::Format::DateManip;
my $date = ParseDate($time);
$date = DateTime::Format::DateManip->parse_datetime($date);
eval{ $time = $date->strftime("%Y-%m-%d %H:%M:%S"); };
Can you recommend a better implementation of the Perl code above to convert the datetime objects from the API to the proper format and time on our server to be inserted into a MySQL datetime field?
Thanks in advance for your help & advice!
Store the times internally in GMT. Do all manipulations in GMT. Then at the last moment, just as you’re about to display results to the user, then convert to the user’s local time.
I recommend using Date::Parse, but you’ll have to augment its timezone offsets because it doesn’t currently have Indochina Summer Time and Japan Daylight Time, for example.
Output:
To support the query you’d like, store the time in GMT plus an offset (i.e., from GMT to the local time from the API). Note that the code below assumes that if
str2timecan parse a given time,strptimecan also. Change the loop toWith the times collected, render it as SQL:
The output is
and we can pipe it to
mysql: