#!/bin/sh awk -v timestamp=$1 ' # awkdate.sh - print UTC unix timestamps, like so # $ sh awkdate.sh `date +"%s"` # May 17 19:39:31 UTC 2009 # this program does not print dates before 1970, it will error. function isleap(year) { if (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0) return 1; return 0; } function getyear(timestamp, year, is_leap) { year = 1970; while (timestamp >= 0) { is_leap = isleap(year); if (is_leap) timestamp -= (366 * 86400); else timestamp -= (365 * 86400); year++; } if (is_leap) ts = timestamp + (366 * 86400); else ts = timestamp + (365 * 86400); year--; return year; } function getmonth(timestamp, year, is_leap, m) { is_leap = isleap(year); m[1] = 31; if (is_leap) m[2] = 29; else m[2] = 28; m[3] = 31; m[4] = 30; m[5] = 31; m[6] = 30; m[7] = 31; m[8] = 31; m[9] = 30; m[10] = 31; m[11] = 30; m[12] = 31; for (i = 1; i < 13; i++) { if ((timestamp - (m[i] * 86400)) < 0) break; timestamp -= (m[i] * 86400); } ts = timestamp; return i; } function convmonth(month, mo) { mo[1] = "Jan"; mo[2] = "Feb"; mo[3] = "Mar"; mo[4] = "Apr"; mo[5] = "May"; mo[6] = "Jun"; mo[7] = "Jul"; mo[8] = "Aug"; mo[9] = "Sep"; mo[10] = "Oct"; mo[11] = "Nov"; mo[12] = "Dec"; return mo[month]; } function getdays(timestamp, days) { days = 1; while ((timestamp -= 86400) >= 0) days++; timestamp += 86400; ts = timestamp; return days; } function gethour(timestamp, hour) { hour = 0; while (timestamp >= 0) { timestamp -= 3600; hour++; } hour--; ts = timestamp + 3600; return hour; } function getminute(timestamp, minute) { minute = 0; while (timestamp >= 0) { timestamp -= 60; minute++; } minute--; ts = timestamp + 60; return minute; } function getdate(timestamp, year, month, day, hour, minute, buf) { if (timestamp > 2147483647 || timestamp < 0) { printf("I dont print time before 1970\n"); exit(1); } ts = 0; year = getyear(timestamp); timestamp = ts; month = getmonth(timestamp, year); timestamp = ts; day = getdays(timestamp); timestamp = ts; hour = gethour(timestamp); timestamp = ts; minute = getminute(timestamp); timestamp = ts; buf = sprintf("%s %s %s:%02s:%02s UTC %s", convmonth(month), day , hour, minute, timestamp, year); return buf; } BEGIN { printf("%s\n", getdate(timestamp)); }'