gawk: Getlocaltime Function

1 
1 10.2.7 Managing the Time of Day
1 -------------------------------
1 
1 Functions:: provide the minimum functionality necessary for dealing with
1 the time of day in human-readable form.  Although 'strftime()' is
1 extensive, the control formats are not necessarily easy to remember or
1 intuitively obvious when reading a program.
1 
1    The following function, 'getlocaltime()', populates a user-supplied
1 array with preformatted time information.  It returns a string with the
1 current time formatted in the same way as the 'date' utility:
1 
1      # getlocaltime.awk --- get the time of day in a usable format
1 
1      # Returns a string in the format of output of date(1)
1      # Populates the array argument time with individual values:
1      #    time["second"]       -- seconds (0 - 59)
1      #    time["minute"]       -- minutes (0 - 59)
1      #    time["hour"]         -- hours (0 - 23)
1      #    time["althour"]      -- hours (0 - 12)
1      #    time["monthday"]     -- day of month (1 - 31)
1      #    time["month"]        -- month of year (1 - 12)
1      #    time["monthname"]    -- name of the month
1      #    time["shortmonth"]   -- short name of the month
1      #    time["year"]         -- year modulo 100 (0 - 99)
1      #    time["fullyear"]     -- full year
1      #    time["weekday"]      -- day of week (Sunday = 0)
1      #    time["altweekday"]   -- day of week (Monday = 0)
1      #    time["dayname"]      -- name of weekday
1      #    time["shortdayname"] -- short name of weekday
1      #    time["yearday"]      -- day of year (0 - 365)
1      #    time["timezone"]     -- abbreviation of timezone name
1      #    time["ampm"]         -- AM or PM designation
1      #    time["weeknum"]      -- week number, Sunday first day
1      #    time["altweeknum"]   -- week number, Monday first day
1 
1      function getlocaltime(time,    ret, now, i)
1      {
1          # get time once, avoids unnecessary system calls
1          now = systime()
1 
1          # return date(1)-style output
1          ret = strftime("%a %b %e %H:%M:%S %Z %Y", now)
1 
1          # clear out target array
1          delete time
1 
1          # fill in values, force numeric values to be
1          # numeric by adding 0
1          time["second"]       = strftime("%S", now) + 0
1          time["minute"]       = strftime("%M", now) + 0
1          time["hour"]         = strftime("%H", now) + 0
1          time["althour"]      = strftime("%I", now) + 0
1          time["monthday"]     = strftime("%d", now) + 0
1          time["month"]        = strftime("%m", now) + 0
1          time["monthname"]    = strftime("%B", now)
1          time["shortmonth"]   = strftime("%b", now)
1          time["year"]         = strftime("%y", now) + 0
1          time["fullyear"]     = strftime("%Y", now) + 0
1          time["weekday"]      = strftime("%w", now) + 0
1          time["altweekday"]   = strftime("%u", now) + 0
1          time["dayname"]      = strftime("%A", now)
1          time["shortdayname"] = strftime("%a", now)
1          time["yearday"]      = strftime("%j", now) + 0
1          time["timezone"]     = strftime("%Z", now)
1          time["ampm"]         = strftime("%p", now)
1          time["weeknum"]      = strftime("%U", now) + 0
1          time["altweeknum"]   = strftime("%W", now) + 0
1 
1          return ret
1      }
1 
1    The string indices are easier to use and read than the various
1 formats required by 'strftime()'.  The 'alarm' program presented in
1 ⇒Alarm Program uses this function.  A more general design for the
1 'getlocaltime()' function would have allowed the user to supply an
1 optional timestamp value to use instead of the current time.
1