Example (tzset -- Assign Values to Locale Information)
This example uses putenv and tzset to set the time zone to Central Time.#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
time_t currentTime;
struct tm *ts;
/* Get the current time */
(void)time(¤tTime);
printf("The GMT time is %s", asctime(gmtime(¤tTime)));
ts = localtime(¤tTime);
if (ts->tm_isdst > 0) /* check if Daylight Saving Time is in effect */
{
printf("The local time is %s", asctime(ts));
printf("Daylight Saving Time is in effect.\n");
}
else {
printf("The local time is %s", asctime(ts));
printf("Daylight Saving Time is not in effect.\n");
}
printf("**** Changing to Central Time ****\n");
putenv("TZ=CST6CDT");
tzset();
ts = localtime(¤tTime);
if (ts->tm_isdst > 0) /* check if Daylight Saving Time is in effect */
{
printf("The local time is %s", asctime(ts));
printf("Daylight Saving Time is in effect.\n");
}
else {
printf("The local time is %s", asctime(ts));
printf("Daylight Saving Time is not in effect.\n");
}
return 0;
/****************************************************************************
The output should be similar to:
The GMT time is Fri Jan 13 21:49:26 1995
The local time is Fri Jan 13 16:49:26 1995
Daylight Saving Time is not in effect.
**** Changing to Central Time ****
The local time is Fri Jan 13 15:49:26 1995
Daylight Saving Time is not in effect.
****************************************************************************/
}