0

I'm writing a server in C that is meant to get a request over UDP for the time in either: Doha, Prague, New York or Berlin. How can I produce the right response?

I tried looking into the localtime() method but couldn't find anything relating to a timezone

Elad Moshe
  • 16
  • 1
  • 1
  • 3

1 Answers1

0

Ok so I figured it out on my own. Let's say I want the current time in Doha. Apparently Doha's time is UTC+3. Which means that I need to get the current UTC time and add 3 hours to it like so:

char* response = malloc(255 * sizeof(char));
time_t now = time(NULL);
tm* t = gmtime(&now); //Gets me the UTC time
t->tm_hour += 3; //Adds 3 hours to it
mktime(t); //Normalizes it (so it's 60 seconds in a minute, 60 minutes in an hour and so on...)
strftime(response, 255, "%H:%M:%S", t);
Elad Moshe
  • 16
  • 1
  • 1
  • 3