Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl get UTC time and get difference between UTC timestamps

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.

like image 456
Dangeruss Avatar asked Dec 02 '22 23:12

Dangeruss


1 Answers

  1. 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.

  2. You can make 'date' return UTC time using 'date -u'

  3. 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).

  4. Use DateTime::Format::MySQL to parse MySQL date/time values. You may also find DateTime useful. Also, see http://datetime.perl.org/

like image 169
zgpmax Avatar answered Dec 18 '22 09:12

zgpmax