Quickly getting to YYYY-mm-dd HH:MM:SS in Perl

后端 未结 9 1032
礼貌的吻别
礼貌的吻别 2021-01-30 02:30

When writing Perl scripts I frequently find the need to obtain the current time represented as a string formatted as YYYY-mm-dd HH:MM:SS (say 2009-11-29 14:28

9条回答
  •  既然无缘
    2021-01-30 02:55

    In many cases, the needed 'current time' is rather $^T, which is the time at which the script started running, in whole seconds (assuming only 60 second minutes) since the UNIX epoch.

    This to prevent that an early part of a script uses a different date (or daylight-saving status) than a later part of a script, for example in query-conditions and other derived values.

    For that variant of 'current time', one can use a constant, also to document that it was frozen at compile time:

    use constant YMD_HMS_AT_START => POSIX::strftime( "%F %T", localtime $^T );
    

    Alternative higher resolution startup time:

    0+ [ Time::HiRes::stat("/proc/$$") ]->[10]
    

提交回复
热议问题