AWK - Time Functions

Complete Python Prime Pack for 2023

9 Courses     2 eBooks

Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

Java Prime Pack 2023

8 Courses     2 eBooks

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

%a

The locale’s abbreviated weekday name.

2

%A

The locale’s full weekday name.

3

%b

The locale’s abbreviated month name.

4

%B

The locale’s full month name.

5

%c

The locale’s appropriate date and time representation. (This is %A %B %d %T %Y in the C locale.)

6

%C

The century part of the current year. This is the year divided by 100 and truncated to the next lower integer.

7

%d

The day of the month as a decimal number (01–31).

8

%D

Equivalent to specifying %m/%d/%y.

9

%e

The day of the month, padded with a space if it is only one digit.

10

%F

Equivalent to specifying %Y-%m-%d. This is the ISO 8601 date format.

11

%g

The 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

%G

The full year of the ISO week number, as a decimal number.

13

%h

Equivalent to %b.

14

%H

The hour (24-hour clock) as a decimal number (00–23).

15

%I

The hour (12-hour clock) as a decimal number (01–12).

16

%j

The day of the year as a decimal number (001–366).

17

%m

The month as a decimal number (01–12).

18

%M

The minute as a decimal number (00–59).

19

%n

A newline character (ASCII LF).

20

%p

The locale’s equivalent of the AM/PM designations associated with a 12-hour clock.

21

%r

The locale’s 12-hour clock time. (This is %I:%M:%S %p in the C locale.)

22

%R

Equivalent to specifying %H:%M.

23

%S

The second as a decimal number (00–60).

24

%t

A TAB character.

25

%T

Equivalent to specifying %H:%M:%S.

26

%u

The weekday as a decimal number (1–7). Monday is day one.

27

%U

The week number of the year (the first Sunday as the first day of week one) as a decimal number (00–53).

28

%V

The week number of the year (the first Monday as the first day of week one) as a decimal number (01–53).

29

%w

The weekday as a decimal number (0–6). Sunday is day zero.

30

%W

The week number of the year (the first Monday as the first day of week one) as a decimal number (00–53).

31

%x

The locale’s appropriate date representation. (This is %A %B %d %Y in the C locale.).

32

%X

The locale’s appropriate time representation. (This is %T in the C locale.).

33

%y

The year modulo 100 as a decimal number (00–99).

34

%Y

The full year as a decimal number (e.g. 2011).

35

%z

The time-zone offset in a +HHMM format (e.g., the format necessary to produce RFC 822/RFC 1036 date headers).

36

%Z

The time zone name or abbreviation; no characters if no time zone is determinable.

awk_built_in_functions.htm