使用本地时间功能的时间和日期

时间:2017-10-06 18:29:57

标签: perl

我尝试使用Perl获取自定义日期和时间输出。

有人可以帮助我根据需要获得输出吗?

#!/usr/bin/perl -w

use POSIX;

my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
$year += 1900;
print "$sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst\n";

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

$date = strftime "%a %b %e %H:%M:%S %Y", localtime;
print "Date1 is: $date\n";

$date = strftime "%a-%B-%e", localtime;
print "Date2 is: $date\n";

$time = strftime "%H:%M:%S", localtime;
print "Time1 is: $time\n";

$time1 = strftime "%h:%m:%s", localtime;
print "Time2 is: $time1\n";

输出

15, 2, 18, 8, 9, 2017, 0, 280, 0
Sun Oct  8 18:02:15 2017
Date1 is: Sun Oct  8 18:02:15 2017
Date2 is: Sun-October- 8
Time1 is: 18:02:15
Time2 is: Oct:10:1507465935

期望的输出:

Date1 is: 06-Oct-2017
Date2 is: 06-10-2017
Time1 is: 23:35:10
Time2 is: 11:35:10 PM

2 个答案:

答案 0 :(得分:0)

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

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

来自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";

答案 1 :(得分:-1)

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

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