DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Internationalization

Date and time

The ANSI C function strftime provides a sprintf-like formatting of the values in a struct tm, along with some date and time representations that depend on the LC_CTIME category of the current locale. (strftime supersedes ctime and ascftime, although, for the sake of compatibility with older systems, these routines format the date and time correctly for a given locale.) Here is how you might use strftime to print the current date in a locale-dependent way:

#include <stdio.h>
#include <locale.h>
#include <time.h>

main() { time_t tval; struct tm *tmptr; char buf [BUFSIZ];

tval = time(NULL); tmptr = localtime(&tval);

setlocale(LC_ALL, "");

strftime(buf, BUFSIZE, "%x", tmptr); puts(buf); }

In this case, strftime puts characters into the array pointed to by buf, as controlled by the string pointed to by %x. %x is a directive that provides an implementation-defined date representation appropriate to the locale. In a Spanish locale, for example, the current date June 14, 1990, might be represented as 14 Junio 1990 or 14/6/90 or any other way the implementation deems appropriate to the locale. No particular format is guaranteed.
Use the %X directive to obtain the locale's appropriate time representation:

   strftime(buf, BUFSIZE, "%x %X", tmptr)
or %c to obtain both the date and time representation. Check the strftime(3C) manual page for the other directives.

Although it requires a bit more work, you can control the format of the date and time for different locales by using printf with the message retrieval functions gettxt or catgets. Suppose, for example, you want the current date June 14, 1990, to be displayed in a British locale as 14/6/90, in a German locale as 14.6.90, and in a U.S. locale as 6/14/90. What you need, in other words, is some way to switch the arguments to printf depending on the program's current locale. The %n$ form of conversion specification lets you convert the nth argument in a printf argument list rather than the next unused argument. That is,

   printf(gettxt("progmsgs:9", "%d/%d/%d\n"),
   	tm->tm.mon,
   	tm->tm.mday,
   	tm->tm.year);
will produce the locale-dependent date displays we want, so long as the string whose index is 9 in the message file progmsgs reads, in the British locale
   "%2$d/%1$d/%3$d\n"
in the German locale
   "%2$d.%1$d.%3$d\n"
and in the U.S. locale
   "%1$d/%2$d/%3$d\n" /* or simply "%d/%d/%d\n" */

You can use scanf in a similar way to interpret formatted dates in the input:

   int month, day, year;
   scanf(gettxt("progmsgs:9", "%d/%d/%d\n"),
   	&month, &day, &year);
Note that the %n$ form of conversion specification has a wider application than the one we've described here, as we will show in ``Message handling''. There, too, we will take a closer look at gettxt and catgets. See fprintf(3S), fscanf(3S), gettxt(3C) and catgets(3C).
Next topic: Numeric and monetary information
Previous topic: Cultural and language conventions

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004