Last active
May 25, 2022 15:36
-
-
Save manvscode/1d0ba6cdeb8f9277bfbeb9307a7e3cca to your computer and use it in GitHub Desktop.
Example of how to work with timezones in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
int main( int argc, char* argv[] ) | |
{ | |
char buffer[ 128 ]; | |
time_t now = time(NULL); | |
const char* time_str = ""; | |
setenv( "TZ", "Asia/Kolkata", 1 ); | |
tzset(); | |
time_str = asctime( localtime(&now) ); | |
strftime( buffer, sizeof(buffer), "%z", localtime(&now) ); | |
printf( "Asia/Kolkata %s %s", buffer, time_str ); | |
setenv( "TZ", "America/New_York", 1 ); | |
tzset(); | |
time_str = asctime( localtime(&now) ); | |
strftime( buffer, sizeof(buffer), "%z", localtime(&now) ); | |
printf( "America/New_York %s %s", buffer, time_str ); | |
setenv( "TZ", "Australia/Brisbane", 1 ); | |
tzset(); | |
time_str = asctime( localtime(&now) ); | |
strftime( buffer, sizeof(buffer), "%z", localtime(&now) ); | |
printf( "Australia/Brisbane %s %s", buffer, time_str ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment