Time 199
Listing 10-4: Demonstrate the effect of timezones and locales
––––––––––––––––––––––––––––––––––––––––––––––––––––––––– time/show_time.c
#include <time.h>
#include <locale.h>
#include "tlpi_hdr.h"
#define BUF_SIZE 200
int
main(int argc, char argv[])
{
time_t t;
struct tm loc;
char buf[BUF_SIZE];
if (setlocale(LC_ALL, "") == NULL)
errExit("setlocale"); / Use locale settings in conversions /
t = time(NULL);
printf("ctime() of time() value is: %s", ctime(&t));
loc = localtime(&t);
if (loc == NULL)
errExit("localtime");
printf("asctime() of local time is: %s", asctime(loc));
if (strftime(buf, BUF_SIZE, "%A, %d %B %Y, %H:%M:%S %Z", loc) == 0)
fatal("strftime returned 0");
printf("strftime() of local time is: %s\n", buf);
exit(EXIT_SUCCESS);
}
––––––––––––––––––––––––––––––––––––––––––––––––––––––––– time/show_time.c
SUSv3 defines two general ways in which the TZ environment variable can be set. As
just described, TZ can be set to a character sequence consisting of a colon plus a
string that identifies the timezone in an implementation-specific manner, typically
as a pathname for a timezone description file. (Linux and some other UNIX imple-
mentations permit the colon to be omitted when using this form, but SUSv3
doesn’t specify this; for portability, we should always include the colon.)
The other method of setting TZ is fully specified in SUSv3. In this method, we
assign a string of the following form to TZ:
std offset [ dst [ offset ][ , start-date [ /time ] , end-date [ /time ]]]
Spaces are included in the line above for clarity, but none should appear in the TZ
value. The brackets ([]) are used to indicate optional components.
The std and dst components are strings identifying the standard and DST time-
zones; for example, CET and CEST for Central European Time and Central Euro-
pean Summer Time. The offset in each case specifies the positive or negative