Thread: Does C++ have any standard implementation for time/date?

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Does C++ have any standard implementation for time/date?

    I'm just looking for a simpler method to get the time and date than the ctime class provides. I want one where it's a little easier to format. I was at first trying to figure out the strftime but ended up using the

    Code:
    Date::Date()
    {
        time_t rawtime;
        rawtime = time(&rawtime);
        string junk;
    
        istringstream i (ctime(&rawtime));
        i >> junk >> month >> day >> junk >> year;
    }
    I don't know what it means but I ended up with some junk values. I easily threw away, but is there a more standard library that provides similar functions.

  2. #2
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    cout << "The time is: " << __TIME__ << endl;
    cout << "The date is: " << __DATE__ << endl;
    
    return 0;
    }
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    brilliant

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Not really, since those aren't defined by the standard and may not be implemented on another compiler.

    The standard way is the one defined by the ctime header and the one you were trying to use. I suggest you keep at it. After a while you'll get used.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Except __DATE__ is constant for the time the program was compiled.
    Run it tomorrow, and it will still be the same.

    > I easily threw away, but is there a more standard library that provides similar functions.
    You mean like gmtime() and localtime() which return a 'struct tm' full of time components.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::vector<string>
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 03-23-2008, 08:21 PM
  2. Replies: 8
    Last Post: 03-08-2008, 08:50 PM
  3. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  4. C/C++ standard
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2003, 03:22 AM
  5. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 04:21 AM