A common use for awk
programs is the processing of log files
containing time stamp information, indicating when a
particular log record was written. Many programs log their time stamp
in the form returned by the time
system call, which is the
number of seconds since a particular epoch. On POSIX systems,
it is the number of seconds since Midnight, January 1, 1970, UTC.
In order to make it easier to process such log files, and to produce
useful reports, gawk
provides two functions for working with time
stamps. Both of these are gawk
extensions; they are not specified
in the POSIX standard, nor are they in any other known version
of awk
.
Optional parameters are enclosed in square brackets ("[" and "]").
systime()
strftime([format [, timestamp]])
systime
function. If no timestamp argument is supplied,
gawk
will use the current time of day as the time stamp.
If no format argument is supplied, strftime
uses
"%a %b %d %H:%M:%S %Z %Y"
. This format string produces
output (almost) equivalent to that of the date
utility.
(Versions of gawk
prior to 3.0 require the format argument.)
The systime
function allows you to compare a time stamp from a
log file with the current time of day. In particular, it is easy to
determine how long ago a particular record was logged. It also allows
you to produce log records using the "seconds since the epoch" format.
The strftime
function allows you to easily turn a time stamp
into human-readable information. It is similar in nature to the sprintf
function
(see section Built-in Functions for String Manipulation),
in that it copies non-format specification characters verbatim to the
returned string, while substituting date and time values for format
specifications in the format string.
strftime
is guaranteed by the ANSI C standard to support
the following date format specifications:
%a
%A
%b
%B
%c
%d
%H
%I
%j
%m
%M
%p
%S
%U
%w
%W
%x
%X
%y
%Y
%Z
%%
If a conversion specifier is not one of the above, the behavior is undefined.(15)
Informally, a locale is the geographic place in which a program
is meant to run. For example, a common way to abbreviate the date
September 4, 1991 in the United States would be "9/4/91".
In many countries in Europe, however, it would be abbreviated "4.9.91".
Thus, the `%x' specification in a "US"
locale might produce
`9/4/91', while in a "EUROPE"
locale, it might produce
`4.9.91'. The ANSI C standard defines a default "C"
locale, which is an environment that is typical of what most C programmers
are used to.
A public-domain C version of strftime
is supplied with gawk
for systems that are not yet fully ANSI-compliant. If that version is
used to compile gawk
(see section Installing gawk
),
then the following additional format specifications are available:
%D
%e
%h
%n
%r
%R
%T
%t
%k
%l
%C
%u
%V
%G
%g
%Ec %EC %Ex %Ey %EY %Od %Oe %OH %OI
%Om %OM %OS %Ou %OU %OV %Ow %OW %Oy
date
utility.)
%v
%z
This example is an awk
implementation of the POSIX
date
utility. Normally, the date
utility prints the
current date and time of day in a well known format. However, if you
provide an argument to it that begins with a `+', date
will copy non-format specifier characters to the standard output, and
will interpret the current time according to the format specifiers in
the string. For example:
$ date '+Today is %A, %B %d, %Y.' -| Today is Thursday, July 11, 1991.
Here is the gawk
version of the date
utility.
It has a shell "wrapper", to handle the `-u' option,
which requires that date
run as if the time zone
was set to UTC.
#! /bin/sh # # date --- approximate the P1003.2 'date' command case $1 in -u) TZ=GMT0 # use UTC export TZ shift ;; esac gawk 'BEGIN { format = "%a %b %d %H:%M:%S %Z %Y" exitval = 0 if (ARGC > 2) exitval = 1 else if (ARGC == 2) { format = ARGV[1] if (format ~ /^\+/) format = substr(format, 2) # remove leading + } print strftime(format) exit exitval }' "$@"
Go to the first, previous, next, last section, table of contents.