Perl 使用localtime函数的时间和日期

Perl 使用localtime函数的时间和日期,perl,Perl,我正在尝试使用Perl获得定制的日期和时间输出 有人能帮我得到想要的输出吗 #/usr/bin/perl-w 使用POSIX; 我的($sec、$min、$hour、$mday、$mon、$year、$wday、$yday、$isdst)=本地时间(time); $year+=1900; 打印“$sec、$min、$hour、$mday、$mon、$year、$wday、$yday、$isdst\n”; $now_string=localtime; 打印“$now\u字符串\n”; $date=

我正在尝试使用Perl获得定制的日期和时间输出

有人能帮我得到想要的输出吗

#/usr/bin/perl-w
使用POSIX;
我的($sec、$min、$hour、$mday、$mon、$year、$wday、$yday、$isdst)=本地时间(time);
$year+=1900;
打印“$sec、$min、$hour、$mday、$mon、$year、$wday、$yday、$isdst\n”;
$now_string=localtime;
打印“$now\u字符串\n”;
$date=strftime“%a%b%e%H:%M:%S%Y”,localtime;
打印“日期1为:$date\n”;
$date=strftime“%a-%B-%e”,localtime;
打印“日期2为:$date\n”;
$time=strftime“%H:%M:%S”,localtime;
打印“时间1为:$time\n”;
$time1=strftime“%h:%m:%s”,localtime;
打印“Time2为:$time1\n”;
输出
15,2,18,8,9,2017,0280,0
2017年10月8日星期日18:02:15
日期1:2017年10月8日星期日18:02:15
日期二:十月八日星期日
时间1是:18:02:15
时间2:10:1507465935
期望输出:
日期1为:2017年10月6日
日期2为:2017年10月6日
时间1是:23:35:10
时间2是:晚上11:35:10

Perl具有强大的内置帮助。首先运行以下命令:

perldoc POSIX
perldoc POSIX
看看strftime部分。 以下是ISO 8601样式的时间戳:

schumack@linux2 18> perl -MPOSIX -le 'print(strftime("%Y-%m-%dT%H:%M:%S", localtime))'
2017-10-07T00:11:41
schumack@linux2 18> perl -MPOSIX -le 'print(strftime("%Y-%m-%dT%H:%M:%S", localtime))'
2017-10-07T00:11:41

Perl有很好的内置帮助。首先运行以下命令:

perldoc POSIX
perldoc POSIX
看看strftime部分。 以下是ISO 8601样式的时间戳:

schumack@linux2 18> perl -MPOSIX -le 'print(strftime("%Y-%m-%dT%H:%M:%S", localtime))'
2017-10-07T00:11:41
schumack@linux2 18> perl -MPOSIX -le 'print(strftime("%Y-%m-%dT%H:%M:%S", localtime))'
2017-10-07T00:11:41
来自perldoc POSIX:

"strftime"
        Convert date and time information to string. Returns the string.

        Synopsis:

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

        The month ("mon"), weekday ("wday"), and yearday ("yday") begin at
        zero, i.e., January is 0, not 1; Sunday is 0, not 1; January 1st
        is 0, not 1. The year ("year") is given in years since 1900, i.e.,
        the year 1995 is 95; the year 2001 is 101. Consult your system's
        "strftime()" manpage for details about these and the other
        arguments.

        If you want your code to be portable, your format ("fmt") argument
        should use only the conversion specifiers defined by the ANSI C
        standard (C89, to play safe). These are "aAbBcdHIjmMpSUwWxXyYZ%".
        But even then, the results of some of the conversion specifiers
        are non-portable. For example, the specifiers "aAbBcpZ" change
        according to the locale settings of the user, and both how to set
        locales (the locale names) and what output to expect are
        non-standard. The specifier "c" changes according to the timezone
        settings of the user and the timezone computation rules of the
        operating system. The "Z" specifier is notoriously unportable
        since the names of timezones are non-standard. Sticking to the
        numeric specifiers is the safest route.

        The given arguments are made consistent as though by calling
        "mktime()" before calling your system's "strftime()" function,
        except that the "isdst" value is not affected.

        The string for Tuesday, December 12, 1995.

                $str = POSIX::strftime( "%A, %B %d, %Y",
                                         0, 0, 0, 12, 11, 95, 2 );
                print "$str\n";

缺少
使用严格;使用警告。避免
-w
。如果您想要
2017年10月06日
,为什么要使用
%a%b%e%H:%M:%S%Y
?如果您想要
2017年10月06日
,为什么要使用
%a-%b-%e
???如果您想要
11:35:10 PM
,为什么要使用
%h:%m:%s
?您不能选择随机格式说明符并期望获得所需的输出!阅读您的文档(
manstrftime
)。实际上
perldocposix
只显示了一个名为
strftime
的函数,但没有详细说明如何使用它。他们建议
man strftime
我第二个。