summaryrefslogtreecommitdiff
path: root/localutc.c
blob: 3f2334e114b0cf21e2d9ece1c6f3800415cabde6 (plain)
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
#include <stdio.h>
#include <time.h>

int main() {
    time_t now = time(NULL);
    struct tm local_time, utc_time;
    struct tm *temp;
    char local_buffer[100];
    char utc_buffer[100];
    
    // Get local time and copy it
    temp = localtime(&now);
    if (temp == NULL) {
        fprintf(stderr, "Error: localtime() failed\n");
        return 1;
    }
    local_time = *temp;
    
    // Get UTC time and copy it
    temp = gmtime(&now);
    if (temp == NULL) {
        fprintf(stderr, "Error: gmtime() failed\n");
        return 1;
    }
    utc_time = *temp;
    
    // Format into separate buffers
    strftime(local_buffer, sizeof(local_buffer), "%Y-%m-%d %H:%M:%S", &local_time);
    strftime(utc_buffer, sizeof(utc_buffer), "%Y-%m-%d %H:%M:%S", &utc_time);
    
    // Now print (order doesn't matter - data is safe)
    printf("Local time: %s\n", local_buffer);
    printf("UTC time:   %s\n", utc_buffer);
    
    return 0;
}