# This AWK program implements the UNIX date program. # Syntax: # date [-u] [ +format ] # Description: # Date prints out the date in a specified format. # If no format is specified as an option, the date is printed # in the format: Fri Jan 19 10:09:42 PST 1996 # Options: # -u # (Unimplemented.) Specifies date is in GMT rather than local time. # +format # The +format specifies the format to use to print the date. # The format string can contain regular characters, which are printed, # and field descriptors, which begin with a % character, and are # substituted for the specific parts of the date as described below. # Note that the format string must appear inside quotes if it contains spaces. # The default format is equivalent to: "%c", which is also # equivalent to: "%a %b %e %T %Z %Y" # Valid format field descriptors are as follows: # %a weekday name abbreviation # %A weekday name # %b month name abbreviation # %B month name # %c same as: %a %b %e %T %Z %Y # %C First two digits of year (eg: 19) # %d day of month (01 to 31) # %D date in format: %m/%d/%y # %e day of month (1 to 31) # %h same as: %b # %H hour (00 to 23) # %I hour (01 to 12) # %j day of year (001 to 366) # %m month of year (01 to 12) # %M minute (00 to 59) # %n insert a newline character # %p AM or PM # %r time in format: %I:%M:%S %p # %R time in format: %H:%M # %S second (00 to 59) # %t insert a tab character # %T same as: %H:%M:%S # %U week number in the year (00 - 52). Sunday is the first day # of the week. Days preceding the first Sunday are in week 0. # %V week number in the year (01 - 53). Uses POSIX method # to determine week number. # %w day of week (0 to 6, Sunday is day 0) # %W week number in the year, with Monday as first day of week (00 to 53) # %W week number in the year (00 - 52). Monday is the first day # of the week. Days preceding the first Monday are in week 0. # %x Date in country specific format. In this version, same as: %m %d %y # %X Time in country specific format. In this version, same as: %H:%M:%S # %y last two digits of year (00 to 99) # %Y year # %Z timezone name, or blank if timezone is unknown # %% a percent symbol # An optional E or O character can appear between the % and the field # descriptor, for example: %Ec. These are used for POSIX locale specific # date/time descriptors, and are ignored in this version. # Bugs: # This version of the date command can not set the date. # -u is unimplemented. local year # 0 to 2100 local month # 1 to 12 local day # 1 to 31 local hour # 0 to 23 (0 means between midnight and 1am) local minute # 0 to 59 local second # 0 to 59 local weekday # 1 to 7 (1 = Sunday, ..., 7 = Saturday) local yearday # 1 to 366 local daynames[], abdaynames[] local monthnames[], abmonthnames[] INIT { split("Sun Mon Tue Wed Thu Fri Sat",abdaynames) split("Sunday Monday Tuesday Wednesday Thursday Friday Saturday",daynames) split("January February March April May June July August September\ October November December",monthnames) split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",abmonthnames) } # Calculate week number of year. # If flag == "U", Sunday is first day of week, range 0 to 52. # If flag == "W", Monday is first day of week, range 0 to 52. # If flag == "V", Use posix wierd method: If the week containing jan 1 # has four or more days in the new year, then it is week 1; # otherwise it is week 53 of the previous year. function weeknumber(flag) { local n; if (flag == "U") return int((yearday + 5 - (weekday-1)) / 7); if (flag == "W") return int((yearday + 5 - ((weekday>1) ? (weekday-2):6)) / 7); n = int((yearday + 8 - (weekday>1 ? (weekday - 2) : 6)) / 7); return n ? n : 53; } # 3-95: This has been modified to include all the POSIX compatible formats. # Locales are not supported so locale-specific formats are the same # as regular formats. function strftime(fmt) { gsub("%%","\xff",fmt) # Change %% so we don't lose them # We do not support POSIX locales, so %Ex and %Ox are translated to %x. gsub("%E|%O","%",fmt) # The %c, %x, %X and %r are country-specific abbreviations for other formats: gsub("%c","%a %b %e %H:%M:%S %Z %Y",fmt) gsub("%x","%m %d %y",fmt) gsub("%X","%H:%M:%S",fmt) gsub("%r","%I:%M:%S %p",fmt) gsub("%D","%m/%d/%y",fmt) gsub("%R","%H:%M",fmt) gsub("%T","%H:%M:%S",fmt) # Now do the format substitutions gsub("%a",abdaynames[weekday],fmt) gsub("%A",daynames[weekday],fmt) gsub("%B",monthnames[month],fmt) gsub("%b",abmonthnames[month],fmt) gsub("%C",int(year/100),fmt) gsub("%d",sprintf("%02d",day),fmt) gsub("%e",sprintf("%2d",day),fmt) gsub("%h",abmonthnames[month],fmt) gsub("%H",sprintf("%02d",hour),fmt) gsub("%I",sprintf("%02d",hour % 12 == 0 ? 12 : hour % 12),fmt) gsub("%j",sprintf("%03d",yearday),fmt) # %k and %l are not in POSIX, but are SunOS compatible? gsub("%k",sprintf("%2d",hour),fmt) gsub("%l",sprintf("%2d",hour % 12 == 0 ? 12 : hour % 12),fmt) gsub("%m",sprintf("%02d",month),fmt) gsub("%M",sprintf("%02d",minute),fmt) gsub("%n","\n",fmt) gsub("%p",hour < 12 ? "AM" : "PM",fmt) gsub("%S",sprintf("%02d",second),fmt) gsub("%t","\t",fmt) # %u is day of week, but assuming 1=Mon, ..., 7=Sun gsub("%u",weekday == 1 ? 7 : weekday-1,fmt) gsub("%U",sprintf("%02d",weeknumber("U")),fmt) gsub("%V",sprintf("%02d",weeknumber("V")),fmt) gsub("%w",weekday-1,fmt) gsub("%W",sprintf("%02d",weeknumber("W")),fmt) gsub("%y",sprintf("%02d",year % 100),fmt) gsub("%Y",year,fmt) # %Z is time zone name, which is taken from the environment variable. # If the TZ environment variable is not set, print nothing. gsub("%Z",ENVIRON["TZ"],fmt) # Time Zone Name gsub("\xff","%",fmt) # Change %% removed above back to % return fmt } BEGIN { local argc local format = "%c" # Default output format # Get the current time timetab(tab) year = tab["YEAR"] month = tab["MONTH"] day = tab["DAY"] hour = tab["HOUR"] minute = tab["MIN"] second = tab["SEC"] weekday = tab["WEEKDAY"] yearday = tab["YEARDAY"] for (argc = 1; argc < ARGC; argc++) { if (ARGV[argc] == "-u") { print "date: -u not supported"; } else if (ARGV[argc] ~ /^\+/) { format = substr(ARGV[1],2); } else { print "date: unrecognized argument"; exit(2); } } print strftime(format); }