如何格式化Perl中的时间戳?

Bri*_*ian 16 format perl time timestamp clock

我想得到这个时间戳格式:

01/13/2010 20:42:03 - -
Run Code Online (Sandbox Code Playgroud)

除了年份,数字总是2位数,其中4位数.它基于24小时制.

我怎么能在Perl中做到这一点?我更喜欢原生功能.

Sin*_*nür 39

POSIX提供strftime:

$ perl -MPOSIX -we 'print POSIX::strftime("%m/%d/%Y %H:%M:%S\n", localtime)'
01/27/2010 14:02:34

你可能想写下这样的东西:

my ($sec, $min, $hr, $day, $mon, $year) = localtime;
printf("%02d/%02d/%04d %02d:%02d:%02d\n", 
       $day, $mon + 1, 1900 + $year, $hr, $min, $sec);
Run Code Online (Sandbox Code Playgroud)

作为避免POSIX的手段.别!POSIX.pm自1996年或1997年以来,AFAIK 一直处于核心地位.

  • 但也不要避免学习`localtime`(或`sprintf`). (2认同)