|
-
December 14th, 2010, 05:37 AM
#1
time_t
Hi,
I have a doubt regarding time_t
I am initializing a time_t variable to 0.
When I display it, it shows "Thu Jan 1 07:30:00 1970" ?
I expected it to Thu Jan 1 00:0:00 1970 since t1 was initialized with 0
Pls let me know why this happens and how it can be rectified
Note:
Operating System - OS X (Mac)
Compiler - GCC
Given below is the code
Code:
/*
Aim - To set time_t variable to 01-Jan-1970 00:00:00
*/
#include <iostream>
#include <time.h>
int main()
{
system("clear");
time_t t1(0);
std :: cout << "t1 = " << ctime(&t1) << std :: endl;
//Why does this display "Thu Jan 1 07:30:00 1970" ?
//I expected it to "Thu Jan 1 00:0:00 1970", since t1 was initialized with 0
return(0);
}
-
December 14th, 2010, 06:00 AM
#2
Re: time_t
I assume you live in a time zone which is 7.30 hours away from GMT zone (London).
You may use functions like gmtime to get UCT times or use tzset to set your timezone accordingly.
-
December 14th, 2010, 06:09 AM
#3
Re: time_t
That would put you on a longitude somewhere between Laos and the Philippines?
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
December 14th, 2010, 06:10 AM
#4
Re: time_t
Thanks a ton !! I wouldn't have guessed that in my dreams !!
Windows shows it as 8....
Strange it actually needs to be 8hrs .... not sure why it is 7:30 ... though the timezone is properly configured on the system.
Thanks again !!
-
December 14th, 2010, 06:12 AM
#5
Re: time_t
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
December 14th, 2010, 06:22 AM
#6
Re: time_t
 Originally Posted by Muthuveerappan
Thanks a ton !! I wouldn't have guessed that in my dreams !!
Windows shows it as 8....
Strange it actually needs to be 8hrs ....  not sure why it is 7:30 ... though the timezone is properly configured on the system.
Thanks again !!
You may check for the values in _timezone and _tzname[0], _tzname[1]. These global variables are declared in time.h.
-
December 14th, 2010, 11:25 PM
#7
Re: time_t
Thanks
------------
Thanks a lot !!
gmtime actually resolves the problem because it is the GMT time
I did check timezone, tzname[0], tzname[1], it doesn't show the correct names, but thats ok.
I guess gmtime should do it for me.
Question
---------------
I am new to this library, according to what i understand, time_t can only store upto the nearest second. Correct me if i am wrong
Is there a data type that is capable of storing milliseconds or is time_t capable of storing it ?
-
December 15th, 2010, 04:02 AM
#8
Re: time_t
time_t is usually in seconds, which, in a 32 system, will cause a problem in the year 2038
1970 + 2^31 seconds = 2038. Sometime about the 18th January.
http://en.wikipedia.org/wiki/Year_2038_problem
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
December 15th, 2010, 04:07 AM
#9
Re: time_t
 Originally Posted by Muthuveerappan
Thanks
------------
Thanks a lot !!
gmtime actually resolves the problem because it is the GMT time
I did check timezone, tzname[0], tzname[1], it doesn't show the correct names, but thats ok.
I guess gmtime should do it for me.
Question
---------------
I am new to this library, according to what i understand, time_t can only store upto the nearest second. Correct me if i am wrong
Is there a data type that is capable of storing milliseconds or is time_t capable of storing it ?
Yes, it is timeb (or _timeb) structure that additionally contains milliseconds. Use ftime to get a timeb struct.
-
December 15th, 2010, 05:26 AM
#10
Re: time_t
 Originally Posted by Muthuveerappan
Is there a data type that is capable of storing milliseconds or is time_t capable of storing it ?
boost::posix_time::ptime
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
December 16th, 2010, 03:28 AM
#11
Re: time_t
Thanks a lot !!
Just feel timeb might be compiler specific (I could be completely wrong)
We are working on mac and windows... so i thought it would be better to use boost library (posix time).
the name posix worries me... if it would work on windows .... 
hopefully it would work on windows as well.
-
December 16th, 2010, 04:15 AM
#12
Re: time_t
 Originally Posted by Muthuveerappan
hopefully it would work on windows as well.
It does.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
December 16th, 2010, 05:29 AM
#13
Re: time_t
 Originally Posted by Muthuveerappan
Thanks a lot !!
Just feel timeb might be compiler specific (I could be completely wrong)
We are working on mac and windows... so i thought it would be better to use boost library (posix time).
the name posix worries me... if it would work on windows ....
hopefully it would work on windows as well.
ftime and timeb were available for any platform.
Code:
/* get local timestamp */
std::string getTimestampAsString(struct timeb* pTimB)
{
char szTimFmt[] = "%Y%m%d %H%M%S";
char szTim[64] = { '\0' };
struct timeb tim;
struct tm* pTim = NULL;
/* get current time in milliseconds */
if (pTimB == NULL)
ftime(&tim);
else
memcpy(&tim, pTimB, sizeof(struct timeb));
/* convert time part to local time */
pTim = localtime(&tim.time);
/* convert to ascii */
strftime(szTim, sizeof(szTim), szTimFmt, pTim);
std::ostringstream oss;
oss << szTim << "." << std::setw(3) << std::setfill('\0') << std::right << tim.millitm;
return oss.str();
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|