Skip to content

Instantly share code, notes, and snippets.

@gcmurphy
Created March 12, 2015 19:20
Show Gist options
  • Save gcmurphy/e81cb378c2e53061dae2 to your computer and use it in GitHub Desktop.
Save gcmurphy/e81cb378c2e53061dae2 to your computer and use it in GitHub Desktop.
WTF time is it in UTC
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
void display_current_time(const char *name, short tz){
time_t t;
struct tm *tdata;
time(&t);
tdata = gmtime(&t);
printf("\t%s:\t%2d:%02d\n", name, (tdata->tm_hour + tz) % 24, tdata->tm_min);
}
void display_time_for_utc(const char *name, short utc_hour, short tz){
short tz_time;
tz_time = utc_hour + tz;
if (tz_time < 0){
tz_time = 24 + tz_time;
printf("%s: %2d:%02d the previous day when it is: %2d:%02d UTC\n",
name, tz_time, 0, utc_hour, 0);
} else if (tz_time > 24){
tz_time %= 24;
printf("%s: %2d:%02d the next day when it is: %2d:%02d UTC\n",
name, tz_time, 0, utc_hour, 0);
} else {
printf("%s: %2d:%02d when it is %2d:%02d UTC\n",
name, tz_time, 0, utc_hour, 0);
}
}
int main(int argc, char *argv[]){
int i;
short utc_hour;
char *end;
for (i = 1; i < argc; ++i){
utc_hour = strtol(argv[i], &end, 10);
if (end != argv[i]+strlen(argv[i])){
printf("Expected UTC hour as argument. Not '%s'\n", argv[i]);
} else {
if (utc_hour > 24){
printf("Converting %d to %d (24 hour time)\n", utc_hour, utc_hour % 24);
utc_hour %= 24;
}
display_time_for_utc("PST", utc_hour, (-8));
display_time_for_utc("AEST", utc_hour, (+10));
}
}
printf("Current time:\n");
display_current_time("UTC", 0);
display_current_time("PST", (-8));
display_current_time("AEST", (+10));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment