Dangeruss
Dangeruss

Reputation: 73

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.

Upvotes: 4

Views: 16085

Answers (5)

OGICT
OGICT

Reputation: 1

use Time::Local;

my @t = localtime(time); my $gmt_offset_in_seconds = timegm(@t) - timelocal(@t);


You have the offset and can now adjust any time against the offset.

Upvotes: 0

user554546
user554546

Reputation:

Here's a solution using DateTime

use strict;
use warnings;
use DateTime;

my $date_str="2012-02-06 13:50:09";

my ($year,$month,$day,$hour,$min,$sec);

if($date_str=~/^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/)
{
  $year=$1;
  $month=$2;
  $day=$3;
  $hour=$4;
  $min=$5;
  $sec=$6;
}
else
{
  die "Couldn't parse timestamp $date_str\n";
}

my $dt=DateTime->new(
  year=>$year,
  month=>$month,
  day=>$day,
  hour=>$hour,
  minute=>$min,
  second=>$sec,
  time_zone=>"UTC"
);

my $now=DateTime->now;
$now->set_time_zone("UTC");

my $difference=$now->delta_ms($dt);

print "The difference is " . (60*($difference->minutes) + ($difference->seconds)) . " seconds\n";

The output is:

The difference is 2078 seconds

which sounds about right.

Upvotes: 1

Dmitry Ovsyanko
Dmitry Ovsyanko

Reputation: 1416

  1. Call time
  2. Fetch UNIX_TIMESTAMP(your_datetime_field) from MySQL
  3. Subtract.
  4. Div and mod 60.
  5. sprintf ("%02d min %02d s", $delta_min, $delta_s);

Upvotes: -1

zgpmax
zgpmax

Reputation: 2857

  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/

Upvotes: 15

JRFerguson
JRFerguson

Reputation: 7526

One way is to use the core Time::Piece

#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
my $when = q(2012-02-06 13:50:09);
my $t = Time::Piece->strptime( $when, "%Y-%m-%d %H:%M:%S" );
print "delta seconds = ", time() - $t->strftime("%s"),"\n";

Upvotes: 2

Related Questions