Hello All,
How do you access timestamp in C++? I have troubles with std::time().
Also, does it have a five-digit year storage? Because if it doesn't, then does that mean the year 10,000 problem applies to it?
Thank you.
3 replies to this topic
#1
Posted 03 July 2010 - 11:01 PM
|
|
|
#2
Posted 04 July 2010 - 04:51 AM
The value returned by time() is the number of seconds since January 1, 1970. So times before that date are invalid. Since it returns a size_t (normally unsigned long int) there is a maximum date/time of year 2038. Most systems are now supporting 64-bit time_t (unsigned long long int) and I don't know what the maximum date is on that.
The tm structure which is returned by localtime() contains tm_year as an int, which is the number of years since 1900. The maximum year is whatever is the maximum value that can be stored in an int (see your limits.h for maximum value)
>>How do you access timestamp in C++? I have troubles with std::time().
why?
wiki said:
In some newer operating systems, time_t has been widened to 64 bits. In the negative direction, this goes back more than twenty times the age of the universe, and so suffices. In the positive direction, whether the approximately 293 billion representable years is truly sufficient depends on the ultimate fate of the universe, but it is certainly adequate for most practical purposes.
The tm structure which is returned by localtime() contains tm_year as an int, which is the number of years since 1900. The maximum year is whatever is the maximum value that can be stored in an int (see your limits.h for maximum value)
>>How do you access timestamp in C++? I have troubles with std::time().
why?
#include <ctime>
int main()
{
time_t now = time(0);
struct tm* tm = localtime(&now);
}
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
#3
Posted 04 July 2010 - 11:45 AM
Note: Since the struct pointer returned by localtime() is statically allocated, any call to pretty much any other time function involving a tm struct will overwrite it. I would copy it over:
struct tm *tmp, now;
time_t now_epoch = time(NULL);
tmp = localtime(&now_epoch);
if( tmp )
memcpy(&now, tmp, sizeof(now));
else
/* handle problem here */
Edited by dargueta, 04 July 2010 - 11:45 AM.
Grammar
sudo rm -rf /
#4
Posted 04 July 2010 - 06:30 PM
Quote
>>How do you access timestamp in C++? I have troubles with std::time().
why?
why?
Well, I don't know...probably my compiler (it had some problems with environment variables and didn't operate properly until recently).
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









