Linux Perl 获取 UTC 时间并获取 UTC 时间戳之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9165572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:25:27  来源:igfitidea点击:

Perl get UTC time and get difference between UTC timestamps

linuxperlbashshell

提问by user1130364

I'm looking for some Perl help on how to do this properly. I don't mind using a library to do this.

我正在寻找有关如何正确执行此操作的 Perl 帮助。我不介意使用图书馆来做到这一点。

I'm pulling a UTC time stamp from a mySQL database, in this format: 2012-02-06 13:50:09

我正在从 mySQL 数据库中提取 UTC 时间戳,格式如下: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.

我需要 Perl 来提取当前的 UTC 时间并以分钟(或秒)为单位获取两个时间戳之间的差异。我是在 Unix 子进程中执行此操作的,但是由于本地框在东部时间运行,因此无法获取 UTC 时间并进行比较。我不介意在 Unix 时间这样做,但不确定如何准确地获得 UTC 时间与“last_timestamp”进行比较,后者是来自 mysql 的基于 UTC 的时间戳。

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.

一如既往,您的帮助当然受到赞赏和重视。

回答by Dmitry Ovsyanko

  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);
  1. 称呼 time
  2. UNIX_TIMESTAMPMySQL 中获取(your_datetime_field)
  3. 减去。
  4. Div 和 mod 60。
  5. sprintf ("%02d min %02d s", $delta_min, $delta_s);

回答by JRFerguson

One way is to use the core Time::Piece

一种方法是使用核心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";

回答by JRFerguson

Here's a solution using DateTime

这是使用的解决方案 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=;
  $month=;
  $day=;
  $hour=;
  $min=;
  $sec=;
}
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.

这听起来是对的。

回答by zgpmax

  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 timebuilt-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 gmtimebuilt-in function. Calling gmtimewith out any arguments is effectively the same as gmtime(time).

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

  1. 范式错误。Unix 机器不会“在东部时间运行”或“在太平洋时间运行”。Unix 机器以“自纪元以来的秒数”运行,并有一个默认时区,用于向用户表示时间值。一旦你理解了这一点,许多其他有用的东西就会免费获得。

  2. 您可以使用 'date -u' 使 'date' 返回 UTC 时间

  3. Perl 的time内置函数返回自纪元以来的秒数(没有时区)。然后,您可以使用 Perl 的gmtime内置函数将其转换为 UTC YMDHMS 值。不gmtime带任何参数的调用实际上与gmtime(time).

  4. 使用DateTime::Format::MySQL解析 MySQL 日期/时间值。您可能还会发现DateTime很有用。另请参阅http://datetime.perl.org/

回答by OGICT

use Time::Local;

使用时间::本地;

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

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



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

您拥有偏移量,现在可以随时根据偏移量进行调整。