1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main() {
time_t now = time(NULL);
struct tm local_time;
struct tm *temp;
char default_buffer[100];
char ny_buffer[100];
char tokyo_buffer[100];
// Get default time zone
temp = localtime(&now);
if (temp == NULL) return 1;
local_time = *temp;
strftime(default_buffer, sizeof(default_buffer), "%Y-%m-%d %H:%M:%S %Z", &local_time);
// Change to New York time
setenv("TZ", "America/New_York", 1);
tzset();
temp = localtime(&now);
if (temp == NULL) return 1;
local_time = *temp;
strftime(ny_buffer, sizeof(ny_buffer), "%Y-%m-%d %H:%M:%S %Z", &local_time);
// Change to Tokyo time
setenv("TZ", "Asia/Tokyo", 1);
tzset();
temp = localtime(&now);
if (temp == NULL) return 1;
local_time = *temp;
strftime(tokyo_buffer, sizeof(tokyo_buffer), "%Y-%m-%d %H:%M:%S %Z", &local_time);
// Now print all three (safe - each has its own buffer)
printf("Default: %s\n", default_buffer);
printf("New York: %s\n", ny_buffer);
printf("Tokyo: %s\n", tokyo_buffer);
return 0;
}
|