UTC time Clock and Local time Clock?
-
Hi, I am making a program to print out both UTC time and local time. But, I am having the same time. Do I have to save UTC time before 'ptrLocalTime = localtime(&localtimer);'? /* This program shows UTC time, and local time in 00:00:00 */ #include #include #define PST (-8) //Pacific Time Zone #define CST (-6) //Central Time Zone int main () { time_t timer; time_t localtimer; tm* ptrUTC; tm* ptrLocalTime; time ( &timer ); //Set timer to the number of seconds elapsed since 00:00 hours, //Jan 1, 1970 UTC from the system clock. time(&localtimer); ptrUTC = gmtime ( &timer ); //Converts timer to tm structure adjusting to UTC //(formerly known as GMT) timezone. ptrLocalTime = localtime(&localtimer); printf ("UTC Time: %2d:%02d:%02d\n", ptrUTC->tm_hour, ptrUTC->tm_min, ptrUTC->tm_sec); printf ("Locat time : %2d:%02d:%02d\n", ptrLocalTime->tm_hour, ptrLocalTime->tm_min, ptrLocalTime->tm_sec); return 0; } Please! Yonggoo
-
Hi, I am making a program to print out both UTC time and local time. But, I am having the same time. Do I have to save UTC time before 'ptrLocalTime = localtime(&localtimer);'? /* This program shows UTC time, and local time in 00:00:00 */ #include #include #define PST (-8) //Pacific Time Zone #define CST (-6) //Central Time Zone int main () { time_t timer; time_t localtimer; tm* ptrUTC; tm* ptrLocalTime; time ( &timer ); //Set timer to the number of seconds elapsed since 00:00 hours, //Jan 1, 1970 UTC from the system clock. time(&localtimer); ptrUTC = gmtime ( &timer ); //Converts timer to tm structure adjusting to UTC //(formerly known as GMT) timezone. ptrLocalTime = localtime(&localtimer); printf ("UTC Time: %2d:%02d:%02d\n", ptrUTC->tm_hour, ptrUTC->tm_min, ptrUTC->tm_sec); printf ("Locat time : %2d:%02d:%02d\n", ptrLocalTime->tm_hour, ptrLocalTime->tm_min, ptrLocalTime->tm_sec); return 0; } Please! Yonggoo
The gmtime, mktime, and localtime functions use the same single, statically allocated structure to hold their results. Each call to one of these functions destroys the result of any previous call. Please check the address stored in ptrLocalTime and ptrUTC. Both are same. So you have to make a copy before calling another function.