Time

This is a discussion on Time within the C++ Programming forums, part of the General Programming Boards category; What is code for getting the time from the computers clock?...

  1. #1
    Registered User zdude's Avatar
    Join Date
    Sep 2002
    Posts
    32

    Time

    What is code for getting the time from the computers clock?
    Those who live by the sword get shot by those who don't.

    I can C.

    Compiler: gcc

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main(void)
    {
    	time_t TheTime;
    
    	time(&TheTime);
    
    	cout << ctime(&TheTime) << endl;
    
    	return 0;
    }

  3. #3
    harryp
    Guest
    Is there a way to get JUST the time, like in int or long format?

    Brendan

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    What do you mean by JUST the time? Hours? Minutes? Seconds?
    Look into the tm struct, its member variables hold all kinds of different measures of the current time.

    Code:
    #include <ctime>
    
    int main(int argc char *argv[])
    {
    	using namespace std;
    	time_t currentTime = time(NULL);
    	tm *timeInfo;
    
    	timeInfo = localtime(&currentTime);
    	cout << 
    	  timeInfo->tm_hour << endl <<	// hr (0 - 23) 
    	  timeInfo->tm_min << endl <<	// min (0 - 59) 
    	  timeInfo->tm_sec << endl;	// sec (0 - 59)
    
      
    	return 0;
    }
    Last edited by Eibro; 09-04-2002 at 08:05 PM.

  5. #5
    harryp
    Guest
    The seconds works. Thanks! The reason I was wondering was because I wanted to make a random number generator that is seeded based on the current time, which meant I couldn't use the date and all that. This way, it may be a bit more random than it usually is. Thanks again!

    Brendan

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    You could use the Windows API function GetLocalTime() to initialize an instance of the SYSTEMTIME data structure...

    Code:
    #include <windows.h>
    #include <iostream>
    
    int main() {
      SYSTEMTIME st;
      GetLocalTime(&st);
      std::cout << st.wYear << std::endl
                << st.wMonth << std::endl
                << st.wDayOfWeek << std::endl
                << st.wDay << std::endl
                << st.wHour << std::endl
                << st.wMinute << std::endl
                << st.wSecond << std::endl
                << st.wMilliseconds << std::endl;
      exit(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 03:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 01:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21