AWK – Time Functions

  • Post author:
  • Post category:AWK
  • Post comments:1 Comment
AWK - Time Functions

This topic is about AWK – Time Functions.

AWK has the following built-in time functions −

systime

This function returns the current time of the day as the number of seconds since the Epoch (1970-01-01 00:00:00 UTC on POSIX systems).

Example

[jerry]$ awk 'BEGIN {
   print "Number of seconds since the Epoch = " systime()
}'

On executing this code, you get the following result −

Output

Number of seconds since the Epoch = 1418574432

mktime(datespec)

This function converts datespec string into a timestamp of the same form as returned by systime(). The datespec is a string of the form YYYY MM DD HH MM SS.

Example

[jerry]$ awk 'BEGIN {
   print "Number of seconds since the Epoch = " mktime("2014 12 14 30 20 10")
}'

On executing this code, you get the following result −

Output

Number of seconds since the Epoch = 1418604610

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

This function formats timestamps according to the specification in format.

Example

[jerry]$ awk 'BEGIN {
   print strftime("Time = %m/%d/%Y %H:%M:%S", systime())
}'

On executing this code, you get the following result −

Output

Time = 12/14/2014 22:08:42

The following time formats are supported by AWK −

S.No.Date format specification & Description
1%aThe locale’s abbreviated weekday name.
2%AThe locale’s full weekday name.
3%bThe locale’s abbreviated month name.
4%BThe locale’s full month name.
5%cThe locale’s appropriate date and time representation. (This is %A %B %d %T %Y in the C locale.)
6%CThe century part of the current year. This is the year divided by 100 and truncated to the next lower integer.
7%dThe day of the month as a decimal number (01–31).
8%DEquivalent to specifying %m/%d/%y.
9%eThe day of the month, padded with a space if it is only one digit.
10%FEquivalent to specifying %Y-%m-%d. This is the ISO 8601 date format.
11%gThe year modulo 100 of the ISO 8601 week number, as a decimal number (00–99). For example, January 1, 1993 is in week 53 of 1992. Thus, the year of its ISO 8601 week number is 1992, even though its year is 1993. Similarly, December 31, 1973 is in week 1 of 1974. Thus, the year of its ISO week number is 1974, even though its year is 1973.
12%GThe full year of the ISO week number, as a decimal number.
13%hEquivalent to %b.
14%HThe hour (24-hour clock) as a decimal number (00–23).
15%IThe hour (12-hour clock) as a decimal number (01–12).
16%jThe day of the year as a decimal number (001–366).
17%mThe month as a decimal number (01–12).
18%MThe minute as a decimal number (00–59).
19%nA newline character (ASCII LF).
20%pThe locale’s equivalent of the AM/PM designations associated with a 12-hour clock.
21%rThe locale’s 12-hour clock time. (This is %I:%M:%S %p in the C locale.)
22%REquivalent to specifying %H:%M.
23%SThe second as a decimal number (00–60).
24%tA TAB character.
25%TEquivalent to specifying %H:%M:%S.
26%uThe weekday as a decimal number (1–7). Monday is day one.
27%UThe week number of the year (the first Sunday as the first day of week one) as a decimal number (00–53).
28%VThe week number of the year (the first Monday as the first day of week one) as a decimal number (01–53).
29%wThe weekday as a decimal number (0–6). Sunday is day zero.
30%WThe week number of the year (the first Monday as the first day of week one) as a decimal number (00–53).
31%xThe locale’s appropriate date representation. (This is %A %B %d %Y in the C locale.).
32%XThe locale’s appropriate time representation. (This is %T in the C locale.).
33%yThe year modulo 100 as a decimal number (00–99).
34%YThe full year as a decimal number (e.g. 2011).
35%zThe time-zone offset in a +HHMM format (e.g., the format necessary to produce RFC 822/RFC 1036 date headers).
36%ZThe time zone name or abbreviation; no characters if no time zone is determinable.

In this topic we learn about AWK – Time Functions. To know more, Click Here.

This Post Has One Comment

Leave a Reply