AWK Scripts: Date and Time

This page is about scripts for the GAWK language, which you must obtain in order to use the scripts - see Multilingual Batch Programs for background information.

This example of a GAWK specific script is a general solution to the class of FAQs dealing with getting and reformatting the date and time. I have addressed this issue for Real DOS and others have produced some truly opaque code to work under Win9x - NT provides actually useful tools in its extensions to FOR that make the process somewhat less than torture, but there really hasn't been a workable universal solution presented in pure batch, not is it likely that anyone ever will - there are just too many variables in the batch and human language versions, and in the available date and time information from the DATE and TIME commands, and from date and time stamps on files. This example avoids all of those issues by using the time functions built into GAWK, especially the strftime() function, which is straight out of ANSI C. There are numerous formatting arguments to strftime(), but for nearly all the FAQs dealing with the current date and time, only a few are needed: In addition to the date arguments, there are also some for day and month names: and some others of less interest The "local appropriate" stuff depends on the TZ environment variable being set correctly - this is something of an art.

The general form of a strftime() statement is
 DateString = strftime( format_string, time)
where DateString is the output string, format_string is a string made up of formatting flags (above) mixed with text and/or delimiters, and time is the time to process in the format returned by the systime() function.

From this, it is an easy couple of steps to DATETIME.AWK
BEGIN{
    Format = "%Y%m%d%H%M"
    print strftime( Format, systime() )
}
to display the current date and time in YYYYMMDDhhmm format. As a one shot batch program that becomes GSPC0010.BAT (DATETIME.BAT)
 @gawk "BEGIN{Format = \"%Y%m%d%H%M\";print strftime( Format, systime() )}"
Note that unlike MAWK, GAWK accepts short programs on the command line.

Since the strftime() format string is now on the command line, it can even be made an environment variable, and since it can be extended text, we can do something like:
 @echo off
 set format=set datetime=%%Y%%m%%d%%H%%M
 gawk "BEGIN{Format = \"%format%\";print strftime( Format, systime() )}" > }{.bat
 call }{
 del }{.bat
 set format=
to put the formatted date and time into an environment variable, where it can be used to rename files, make directories, and numerous other tasks. That is NT specific because of the '=' in the argument to the SET command: you can't use '=' inside a SET command in any other of the common Microsoft operating systems, and while it is trivial to work around this simply by putting the format string in the GAWK argument as a string literal, there are many cases where it would be useful to define the format once for use many times.

Obviously, we can put only the variable part of the format string in the environment and put the problem part in the command:
 @echo off
 set format=%%Y%%m%%d%%H%%M
 gawk "BEGIN{Format = \"set datetime=%format%\";print strftime( Format, systime() )}" > }{.bat
 call }{
 del }{.bat
 set format=
and gain the advantage that the format string can be used for things other than just setting one specific environment variable. However, this does not solve all the possible problems found in variations of this program. Rather than try to identify all of them and treat them individually, I will take this as a good point to introduce the use of escaped octal character numbers in string - such as format strings - that are treated as string literals by both batch language and the AWK statement parser. This may be a bit forced here, but the technique is valuable and I want to introduce it early in this work.

From Character Codes in Decimal, Hex, and Octal we see that the octal code for '=' is 075 - by putting a backslash in front of the octal number (\075), we make AWK interpreted it as the character represented by the number, rather than the string literal "075". Note that since the backslash ('\') is the escape character, it must itself be escaped where it is to be taken as a literal backslash by putting a backslash in front of it ("\\"). This is critical when passing filespecs as strings: "c:\foo" will cause either a syntax error (if the first letter of foo is not one of the characters that when escaped means something (\t means TAB and \n means a newline mark, for examples)) or will cause the string to be improperly interpreted if the character is one of those special characters, or if the first three characters following the '\' are numerals in the octal range 0-7. See the language documentation for more information on these special characters. Normally only the characters that are magic somewhere in batch language will need such treatment - the most common are which are the redirection characters '>', '<', and '|', command argument delimiters ',', ';', and '=', and the variable marker '%'. '^' (\136) and '&' (\046) are magic in NT4, but not in Real DOS or Win9x.

So, to make the same batch program work in all the common environments, we must either incorporate at least the text part of the format string directly in the AWK (GAWK) argument, or escape any problem codes in the environment variable. Notice that I have already escaped the '%' characters in the SET FORMAT= command for batch language by doubling them. There is no reason why they can't be escaped for AWK instead by using
 @echo off
 set format=set datetime\075\045Y\045m\045d\045H\045M
 gawk "BEGIN{Format = \"%format%\";print strftime( Format, systime() )}" > }{.bat
 call }{
 del }{.bat
 set format=
Because that is about as bullet proof as I can make it, I will use that code as the general pattern for all code to set an environment variable to the date and/or time in whatever format is needed at the moment.

This code provides the basis for answers to a number of related FAQs (Frequently Asked Questions), most of which - whether on usenet or in my e-mail - contain not a single clue to what operating system the answer is wanted for, and therefore cannot be reasonably answered with pure batch code.

Date and Time FAQs






This stuff has been only partially tested at the time of its initial release, but it is known that the versions of GAWK and MAWK used here do work in Real DOS, Win9x, and NT4. The complete programs have not all been tested under Real DOS.



  ** Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001 Ted Davis - see License, included by reference. ** 

Input and feedback from readers are welcome. NOTE: the subject of the message must contain the word "batch" for the message to get past the spam filter.

Back to the Table of Contents page

Back to my personal links page - back to my home page