I want to get the current time in Hong Kong (UTC+8), and my local time is UTC-5.
Using and running the following in VS2012:
#pragma warning(disable : 4996)
char buffer[10];
time_t rawtime;
time(&rawtime);
strftime(buffer, 10, "%H:%M:%S", localtime(&rawtime));
cout << "LocalTime=" << buffer << endl;
strftime(buffer, 10, "%H:%M:%S", gmtime(&rawtime));
cout << "GMTime=" << buffer << endl;
tm* r = gmtime(&rawtime);
r->tm_hour += 8; // Hong Kong time
mktime(r); // Normalize the struct
strftime(buffer, 10, "%H:%M:%S", r);
cout << "HongKongTime=" << buffer << endl;
Produces the following output:
LocalTime=22:51:47
GMTime=02:51:47
HongKongTime=11:51:47
So it's computing UTC correctly, but then adding 8 hours to that is actually producing a time that is UTC +9. What's going wrong?
And is there a more elegant/reliable way of getting UTC+8 than this kludge?
Aucun commentaire:
Enregistrer un commentaire