I’ve been having trouble getting my text based program to display the time correctly.
I want it to display in the format
Www Mmm dd hh:mm:ss yyyy
I can do so using this code here:
time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( "Current date and time are: %s", asctime (timeinfo) );
Here is an example of that output
Current date and time are: Sat May 20 15:21:51 2000
That’s exaclty how I want it to display, but the problem is the hour is wrong. I am in Mountain Time or -7:00GMT. So the clock is displayed 7 hours fast.
Now I can get it to work using gmtime instead of localtime, but then my day of the week, and month are now numeric. And if the tm_hour+(MST) = a negative number, it doesn’t convert it (for example -1 does not become 23). For example:
#define MST (-7) time_t rawtime; tm * ptm; time ( &rawtime ); ptm = gmtime ( &rawtime ); printf ("MST Is: %2d %02d %02d %2d:%02d:%02d %0002d ", ptm->tm_wday, ptm->tm_mon, tm_hour+MST, ptm->tm_min, ptm->tm_sec, ptm->tm_year+1900);
MST Is: 3 09 07 -01:15:13 2005
If anyone could shed some light on my problem I would greatly appreciate it. I am also just starting out in C++ and have been googling but couldn’t find the answer. Some code borrowed from here and here.