Skip to main content

Date & Time

systime, strftime and mktime follow gawk, with a much more forgiving parser.

Current time and formatting​

systime() # Unix time
strftime("%Y-%m-%d %H:%M:%S") # now, local time
strftime("%Y-%m-%d", 0) # a timestamp
strftime("%H:%M", 0, 1) # in UTC (3rd argument)
strftime("%+") # ISO 8601 / RFC 3339

Format directives are those of chrono.

Parsing​

mktime accepts gawk's "YYYY MM DD HH MM SS" and many common formats:

mktime("2012 12 21 0 0 0")
mktime("2019-11-29 08:08-08")
mktime("2024-04-27 17:07:25.684184848 +08:00")
mktime("1970 01 02 00 00 00", 1) # 86400: parse as UTC
mktime("2024-01-01 10:00:00", 0, 8) # 1704074400: UTC offset in hours

It returns -1 if the text cannot be parsed, as gawk does.

datetime() breaks a time into parts: year, month, monthday, hour, minute, second, yearday, weekday, althour.

datetime()["year"] # now
datetime(1621530000)["month"]
datetime("2020-02-02")["weekday"]

Durations​

duration("2min + 12sec") # 132 seconds
duration("1 day") # 86400

Units: sec, min, hour/h, day/d, week/wk, month/mo, year/yr.

Example: orders per day​

zawk -i csv -H '{ t = mktime($FI["created_at"]); c[strftime("%Y-%m-%d", t, 1)]++ }
END { PROCINFO["sorted_in"] = "@ind_str_asc"; for (d, n in c) print d, n }' orders.csv