#!/usr/local/bin/gawk -f # gawk version of shift_date ksh script. # Note: # In testing I have noticed that shift_date.awk 20000327 -27 gives 20000228, while # shift_date.awk 20000326 -26 gives 20000229 (Mac OS 10.2/Darwin 6.8/GNU Awk 3.1.3) # As far as I can tell mktime() is wrong. Need to test on other systems. # In Oracle SQL: # SELECT (DATE '2000-03-27' - DATE '1970-01-01') * 86400 ts FROM dual # gives 954115200, not 954111600 # SELECT DATE '1970-01-01' + (954111600 / 86400) ts FROM dual # gives 26-MAR-2000. # William Robertson 2004 BEGIN { DEBUG = ENVIRON["DEBUG"] } function debug (message) { if ( DEBUG == "TRUE" ) { print message } } BEGIN { debug("Debugging messages enabled. Set environment variable DEBUG=FALSE to disable.") in_date = ARGV[1] shift_days = ARGV[2] year = int(substr(in_date,1,4)) month = int(substr(in_date,5,2)) day = int(substr(in_date,7,2)) # Arrange date/time info into a format acceptable to mktime(): datetimestring = sprintf("%04i %02i %02i 00 00 00", year, month, day) debug("Old datetimestring = " datetimestring) # Convert year month day etc string into a timestamp: timestamp = mktime(datetimestring) debug("Old timestamp = " timestamp) # Check conversion was successful: if (timestamp < 1) { print -u2 "Error: Could not understand date", datetimestring exit 1 } # Add n days (timestamp is in seconds): timestamp += shift_days * 86400 debug("New timestamp = " timestamp) # Convert back from timestamp into year month day etc: datetimestring = strftime("%Y %m %d", timestamp) debug("New datetimestring = " datetimestring) # Format datetime elements: split(datetimestring, datetimeelements) year = datetimeelements[1] month = datetimeelements[2] day = datetimeelements[3] print year month day }