I'm trying to write a function that takes a time_t
as an argument and prints the date and time in GMT and in local time. However, gmtime()
and localtime()
are both giving GMT time. I've tried the things that have been suggested on a couple of pages, but I'm still getting GMT for both.
Here is my code:
#include <stdio.h>
#include <time.h>
void printDateOfTime(time_t t);
int main(void)
{
time_t now;
now = time(NULL);
printDateOfTime(now);
return 0;
}
void printDateOfTime(time_t t)
{
struct tm* gTime;
struct tm* lTime;
lTime = localtime(&t);
printf("Local: %s\n", asctime(lTime));
gTime = gmtime(&t);
printf("GMT: %s\n", asctime(gTime));
}
I'm in the Eastern Standard Timezone (5 hours behind GMT), and here is the output I'm getting:
Local: Mon Nov 5 06:18:57 2018
GMT: Mon Nov 5 06:18:57 2018
转载于:https://stackoverflow.com/questions/53149399/why-are-localtime-and-gmtime-giving-same-result-in-c