【问题标题】:Perl get UTC time and get difference between UTC timestampsPerl 获取 UTC 时间并获取 UTC 时间戳之间的差异
【发布时间】:2012-02-28 06:00:25
【问题描述】:

我正在寻找有关如何正确执行此操作的 Perl 帮助。我不介意使用库来执行此操作。

我正在从 mySQL 数据库中提取 UTC 时间戳,格式如下:2012-02-06 13:50:09

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

my $date_diff = qx(date -d "\$(date +'%F %T')" +%s) -
qx(date -d            "$last_timestamp" +%s); 

与往常一样,您的帮助肯定会受到赞赏和重视。

【问题讨论】:

    标签: linux perl bash shell


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

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

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

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

    【讨论】:

    • 您不必致电gmtime(time)gmtime()基本上 为您致电time()。 (补充回答)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      这是使用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 difference is 2078 seconds
      

      听起来不错。

      【讨论】:

      • 如果要解析 MySQL 时间戳,请使用 DateTime::Format::MySQL
      【解决方案4】:
      1. 致电time
      2. MySQL获取UNIX_TIMESTAMP(your_datetime_field)
      3. 减去。
      4. Div 和 mod 60。
      5. sprintf ("%02d min %02d s", $delta_min, $delta_s);

      【讨论】:

        【解决方案5】:

        使用时间::本地;

        我的@t = 本地时间(时间); 我的 $gmt_offset_in_seconds = timegm(@t) - timelocal(@t);


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

        【讨论】:

        • 请使用 StackOverlfow 中可用的格式从文本正确格式化您的编程。
        猜你喜欢
        • 2015-01-08
        • 2014-03-31
        • 2011-12-24
        • 2012-05-29
        • 2014-11-21
        • 2017-05-31
        • 1970-01-01
        • 2019-08-25
        • 2016-04-01
        相关资源
        最近更新 更多