如何在Perl中使用时间戳获取微秒?

时间:2015-10-20 21:12:54

标签: perl

这可能是一个愚蠢的问题,但我无法找到答案:

我有一个Python代码,它打印时间戳如下:

2015-05-19 22:27:00.688441

生成此字符串的代码如下所示:

print str(datetime.datetime.strptime(time_value, '%Y-%m-%d %H:%M:%S.%f'))

我想在Perl(版本5.14)中做同样的事情。除了最后一个小数部分(%f)之外,我可以获得此字符串的所有部分。

如何在Perl中获得完全相同的字符串?

PS:我尝试使用Time::HiRes模块。

use Time::HiRes;
my ($seconds, $microseconds) = gettimeofday;

如果以某种方式我可以$seconds使用localtime(),是否可能?如果是这样,那怎么样?

对原始问题的扩展:如果我有一个变量$time_value - 假设我从某个地方检索过此变量中的时间戳字符串,我该如何将其格式化为所需的格式,即带有微秒的时间戳?

4 个答案:

答案 0 :(得分:4)

最类似于Python的方式是安装和使用DateTime。请注意,微秒不是标准strptime格式的一部分。 DateTime使用%N表示小数秒。 %6N表示派系秒数到6位(微秒)。

# `DateTime->now` uses seconds, so we have to supply our own
# hires time.
use Time::HiRes qw(time);
use DateTime;

print DateTime->from_epoch( epoch => time )->strftime('%Y-%m-%d %H:%M:%S.%6N');

答案 1 :(得分:4)

您也可以使用Time::Moment。为了充分披露,我是Time :: Moment的作者。

当地时间:

$ perl -MTime::Moment -E'
say Time::Moment->now    
                ->strftime("%Y-%m-%d %H:%M:%S.%6N");
'
2015-10-21 19:48:56.333887

UTC时间:

$ perl -MTime::Moment -E'                                                                                       
say Time::Moment->now_utc
                ->strftime("%Y-%m-%d %H:%M:%S.%6N");
'
2015-10-21 17:48:57.805921

答案 2 :(得分:3)

    <!DOCTYPE html>
    <html>

    <head>
      <meta charset="UTF-8" />
      <title>Sebastien Caumes</title>
      <link rel="stylesheet" href="/sebastiencaumes/web/bundles/scsebastiencaumes/css/bootstrap.min.css">

      <link rel="stylesheet" href="/sebastiencaumes/web/bundles/scsebastiencaumes/css/global.css">



      <link rel="icon" type="image/x-icon" href="/sebastiencaumes/web/favicon.ico" />
    </head>

    <body>
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-12 header">
            <button type="button" class="btn btn-primary btn-circle btn-xl"><i class="glyphicon glyphicon-list"></i>
            </button>


            HEADER
          </div>
        </div>
        <div class="row">
          <div class="col-md-12">
            <div class="row">
              <div class="col-md-6">
                DESCRIPTIF COMPETENCES ETC
              </div>
              <div class="col-md-6">
                <div class="test-box">projet1
                  <a href="/sebastiencaumes/web/app_dev.php/work/3">clic</a>
                  <button type="button" class="btn btn-lg btn-danger" data-toggle="popover" title="Popover title" data-content="And here's some amazing content. It's very engaging. Right?">Click to toggle popover</button>


                  <script>
                    $(function() {
                      $('[data-toggle="popover"]').popover()
                    })
                  </script>
                </div>
                <div class="test-box">projet2</div>
                <div class="test-box">projet3</div>
                <div class="test-box">projet4</div>
                <div class="test-box">projet5</div>
                <div class="test-box">projet6</div>
                <div class="test-box">projet7</div>
                <div class="test-box">projet8</div>
                <div class="test-box">projet9</div>
                <div class="test-box">projet10</div>

              </div>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-md-12 footer">
            FOOTER
            <div class="pull-right">

              <i class="glyphicon glyphicon-blackboard"></i>
              <i class="glyphicon glyphicon-blackboard"></i>
              <i class="glyphicon glyphicon-blackboard"></i>
            </div>

          </div>
        </div>
      </div>

      <script src="/sebastiencaumes/web/bundles/scsebastiencaumes/js/jquery-2.1.4.min.js"></script>
      <script src="/sebastiencaumes/web/bundles/scsebastiencaumes/js/bootstrap.min.js"></script>
      <script src="/sebastiencaumes/web/bundles/scsebastiencaumes/js/global.js"></script>
</body>
</html>

(删除UTC的

    {% extends'SCSebastienCaumesBundle::layout.html.twig' %}

    {% block portfolio %}
        <div class="test-box">projet1
        <a href="{{ path('sc_sebastien_caumes_work_detail', {id : 3}) }}">clic</a>
            <button type="button" class="btn btn-lg btn-danger" data-toggle="popover" title="Popover title" data-content="And here's some amazing
             content. It's very engaging. Right?">Click to toggle popover</button>


            <script>
                $(function () {
                    $('[data-toggle="popover"]').popover()
                })
            </script>
        </div>
        <div class="test-box">projet2</div>
        <div class="test-box">projet3</div>
        <div class="test-box">projet4</div>
        <div class="test-box">projet5</div>
        <div class="test-box">projet6</div>
        <div class="test-box">projet7</div>
        <div class="test-box">projet8</div>
        <div class="test-box">projet9</div>
        <div class="test-box">projet10</div>
    {% endblock %}
。)

但DateTime有点沉重。 POSIX提供最轻的解决方案。

    {% extends'SCSebastienCaumesBundle::layout.html.twig' %}

    {% block portfolio %}
        <div class="test-box">projet1
        <a href="{{ path('sc_sebastien_caumes_work_detail', {id : 3}) }}">clic</a>
            <button type="button" class="btn btn-lg btn-danger" data-toggle="popover" title="Popover title" data-content="And here's some amazing
             content. It's very engaging. Right?">Click to toggle popover</button>


            <script>
                $(function () {
                    $('[data-toggle="popover"]').popover()
                })
            </script>
        </div>
        <div class="test-box">projet2</div>
        <div class="test-box">projet3</div>
        <div class="test-box">projet4</div>
        <div class="test-box">projet5</div>
        <div class="test-box">projet6</div>
        <div class="test-box">projet7</div>
        <div class="test-box">projet8</div>
        <div class="test-box">projet9</div>
        <div class="test-box">projet10</div>
    {% endblock %}

(对于UTC,将$ perl -E' use DateTime qw( ); my $time = 1445400376.20984; my $dt = DateTime->from_epoch( epoch => $time, time_zone => "local" ); say $dt->strftime("%Y-%m-%d %H:%M:%S.%6N"); ' 2015-10-20 21:06:16.209840 更改为time_zone => "local"。)

如果您还没有时间,可以在第一个片段中使用Time :: HiRes的$ perl -E' use POSIX qw( strftime ); my $time = 1445400376.20984; my $microsecs = ($time - int($time)) * 1e6; say sprintf("%s.%06.0f", strftime("%Y-%m-%d %H:%M:%S", localtime($time)), $microsecs); ' 2015-10-20 21:06:16.209840 ,在第二个片段中使用localtime

答案 3 :(得分:1)

使用Time :: Piece,它不支持小数秒,最后加上它们:

use 5.014;
use warnings;
use Time::Piece;
use Time::HiRes;

my ($seconds,$microseconds) = Time::HiRes::gettimeofday;
my $str = sprintf '%s.%06d', gmtime($seconds)->strftime('%Y-%m-%d %H:%M:%S'), $microseconds;