Thread: Get info from Computer clock.

  1. #1
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    Get info from Computer clock.

    I can use Clock() to get a pause in the system, But is there a function that actually works as a clock? I can make a Stopwatach thingy, but no buttons on top. But i really would like to have a function that gets the Day, Date Hours:Minutes:seconds from the PC clock. Is there a way to do that?
    This war, like the next war, is a war to end war.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Is there a way to do that?
    There sure is :-)
    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
      time_t current;
    
      current = time(0);
    
      if (current != static_cast<time_t>(-1))
      {
        cout<< ctime(&current) <<flush;
      }
    }
    You can also use the tm struct and get more control over the content itself :-)
    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
      time_t current;
      struct tm *date;
      char datestr[1024];
      const char fmt[] = "%A %B %d, %y -- %I:%M:%S %p";
    
      current = time(0);
      date = localtime(&current);
    
      if (strftime(datestr, sizeof datestr, fmt, date) != 0)
      {
        cout<< datestr <<endl;
      }
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clock Troubles
    By _Nate_ in forum C Programming
    Replies: 22
    Last Post: 06-19-2008, 05:15 AM
  2. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  3. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  4. A talking computer
    By AdamLAN in forum C++ Programming
    Replies: 19
    Last Post: 09-24-2004, 06:17 AM
  5. Replies: 23
    Last Post: 01-31-2003, 03:13 AM