Jump to content

Timestamp in C++

- - - - -

  • Please log in to reply
3 replies to this topic

#1
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
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.

#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
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.

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
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
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
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly

Quote

>>How do you access timestamp in C++? I have troubles with std::time().

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