这个只适用于GAWK
当你从下面的例子中看到, SYSTIME ( )返回POSIX划时代的时间时,即自1970年1月1日经过的秒数。
awk 'BEGIN { print systime() }'
1441097412
当您使用strftime函数去转换划时代时间为可读格式时SYSTIME函数变得更加有用。
下面的例子显示使用SYSTIME和strftime函数当前时间戳以可读格式。
$ awk 'BEGIN { print strftime("%c",systime()) }'
Tue 01 Sep 2015 04:56:47 PM CST
以下awk脚本列出了各种可能的日期格式。
$ cat strftime.awk
BEGIN {
print "--- basic formats --"
print strftime("Format 1: %m/%d/%Y %H:%M:%S",systime())
print strftime("Format 2: %m/%d/%y %I:%M:%S %p",systime())
print strftime("Format 3: %m-%b-%Y %H:%M:%S",systime())
print strftime("Format 4: %m-%b-%Y %H:%M:%S %Z",systime())
print strftime("Format 5: %a %b %d %H:%M:%S %Z %Y",systime())
print strftime("Format 6: %A %B %d %H:%M:%S %Z %Y",systime())
print "--- quick formats --"
print strftime("Format 7: %c",systime())
print strftime("Format 8: %D",systime())
print strftime("Format 8: %F",systime())
print strftime("Format 9: %T",systime())
print strftime("Format 10: %x",systime())
print strftime("Format 11: %X",systime())
print "--- single line format with %t--"
print strftime("%Y %t%B %t%d",systime())
print "--- multi line format with %n --"
print strftime("%Y%n%B%n%d",systime())
}
$ awk -f strftime.awk
--- basic formats --
Format 1: 09/01/2015 17:00:29
Format 2: 09/01/15 05:00:29 PM
Format 3: 09-Sep-2015 17:00:29
Format 4: 09-Sep-2015 17:00:29 CST
Format 5: Tue Sep 01 17:00:29 CST 2015
Format 6: Tuesday September 01 17:00:29 CST 2015
--- quick formats --
Format 7: Tue 01 Sep 2015 05:00:29 PM CST
Format 8: 09/01/15
Format 8: 2015-09-01
Format 9: 17:00:29
Format 10: 09/01/2015
Format 11: 05:00:29 PM
--- single line format with %t--
2015 September 01
--- multi line format with %n --
2015
September
01
以下是不同的时间格式标识符可以在strftime函数使用。请注意,如下所示的所有缩写取决于您的区域设置。这些实施例示出为英语(en) 。
Format Identifier | Description |
---|---|
%m | Month in two number format. January is shown as 01 |
%b | Month abbreviated. January is shown as Jan |
%B | Month displayed fully. January is shown as January. |
%d | Day in two number format. 4th of the month is shown as 04. |
%Y | Year in four number format. For example: 2011 |
%y | Year in two number format. 2011 is shown as 11. |
%H | Hour in 24 hour format. 1 p.m is shown as 13 |
%I | Hour in 12 hour format. 1 p.m is shown as 01. |
%p | Displays AM or PM. Use this along with %I 12 hour format. |
%M | Minute in two character format. 9 minute is shown as 09. |
%S | Seconds in two character format. 5 seconds is shown as 05 |
%a | Day of the week shown in three character format. Monday is shown as Mon. |
%A | Day of the week shown fully. Monday is shown as Monday. |
%Z | Time zone. Pacific standard time is shown as PST. |
%n | Displays a new line character |
%t | Displays a tab character |
Format Identifier | Description |
---|---|
%c | Displays the date in current locale full format. For example: Fri 11 Feb 2011 02:45:03 AM PST |
%D | Quick date format. Same as %m/%d/%y |
%F | Quick date format. Same as %Y-%m-%d |
%T | Quick time format. Same as %H:%M:%S |
%x | Date format based on your locale. |
%X | Time format based on your locale. |