How to get the local time in seconds?
-
How to get local time in seconds? I am using MS Visual Studio 2005. I could get UTC time in seconds below. Are they correct? Thanks! #include #include double Clock::getUtcSeconds()const // returns UTC time in second { time_t UtcTimeValue; return time(&UtcTimeValue); } string Clock::getUtcHMSTime()const { char buffer[10]; string strUtcTime; time_t UtcTimeValue; time(&UtcTimeValue); struct tm* UtcTime = gmtime(&UtcTimeValue); //convert the calender time to UTC time strUtcTime+=itoa(UtcTime->tm_hour, buffer,10); //convert to decimal number strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_min, buffer,10); strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_sec, buffer,10); return strUtcTime; }
Yonggoo
-
How to get local time in seconds? I am using MS Visual Studio 2005. I could get UTC time in seconds below. Are they correct? Thanks! #include #include double Clock::getUtcSeconds()const // returns UTC time in second { time_t UtcTimeValue; return time(&UtcTimeValue); } string Clock::getUtcHMSTime()const { char buffer[10]; string strUtcTime; time_t UtcTimeValue; time(&UtcTimeValue); struct tm* UtcTime = gmtime(&UtcTimeValue); //convert the calender time to UTC time strUtcTime+=itoa(UtcTime->tm_hour, buffer,10); //convert to decimal number strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_min, buffer,10); strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_sec, buffer,10); return strUtcTime; }
Yonggoo
-
We want to keep our system cross-platform. Any ANSI standard way to get local time in seconds?
Yonggoo
-
How to get local time in seconds? I am using MS Visual Studio 2005. I could get UTC time in seconds below. Are they correct? Thanks! #include #include double Clock::getUtcSeconds()const // returns UTC time in second { time_t UtcTimeValue; return time(&UtcTimeValue); } string Clock::getUtcHMSTime()const { char buffer[10]; string strUtcTime; time_t UtcTimeValue; time(&UtcTimeValue); struct tm* UtcTime = gmtime(&UtcTimeValue); //convert the calender time to UTC time strUtcTime+=itoa(UtcTime->tm_hour, buffer,10); //convert to decimal number strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_min, buffer,10); strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_sec, buffer,10); return strUtcTime; }
Yonggoo
Yonggoo wrote:
I could get UTC time in seconds below. Are they correct?
Have you tried it? Good luck on January 18, 2038 :)
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")
-
How to get local time in seconds? I am using MS Visual Studio 2005. I could get UTC time in seconds below. Are they correct? Thanks! #include #include double Clock::getUtcSeconds()const // returns UTC time in second { time_t UtcTimeValue; return time(&UtcTimeValue); } string Clock::getUtcHMSTime()const { char buffer[10]; string strUtcTime; time_t UtcTimeValue; time(&UtcTimeValue); struct tm* UtcTime = gmtime(&UtcTimeValue); //convert the calender time to UTC time strUtcTime+=itoa(UtcTime->tm_hour, buffer,10); //convert to decimal number strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_min, buffer,10); strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_sec, buffer,10); return strUtcTime; }
Yonggoo
Yonggoo wrote:
Are they correct?
You are in the best position to determine that.
Yonggoo wrote:
strUtcTime+=" : ";
You are making an assumption here that a colon is always the separator.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
We want to keep our system cross-platform. Any ANSI standard way to get local time in seconds?
Yonggoo
I have tried many ways. The following worked. double Clock::getLocalSeconds()const // The local time is hours behind. All else are the sams as UTC time. { time_t UtcTimeValue; time(&UtcTimeValue); //get the system time, which is UTC time struct tm* UtcHMS= gmtime(&UtcTimeValue); //convert it to struct tm*, which is UTC time int utcHours = UtcHMS->tm_hour; time_t LocalTimeValue; time(&LocalTimeValue); //get the system time, which is UTC time struct tm* LocalHMS = localtime(&LocalTimeValue); //convert it to struct tm*, which is the local time int localHours = LocalHMS->tm_hour; double diffHours =utcHours - localHours; return ((double)UtcTimeValue - diffHours*SECONDS_IN_HOUR); }
Yonggoo
-
Yonggoo wrote:
Any ANSI standard way to get local time in seconds?
To bad you didn't include that requirement in your first post I might not have wasted my time giving you an answer that you can't use. X|
led mike