Technical article 105525

How can I change the content of the fields output by the date(C) command?

OpenServer 5.0.0OpenServer 5.0.2

Written · Last revised

Problem

If I run the "date(C)" command on my system, it presents the output in the following format:

10:16:47 AM Wed 02 Apr 1997 BST

I do not want it to print the "AM" or "PM" designation after showing the time in 24 hour notation. For example, the following would be preferable:

Wed Apr  2 10:15:35 BST 1997
How can I change the default presentation format?

Solution

The date(C) command allows the user to set the time and date and also print out that time and date for a system. The representation can be printed in differing formats using the "+format" options. However, it is not possible to use the date(C) command to actually change the format of the individual fields that make up the overall output from the command, that is, how the time field within the date output is represented to the user by the system.

For example, on a sample system, the command "date" shows:

10:16:47 AM Wed 02 Apr 1997 BST

The individual settings that go to make up this string can be examined using the "+format" option:

#date +%X
10:31:59 AM
or
#date +%X%a%b%d
10:33:58 AMWedApr02

The format of the output from the date(C) command is governed by the particular locale setting of the system. This is set within the file "/etc/default/lang". This contains, for example:

...
...
#       default/lang - system locales: locale(M)
#
LANG=english_uk.8859
LANG=C_C.C
LANG=english_us.ascii
LANG=en_US.ascii
LANG=C_C.C
LANG=german_germany.8859
LANG=french_france.8859
...
...

The current locales of the system may be inspected using the locale(C) command. For example:

#locale
LANG=english_uk.8859
LC_CTYPE="english_uk.8859"
LC_COLLATE="english_uk.8859"
LC_TIME="english_uk.8859"
LC_NUMERIC="english_uk.8859"
LC_MONETARY="english_uk.8859"
LC_MESSAGES="english_uk.8859"
LC_ALL=

The locale database for the system is contained in the "/usr/lib/lang .src" directory. The definition of how the date is represented for the "english_uk.8859" locale is set in the file "tim_uk.src". This contains:

...
#       timtbl specification file for english.uk
#
DATE_FMT="%d/%m/%y"           # 22/04/87
TIME_FMT="%I:%M:%S %p"        # 02:43:40 PM
D_T_FMT="%X %a %d %b %Y %Z"   # 02:43:40 PM Wed 22 Apr 1987 BST
F_NOON="AM"
A_NOON="PM"
ABDAY_1="Sun"
...

The date format D_T_FMT is specified as "%X %a %d %b" and so on. This corresponds to:

%X      -       current time, as defined by the locale
%a      -       abbreviated weekday
%d      -       day of month, 01 to 31
%b      -       abbreviated month name
..
..

and matches the output seen as "10:16:47 AM Wed 02 Apr 1997 BST". See the date(C) manual page for a complete description of the "%" settings. The "%X" setting within D_T_FMT in turn references TIME_FMT which is set to "%I:%M:%S %p". The "%p" option is defined as:

%p      equivalent of AM or PM for current locale

which accounts for the AM/PM output. The format is fixed by the locale file.

Several options exist to alter the AM/PM designation. These include:

(a) Set the locale environment variable LC_TIME to another locale.

As an example, the source file for the "C_C.C" locale specifies the date without the "AM/PM" designation:

...
#       timtbl specification file for C_C.C
#
DATE_FMT="%m/%d/%y"             # 04/22/87
TIME_FMT="%H:%M:%S"             # 14:43:40
D_T_FMT="%a %b %d %H:%M:%S %Y"  # Wed Apr 22 14:43:40 BST 1987
F_NOON="AM"
A_NOON="PM"
...
$ date
03:01:29 PM Mon 07 Apr 1997 BST
$ LC_TIME=C_C.C
$ export LC_TIME
$ date
Mon Apr  7 14:59:32 BST 1997
$locale
LANG=english_uk.8859
LC_CTYPE="english_uk.8859"
LC_COLLATE="english_uk.8859"
LC_TIME=C_C.C
LC_NUMERIC="english_uk.8859"
LC_MONETARY="english_uk.8859"
LC_MESSAGES="english_uk.8859"
LC_ALL=

(b) Set the locale environment variable LANG to another locale.

# date
04:39:14 PM Mon 07 Apr 1997 BST
# LANG=C_C.C
# export LANG
# date
Mon Apr  7 16:39:46 BST 1997
# locale
LANG=C_C.C
LC_CTYPE="C_C.C"
LC_COLLATE="C_C.C"
LC_TIME="C_C.C"
LC_NUMERIC="C_C.C"
LC_MONETARY="C_C.C"
LC_MESSAGES="C_C.C"
LC_ALL=

(c) Switch the system to use another default locale permanently, such as "C_C.C" using the International Settings Manager utility in scoadmin(ADM) or editing the "/etc/default/lang" file.

(d) The particular locale database file, in this case "tim.uk.src", can be modified using timtbl(M), or a new locale can be created for the system if several variables are to be changed from the default.

NOTE:     If the locale source files are changed, existing programs or
          scripts may be affected if they rely on certain formats within a
          locale. Also, the default locale files provided with the SCO
          operating system are conformant to publically accepted standards
          such as ISO, POSIX, BSI, XOpen, and so on. Altering these may make
          your system noncompliant. If changes are made, ensure that all
          previous settings are recorded and copies of the original files
          are made.

See also

date(C), locale(C), timtbl(M)

From the Xinuos technical article database, record 105525. Reproduced with permission. The original is at www.sco.com/ta/105525, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.