Thread: ctime - advice needed !!!!!

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Thumbs up ctime - advice needed !!!!!

    Hi all,

    I'm learning C++ from a textbook and got stumped by an exercise asking for a constructor capable of using the current time from the time and localtime functions in <ctime> in a class called Time.

    So far I've been playing around and formulating a plan to find the answer, here's what I have so far:

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main()
    {
        time_t t;
        t = time( NULL );
        cout << "Number of seconds passed since Jan. 1, 1970: " << t << endl;
        cout << "Number of minutes passed since Jan. 1, 1970: " << t / 60 << endl;
        cout << "Number of hours passed since Jan. 1, 1970: " << ( t / 60 ) / 60 << endl;
        cout << "Number of days passed since Jan. 1, 1970: " << ( ( t / 60 ) / 60 ) / 24 << endl;
        cout << "Number of years passed since Jan. 1, 1970: " << ( ( ( t / 60 ) / 60 ) / 24 ) / 365 << endl;
    } // end main
    That's relatively easy, but how on Earth do I work out the months?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You need to look at the struct tm component of the ctime lib and also the localtime() function.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice needed
    By Incantrix in forum Game Programming
    Replies: 4
    Last Post: 10-18-2010, 08:39 PM
  2. Classes advice needed
    By DanFraser in forum C# Programming
    Replies: 5
    Last Post: 12-05-2007, 05:30 AM
  3. Learning to Program in C - Advice Needed
    By EnderMB in forum C Programming
    Replies: 3
    Last Post: 10-16-2006, 07:40 PM
  4. Job advice needed regarding C++
    By joeyzt in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-20-2003, 01:10 AM
  5. advice needed
    By unregisterd in forum C Programming
    Replies: 3
    Last Post: 10-19-2002, 07:04 AM