{"id":3402,"date":"2014-08-01T18:10:35","date_gmt":"2014-08-01T10:10:35","guid":{"rendered":"http:\/\/rmohan.com\/?p=3402"},"modified":"2014-08-01T18:11:13","modified_gmt":"2014-08-01T10:11:13","slug":"date-and-time-calculation-functions-using-awk","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3402","title":{"rendered":"Date and time calculation functions using AWK"},"content":{"rendered":"<p>awk has 3 functions to calculate date and time:<br \/>\nsystime<br \/>\nstrftime<br \/>\nmktime<br \/>\n   Let us see in this article how to use these functions:<\/p>\n<p>systime:<br \/>\n   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).<br \/>\n$ echo | awk &#8216;{print systime();}&#8217;<br \/>\n1358146640<br \/>\nNote: systime function does not take any arguments.<\/p>\n<p>strftime:<br \/>\n   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.<\/p>\n<p>Syntax:<br \/>\n   strftime (<format specifiers>,unix time);<br \/>\n1. Printing current date time using strftime:<br \/>\n$ echo | awk &#8216;{print strftime(&#8220;%d-%m-%y %H-%M-%S&#8221;,systime());}&#8217;<br \/>\n14-01-13 12-37-45<br \/>\n   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.<\/p>\n<p>2. Display current date time using strftime without systime:<br \/>\n$ echo | awk &#8216;{print strftime(&#8220;%d-%m-%y %H-%M-%S&#8221;);}&#8217;<br \/>\n14-01-13 12-38-08<br \/>\n   Both the arguments of strftime are optional. When the timestamp is not provided, it takes the systime by default.<\/p>\n<p>3. strftime with no arguments:<br \/>\n$ echo | awk &#8216;{print strftime();}&#8217;<br \/>\nMon Jan 14 12:30:05 IST 2013<br \/>\n  strftime without the format specifiers provides the output in the default output format as the Unix date command.<\/p>\n<p>mktime:<br \/>\n    mktime function converts any given date time string into a Unix time, which is of the systime format.<br \/>\nSyntax:<br \/>\n  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<\/p>\n<p>1. Printing timestamp for a specific date time :<br \/>\n$ echo | awk &#8216;{print mktime(&#8220;2012 12 21 0 0 0&#8221;);}&#8217;<br \/>\n1356028200<br \/>\n     This gives the Unix time for the date 21-Dec-12.<\/p>\n<p>2.  Using strftime with mktime:<br \/>\n$ echo | awk &#8216;{print strftime(&#8220;%d-%m-%Y&#8221;,mktime(&#8220;2012 12 21 0 0 0&#8221;));}&#8217;<br \/>\n21-12-2012<br \/>\n   The output of mktime can be validated by formatting the mktime output using the strftime function as above.<\/p>\n<p>3. Negative date in mktime:<br \/>\n$ echo | awk &#8216;{print strftime(&#8220;%d-%m-%Y&#8221;,mktime(&#8220;2012 12 -1 0 0 0&#8221;));}&#8217;<br \/>\n29-11-2012<br \/>\n  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.<\/p>\n<p>4. Negative hour value in mktime:<br \/>\n$ echo | awk &#8216;{print strftime(&#8220;%d-%m-%Y %H-%M-%S&#8221;,mktime(&#8220;2012 12 3 -2 0 0&#8221;));}&#8217;<br \/>\n02-12-2012 22-00-00<br \/>\n    -2 in the hours position indicates 2 hours before the specified date time which in this case leads to &#8220;2-Dec-2012 22&#8221; hours. <\/p>\n<p>$ cat tst.awk<br \/>\nBEGIN {<br \/>\n    ARGV[ARGC++] = ARGV[ARGC-1]<\/p>\n<p>    mths = &#8220;JanFebMarAprMayJunJulAugSepOctNovDec&#8221;<\/p>\n<p>    if (days)  { hours = days * 24  }<br \/>\n    if (hours) { mins  = hours * 60 }<br \/>\n    if (mins)  { secs  = mins * 60  }<br \/>\n    deltaSecs = secs<br \/>\n}<\/p>\n<p>NR==FNR {<br \/>\n    nr2secs[NR] = mktime($6&#8243; &#8220;(match(mths,$5)+2)\/3&#8221; &#8220;$4&#8221; &#8220;gensub(\/:\/,&#8221; &#8220;,&#8221;g&#8221;,$7))<br \/>\n    next<br \/>\n}<\/p>\n<p>nr2secs[FNR] >= (nr2secs[NR-FNR] &#8211; deltaSecs)<\/p>\n<p>$ awk -v hours=1 -f tst.awk file<br \/>\n157.55.34.99 &#8211; &#8211;  06 Sep 2013 09:13:10 +0300  &#8220;GET \/index.php HTTP\/1.1&#8221; 200 16977 &#8220;-&#8221; &#8220;Mozilla\/5.0 (compatible; bingbot\/2.0; +http:\/\/www.bing.com\/bingbot.htm)&#8221;<br \/>\n85.163.134.149 &#8211; &#8211;  06 Sep 2013 09:50:23 +0300  &#8220;GET \/wap\/wapicons\/mnrwap.jpg HTTP\/1.1&#8221; 200 1217 &#8220;http:\/\/mydomain.com\/main.php&#8221; &#8220;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&#8221;<br \/>\n83.113.48.218 &#8211; &#8211;  06 Sep 2013 10:13:07 +0300  &#8220;GET \/english\/nicons\/word.gif HTTP\/1.1&#8221; 200 803 &#8220;http:\/\/mydomain.com\/french\/details.php?eid=127928&#038;cid=18&#038;fromval=1&#038;frid=18&#8221; &#8220;Mozilla\/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident\/5.0)&#8221;<\/p>\n<p>$ gawk -v mins=60 -f tst.awk file<br \/>\n157.55.34.99 &#8211; &#8211;  06 Sep 2013 09:13:10 +0300  &#8220;GET \/index.php HTTP\/1.1&#8221; 200 16977 &#8220;-&#8221; &#8220;Mozilla\/5.0 (compatible; bingbot\/2.0; +http:\/\/www.bing.com\/bingbot.htm)&#8221;<br \/>\n85.163.134.149 &#8211; &#8211;  06 Sep 2013 09:50:23 +0300  &#8220;GET \/wap\/wapicons\/mnrwap.jpg HTTP\/1.1&#8221; 200 1217 &#8220;http:\/\/mydomain.com\/main.php&#8221; &#8220;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&#8221;<br \/>\n83.113.48.218 &#8211; &#8211;  06 Sep 2013 10:13:07 +0300  &#8220;GET \/english\/nicons\/word.gif HTTP\/1.1&#8221; 200 803 &#8220;http:\/\/mydomain.com\/french\/details.php?eid=127928&#038;cid=18&#038;fromval=1&#038;frid=18&#8221; &#8220;Mozilla\/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident\/5.0)&#8221;<\/p>\n<p>$ gawk -v mins=20 -f tst.awk file<br \/>\n83.113.48.218 &#8211; &#8211;  06 Sep 2013 10:13:07 +0300  &#8220;GET \/english\/nicons\/word.gif HTTP\/1.1&#8221; 200 803 &#8220;http:\/\/mydomain.com\/french\/details.php?eid=127928&#038;cid=18&#038;fromval=1&#038;frid=18&#8221; &#8220;Mozilla\/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident\/5.0)&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>awk has 3 functions to calculate date and time: systime strftime mktime Let us see in this article how to use these functions:<\/p>\n<p>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 &#8216;{print systime();}&#8217; [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3402"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3402"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3402\/revisions"}],"predecessor-version":[{"id":3404,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3402\/revisions\/3404"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}