I'm looking for some Perl help on how to do this properly. I don't mind using a library to do this.
I'm pulling a UTC time stamp from a mySQL database, in this format: 2012-02-06 13:50:09
I need Perl to pull the current UTC time and get the difference between the two time stamps in minutes (or seconds). I was doing this in a Unix subprocess, but I'm having difficulty getting the UTC time and comparing it, since the local box is running on Eastern time. I wouldn't mind doing this in Unix time, but not sure how to accurately get the UTC time to compare to the "last_timestamp" which is a UTC based timestamp from mysql.
my $date_diff = qx(date -d "\$(date +'%F %T')" +%s) -
qx(date -d "$last_timestamp" +%s);
As always your help is certainly appreciated and valued.
Paradigm error. Unix boxes do not "run on Eastern time" or "run on Pacific time". Unix boxes run on "seconds since epoch", and have a default time zone which is used for representing time values to users. Once you understand this, lots of other useful things come for free.
You can make 'date' return UTC time using 'date -u'
Perl's time
built-in function returns seconds since epoch (there is no time zone). You can then convert this to UTC Y-M-D-H-M-S values using Perl's gmtime
built-in function. Calling gmtime
with out any arguments is effectively the same as gmtime(time)
.
Use DateTime::Format::MySQL to parse MySQL date/time values. You may also find DateTime useful. Also, see http://datetime.perl.org/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With