Thread: Date and Time with <ctime>

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    Date and Time with <ctime>

    Hello,

    I am having a dilemma with manipulation of time and date in an assignment. I understand that I can only get limited help with my problem so any and every hint will definitely help me get going in the right direction!

    During my program I am prompting the user to enter a start date, and start time then I will prompt again to ask them to enter a stop date and stop time. Now I have researched into date and time functions with regards to the Ctime library but I'm really unsure and confused about how to manipulate the functions with regards to my code.

    Here's a sample of how it looks like:

    Date and Time with &lt;ctime&gt;-output-png

    Please note that it's in the format [DAY:MONTH:YEAR HOUR:MINUTE:SECOND] for both start date/time and stop date/time. I want the user to be able to enter any time or date that they want and not use the current system time etc. My professor mentioned that we could use it as a string but later on in the program I will have to subtract two times to see if the device is done recording (My program is to create a programmable household device system).

    Again any and all help is greatly appreciated!

    Thanks,

    Fekore
    Last edited by Fekore; 11-13-2012 at 08:11 PM. Reason: Clarification

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    mktime - C++ Reference
    Fill in a struct tm for each time, use mktime() to get a time_t representation.

    Having done that, use difftime() to work out the difference in seconds.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Quote Originally Posted by Salem View Post
    mktime - C++ Reference
    Fill in a struct tm for each time, use mktime() to get a time_t representation.

    Having done that, use difftime() to work out the difference in seconds.
    Thanks for your response however I am still confused about how I would go about getting the input from the user. So far I have...
    Code:
    public:
    	void getinfo()	//function used to get channel, date and time from the user about a specific device
    	{
    		cout<<endl<<endl<<setw(33)<<right<<"Select the channel ==> ";
    		cin>>channel;
    
    		/*The below code prompts the user for a start date/time & a stop date/time*/
    		cout<<endl<<setw(49)<<right<<"Enter the start date (Day Mon Year) => ";
    		cin>>start_day>>start_month>>start_year;
    		cout<<setw(49)<<right<<"Enter the start time (Hour Min Sec) => ";
    		cin>>start_hour>>start_min>>start_sec;
    		cout<<endl<<setw(48)<<right<<"Enter the stop date (Day Mon Year) => ";
    		cin>>stop_day>>stop_month>>stop_year;
    		cout<<setw(48)<<right<<"Enter the stop time (Hour Min Sec) => ";
    		cin>>stop_hour>>stop_min>>stop_sec;
    
    		/*Test code to process time/date etc from the user!*/
    		time (&rawtime);
    		timeinfo = localtime (&rawtime);
    		timeinfo->tm_year = start_year - 1900;
    		timeinfo->tm_mon = start_month - 1;
    		timeinfo->tm_mday = start_day;
    	}
    But I am really unsure if i'm heading in the right direction. I was told by my professor that similar to
    Code:
    #include <string>
    (where you can use the string data type) there is one for time and date. I still need more clarification on how to go about using mktime and/or any other functions built into the <Ctime> library.

    Thanks again!

    Edit: I have found the data type for strictly time: http://www.cplusplus.com/reference/clibrary/ctime/time/ but I cannot find anything for date.
    Last edited by Fekore; 11-15-2012 at 06:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linux ctime(date as a name)
    By septix in forum C Programming
    Replies: 5
    Last Post: 08-01-2011, 03:13 PM
  2. time.h ctime and milliseconds
    By Deewhyandy1 in forum C Programming
    Replies: 4
    Last Post: 01-26-2008, 01:42 AM
  3. time precision (ctime), tm struct question
    By cjschw in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2003, 01:51 PM
  4. ctime - current date
    By razrektah in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2001, 12:42 PM
  5. how to pull system time, date, etc from <ctime>
    By Matt in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2001, 09:08 AM