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

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

    Time::Piece (in core since Perl 5.10) also has a strftime function and by default overloads localtime and gmtime to return Time::Piece objects:

    use Time::Piece;
    print localtime->strftime('%Y-%m-%d');
    

    or without the overridden localtime:

    use Time::Piece (); 
    print Time::Piece::localtime->strftime('%F %T');
    

提交回复
热议问题