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

后端 未结 9 1033
礼貌的吻别
礼貌的吻别 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:41

    I made a little test (Perl v5.20.1 under FreeBSD in VM) calling the following blocks 1.000.000 times each:

    A

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    my $now = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
    

    B

    my $now = strftime('%Y%m%d%H%M%S',localtime);
    

    C

    my $now = Time::Piece::localtime->strftime('%Y%m%d%H%M%S');
    

    with the following results:

    A: 2 seconds

    B: 11 seconds

    C: 19 seconds

    This is of course not a thorough test or benchmark, but at least it is reproducable for me, so even though it is more complicated, I'd prefer the first method if generating a datetimestamp is required very often.

    Calling (eg. under FreeBSD 10.1)

    my $now = `date "+%Y%m%d%H%M%S" | tr -d "\n"`;
    

    might not be such a good idea because it is not OS-independent and takes quite some time.

    Best regards, Holger

    0 讨论(0)
  • 2021-01-30 02:41

    Time::Piece::datetime() can eliminate T.

    use Time::Piece;
    print localtime->datetime(T => q{ });
    
    0 讨论(0)
  • 2021-01-30 02:43

    Use strftime in the standard POSIX module. The arguments to strftime in Perl’s binding were designed to align with the return values from localtime and gmtime. Compare

    strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)
    

    with

    my          ($sec,$min,$hour,$mday,$mon,$year,$wday,     $yday,     $isdst) = gmtime(time);
    

    Example command-line use is

    $ perl -MPOSIX -le 'print strftime "%F %T", localtime $^T'
    

    or from a source file as in

    use POSIX;
    
    print strftime "%F %T", localtime time;
    

    Some systems do not support the %F and %T shorthands, so you will have to be explicit with

    print strftime "%Y-%m-%d %H:%M:%S", localtime time;
    

    or

    print strftime "%Y-%m-%d %H:%M:%S", gmtime time;
    

    Note that time returns the current time when called whereas $^T is fixed to the time when your program started. With gmtime, the return value is the current time in GMT. Retrieve time in your local timezone with localtime.

    0 讨论(0)
  • 2021-01-30 02:45

    Try this:

    use POSIX qw/strftime/;
    print strftime('%Y-%m-%d',localtime);
    

    the strftime method does the job effectively for me. Very simple and efficient.


    0 讨论(0)
  • 2021-01-30 02:45

    Short and sweet, no additional modules needed:

    my $toDate = `date +%m/%d/%Y" "%l:%M:%S" "%p`;
    

    Output for example would be: 04/25/2017 9:30:33 AM

    0 讨论(0)
  • 2021-01-30 02:48

    if you just want a human readable time string and not that exact format:

    $t = localtime;
    print "$t\n";
    

    prints

    Mon Apr 27 10:16:19 2015
    

    or whatever is configured for your locale.

    0 讨论(0)
提交回复
热议问题