May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Date and time calculation functions using AWK

awk has 3 functions to calculate date and time:
systime
strftime
mktime
Let us see in this article how to use these functions:

systime:
This function is equivalent to the Unix date (date +%s) command. It gives the Unix time, total number of seconds elapsed since the epoch(01-01-1970 00:00:00).
$ echo | awk ‘{print systime();}’
1358146640
Note: systime function does not take any arguments.

strftime:
A very common function used in gawk to format the systime into a calendar format. Using this function, from the systime, the year, month, date, hours, mins and seconds can be separated.

Syntax:
strftime (,unix time);
1. Printing current date time using strftime:
$ echo | awk ‘{print strftime(“%d-%m-%y %H-%M-%S”,systime());}’
14-01-13 12-37-45
strftime takes format specifiers which are same as the format specifiers available with the date command. %d for date, %m for month number (1 to 12), %y for the 2 digit year number, %H for the hour in 24 hour format, %M for minutes and %S for seconds. In this way, strftime converts Unix time into a date string.

2. Display current date time using strftime without systime:
$ echo | awk ‘{print strftime(“%d-%m-%y %H-%M-%S”);}’
14-01-13 12-38-08
Both the arguments of strftime are optional. When the timestamp is not provided, it takes the systime by default.

3. strftime with no arguments:
$ echo | awk ‘{print strftime();}’
Mon Jan 14 12:30:05 IST 2013
strftime without the format specifiers provides the output in the default output format as the Unix date command.

mktime:
mktime function converts any given date time string into a Unix time, which is of the systime format.
Syntax:
mktime(date time string) # where date time string is a string which contains atleast 6 components in the following order: YYYY MM DD HH MM SS

1. Printing timestamp for a specific date time :
$ echo | awk ‘{print mktime(“2012 12 21 0 0 0”);}’
1356028200
This gives the Unix time for the date 21-Dec-12.

2. Using strftime with mktime:
$ echo | awk ‘{print strftime(“%d-%m-%Y”,mktime(“2012 12 21 0 0 0”));}’
21-12-2012
The output of mktime can be validated by formatting the mktime output using the strftime function as above.

3. Negative date in mktime:
$ echo | awk ‘{print strftime(“%d-%m-%Y”,mktime(“2012 12 -1 0 0 0”));}’
29-11-2012
mktime can take negative values as well. -1 in the date position indicates one day before the date specified which in this case leads to 29th Nov 2012.

4. Negative hour value in mktime:
$ echo | awk ‘{print strftime(“%d-%m-%Y %H-%M-%S”,mktime(“2012 12 3 -2 0 0”));}’
02-12-2012 22-00-00
-2 in the hours position indicates 2 hours before the specified date time which in this case leads to “2-Dec-2012 22” hours.

$ cat tst.awk
BEGIN {
ARGV[ARGC++] = ARGV[ARGC-1]

mths = “JanFebMarAprMayJunJulAugSepOctNovDec”

if (days) { hours = days * 24 }
if (hours) { mins = hours * 60 }
if (mins) { secs = mins * 60 }
deltaSecs = secs
}

NR==FNR {
nr2secs[NR] = mktime($6″ “(match(mths,$5)+2)/3” “$4” “gensub(/:/,” “,”g”,$7))
next
}

nr2secs[FNR] >= (nr2secs[NR-FNR] – deltaSecs)

$ awk -v hours=1 -f tst.awk file
157.55.34.99 – – 06 Sep 2013 09:13:10 +0300 “GET /index.php HTTP/1.1” 200 16977 “-” “Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)”
85.163.134.149 – – 06 Sep 2013 09:50:23 +0300 “GET /wap/wapicons/mnrwap.jpg HTTP/1.1” 200 1217 “http://mydomain.com/main.php” “Mozilla/5.0 (Linux; U; Android 4.1.2; en-gb; GT-I9082 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30”
83.113.48.218 – – 06 Sep 2013 10:13:07 +0300 “GET /english/nicons/word.gif HTTP/1.1” 200 803 “http://mydomain.com/french/details.php?eid=127928&cid=18&fromval=1&frid=18” “Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)”

$ gawk -v mins=60 -f tst.awk file
157.55.34.99 – – 06 Sep 2013 09:13:10 +0300 “GET /index.php HTTP/1.1” 200 16977 “-” “Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)”
85.163.134.149 – – 06 Sep 2013 09:50:23 +0300 “GET /wap/wapicons/mnrwap.jpg HTTP/1.1” 200 1217 “http://mydomain.com/main.php” “Mozilla/5.0 (Linux; U; Android 4.1.2; en-gb; GT-I9082 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30”
83.113.48.218 – – 06 Sep 2013 10:13:07 +0300 “GET /english/nicons/word.gif HTTP/1.1” 200 803 “http://mydomain.com/french/details.php?eid=127928&cid=18&fromval=1&frid=18” “Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)”

$ gawk -v mins=20 -f tst.awk file
83.113.48.218 – – 06 Sep 2013 10:13:07 +0300 “GET /english/nicons/word.gif HTTP/1.1” 200 803 “http://mydomain.com/french/details.php?eid=127928&cid=18&fromval=1&frid=18” “Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)”

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>