Time functions

systime ( )

1970-01-01 00:00:00 UTC (system epoch) 로부터 현재까지의 시간을 초 단위로 반환합니다.
이 값은 strftime 함수의 인수로 사용됩니다.

$ awk 'BEGIN { print systime() }' 
1524693474

mktime ( datespec )

systime 함수와 같이 system epoch 로부터의 시간을 초 단위로 반환하는데 차이점은 datespec 인수를 이용하여 특정일의 시간을 지정할 수 있습니다.

"YYYY MM DD HH MM SS"

(YYYY) 4자리, (MM) from 1 to 12, (DD) from 1 to 31,
(HH) from 0 to 23, (MM) from 0 to 59, (SS) from 0 to 60

# system epoch 로부터 2015년 6월 7일 8시 9분 10초 까지의 시간
$ awk 'BEGIN { 
    print mktime("2015 6 7 8 9 10") 
    print mktime("2015 06 07 08 09 10") 
}' 
1433632150
1433632150  # 같은 값
......................................

# '0' 일은 전달 마지막 일이 됩니다. ( 2 월이 28 일까지 인것을 알 수 있습니다 )
$ awk 'BEGIN{ print strftime("%Y-%m-%d %H:%M:%S", mktime("2017 3 0 0 0 0")) }'
2017-02-28 00:00:00

# 음수를 사용할 수도 있습니다.
$ awk 'BEGIN{ print strftime("%Y-%m-%d %H:%M:%S", mktime("2017 3 -1 0 0 0")) }'
2017-02-27 00:00:00

시간 차이 구하기

1 분은 60 초, 1 시간은 3600 초, 1 일은 86400 초 이므로

# 2017년 06월 07일 08시 09분 10초 로부터 현재까지의 시간 차이
$ awk 'BEGIN { print timediff( systime() - mktime("2017 6 7 8 9 10")) }
function timediff( d ) {
    day=86400; hour=3600; minute=60;
    return int( d / day ) " 일 " \
    int(( d % day ) / hour ) " 시간 " \
    int((( d % day ) % hour ) / minute ) " 분 " \
    int((( d % day ) % hour ) % minute ) " 초 " 
}'
3232 시간 3740

strftime ( [ format [, timestamp [, utc-flag ] ] ] )

strftime 함수는 포멧 스트링을 이용해 원하는 형식의 시간을 출력할 수 있습니다. systime, mktime 함수로부터 timestamp 값을 입력받아 사용할 수 있고, utc-flag 값이 non-null 이면 UTC 값이 적용됩니다.

UTC (Coordinated Universal Time, formerly GMT or Greenwich Mean Time).

$ awk 'BEGIN { print strftime() }' 
Thu Apr 26 07:07:05 KST 2018

$ awk 'BEGIN { 
    print strftime("%Y/%m/%d %H:%M:%S")
    print strftime("%Y/%m/%d %H:%M:%S", systime())
    print strftime("%Y/%m/%d %H:%M:%S", systime(), 1)   # UTC
}'
2018/04/26 07:03:37
2018/04/26 07:03:37
2018/04/25 22:03:37   # UTC
.............................................................

$ awk 'BEGIN { 
    print strftime("%Y/%m/%d %H:%M:%S", mktime("2015 6 7 8 9 10"))
    print strftime("%Y/%m/%d %H:%M:%S", mktime("2015 6 7 8 9 10"), 1)  # UTC
}'
2015/06/07 08:09:10
2015/06/06 23:09:10   # UTC

Format specifications & Descriptions

Spc Description Output
%a The locale’s abbreviated weekday name. Thu
%A The locale’s full weekday name. Thursday
%b The locale’s abbreviated month name. Apr
%B The locale’s full month name. April
%c The locale’s “appropriate” date and time representation.
(This is ‘%A %B %d %T %Y’ in the "C" locale.)
Thu 26 Apr 2018 09:36:07 AM KST
%C The century part of the current year. This is the year divided by 100 and truncated to the next lower integer. 20
%d The day of the month as a decimal number (01–31). 26
%D Equivalent to specifying ‘%m/%d/%y’. 04/26/18
%e The day of the month, padded with a space if it is only one digit. 26
%F Equivalent to specifying ‘%Y-%m-%d’. This is the ISO 8601 date format. 2018-04-26
%g The year modulo 100 of the ISO 8601 week number, as a decimal number (00–99). For example, January 1, 2012, is in week 53 of 2011. Thus, the year of its ISO 8601 week number is 2011, even though its year is 2012. Similarly, December 31, 2012, is in week 1 of 2013. Thus, the year of its ISO week number is 2013, even though its year is 2012. 18
%G The full year of the ISO week number, as a decimal number. 2018
%h Equivalent to ‘%b’. Apr
%H The hour (24-hour clock) as a decimal number (00–23). 21
%I The hour (12-hour clock) as a decimal number (01–12). 09
%j The day of the year as a decimal number (001–366). 116
%m The month as a decimal number (01–12). 04
%M The minute as a decimal number (00–59). 40
%n A newline character (ASCII LF). (newline)
%p The locale’s equivalent of the AM/PM designations associated with a 12-hour clock. AM
%r The locale’s 12-hour clock time.
(This is ‘%I:%M:%S %p’ in the "C" locale.)
09:42:13 AM
%R Equivalent to specifying ‘%H:%M’. 09:42
%S The second as a decimal number (00–60). 47
%t A TAB character. (tab)
%T Equivalent to specifying ‘%H:%M:%S’. 09:43:04
%u The weekday as a decimal number (1–7). Monday is day one. 4
%U The week number of the year (with the first Sunday as the first day of week one) as a decimal number (00–53). 16
%V The week number of the year (with the first Monday as the first day of week one) as a decimal number (01–53). The method for determining the week number is as specified by ISO 8601. (To wit: if the week containing January 1 has four or more days in the new year, then it is week one; otherwise it is the last week [52 or 53] of the previous year and the next week is week one.) 17
%w The weekday as a decimal number (0–6). Sunday is day zero. 4
%W The week number of the year (with the first Monday as the first day of week one) as a decimal number (00–53). 17
%x The locale’s “appropriate” date representation.
(This is ‘%A %B %d %Y’ in the "C" locale.)
04/26/2018
%X The locale’s “appropriate” time representation.
(This is ‘%T’ in the "C" locale.)
09:44:32 AM
%y The year modulo 100 as a decimal number (00–99). 18
%Y The full year as a decimal number (e.g., 2015). 2018
%z The time zone offset in a ‘+HHMM’ format
(e.g., the format necessary to produce RFC 822/RFC 1036 date headers).
+0900
%Z The time zone name or abbreviation; no characters if no time zone is determinable. KST
%% A literal ‘%’. %

For systems that are not yet fully standards-compliant, gawk supplies a copy of strftime() from the GNU C Library. It supports all of the just-listed format specifications. If that version is used to compile gawk (see Installation), then the following additional format specifications are available:

Spc Description Output
%k The hour (24-hour clock) as a decimal number (0–23).
Single-digit numbers are padded with a space.
(space)7
%l The hour (12-hour clock) as a decimal number (1–12).
Single-digit numbers are padded with a space.
(space)8
%s The time as a decimal timestamp in seconds since the epoch. 1524704904