c - Getting UTC time as time_t -
i trying utc time time_t
. code below seems giving correct surprisingly prints local time only:
time_t mytime; struct tm * ptm; time ( &mytime ); // local time in time_t ptm = gmtime ( &mytime ); // find out utc time time_t utctime = mktime(ptm); // utc time time_t printf("\nlocal time %x (%s) , utc time %x (%s)", mytime, ctime(&mytime), utctime, ctime(&utctime));
as can see values of mytime
, utctime
different. however, when passed parameters ctime
converts them same string.
local time 55d0cb59 (sun aug 16 23:11:45 2015) , utc time 55d07e01 (sun aug 16 23:11:45 2015)
the ctime result static variable. not use ctime twice in same print statement. in 2 separate print statements.
if ctime replaced asctime, same problem arise asctime returns result static variable.
Comments
Post a Comment