C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

How to use ctime functions(time.h) Rate Topic: -----

#1 tnjones  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-August 08


Dream Kudos: 0

Share |

How to use ctime functions(time.h)

Posted 22 September 2008 - 01:55 PM

Hey,
I have a project where I need to get the system time along with the wall clock time in C++. Currently, I have researched time and found out about clock() but isn't sure on how to use it to get the system time. If anyone can give an example I will greatly appreciate it. By the way is this the best way to get the system clock in Linux using C++.

Thanks In Advance
Was This Post Helpful? 0
  • +
  • -


#2 UG Cyber  Icon User is offline

  • D.I.C Regular
  • PipPipPip

Reputation: 14
  • Posts: 414
  • Joined: 24-July 08


Dream Kudos: 0

Re: How to use ctime functions(time.h)

Posted 22 September 2008 - 02:06 PM

View Posttnjones, on 22 Sep, 2008 - 02:55 PM, said:

Hey,
I have a project where I need to get the system time along with the wall clock time in C++. Currently, I have researched time and found out about clock() but isn't sure on how to use it to get the system time. If anyone can give an example I will greatly appreciate it. By the way is this the best way to get the system clock in Linux using C++.

Thanks In Advance


This is how to get System time.....Just scroll down to the C++

http://www.codersour..._date_time.html

Not sure if it will work on linux though, i dont work with linux.

This post has been edited by UG Cyber: 22 September 2008 - 02:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 gabehabe  Icon User is offline

  • Black Scatmaster
  • Icon

Reputation: 260
  • View blog
  • Posts: 9,437
  • Joined: 06-February 08


Dream Kudos: 3300

Expert In: Lots of things.

Re: How to use ctime functions(time.h)

Posted 22 September 2008 - 02:17 PM

Just to get you started, a simple way to get the system time/date is the following:
#include <iostream>
#include <ctime>

using namespace std;

int main ()
{
    time_t now = time(0);
    cout << ctime(&now);
    cin.get();
    return EXIT_SUCCESS;
}

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#4 sergio1  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 15-July 08


Dream Kudos: 0

Re: How to use ctime functions(time.h)

Posted 22 September 2008 - 04:47 PM

I made this just for you:


	SYSTEMTIME test;
	GetLocalTime(&test); // makes sure its GetLocalTime(&test) and not GetsystemTime(&test)
	int day = test.wDay;
	int month = test.wMonth;
	int dayofweek = test.wDayOfWeek;
	int year = test.wYear;
	int hour = test.wHour;
	int minute = test.wMinute;
	int second = test.wSecond;
	int realhr = hour;
	string am_pm = " am";
	if (hour >= 12)
	{
		realhr = hour - 12;
		am_pm=" pm";
	}
	cout << "today is : "<<month<<"/"<<day<<"/"<<year<<endl;
	cout << "the time is: "<<realhr<<":"<<minute<<":"<<second<<am_pm<<endl;

	switch(dayofweek){
		case 1:
			cout << "its monday"<<endl;
			break;
		case 2:
			cout << "its tuesday"<<endl;
			break;
		case 3:		
			cout << "its wendsday"<<endl;
			break;
		case 4:	
			cout << "its thursday"<<endl;
			break;
		case 5:
			cout << "its fryday"<<endl;
			break;
		case 6:
			cout << "its saturday"<<endl;
			break;
		case 0:
			cout << "its sunday"<<endl;
			break;
		default:	
		cout << "error with year"<<endl;
		

Was This Post Helpful? 0
  • +
  • -

#5 alowais  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 13-September 08


Dream Kudos: 0

Re: How to use ctime functions(time.h)

Posted 23 September 2008 - 07:23 AM

View Postgabehabe, on 22 Sep, 2008 - 03:17 PM, said:

Just to get you started, a simple way to get the system time/date is the following:
#include <iostream>
#include <ctime>

using namespace std;

int main ()
{
    time_t now = time(0);
    cout << ctime(&now);
    cin.get();
    return EXIT_SUCCESS;
}

Hope this helps :)



Can i ask you : why you place cin.get();
thank you,
[color=#660000]

This post has been edited by alowais: 23 September 2008 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

#6 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • Icon

Reputation: 131
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07


Dream Kudos: 775

Re: How to use ctime functions(time.h)

Posted 23 September 2008 - 07:28 AM

View Postalowais, on 23 Sep, 2008 - 10:23 AM, said:


Can i ask you : why you place cin.get();
thank you,


You can find the answer to that here (holding the execution window open):
http://www.dreaminco...h...st&p=238158

edit--ha ha, beat you to it, gabehabe ;)

This post has been edited by OliveOyl3471: 23 September 2008 - 07:29 AM

Was This Post Helpful? 0
  • +
  • -

#7 gabehabe  Icon User is offline

  • Black Scatmaster
  • Icon

Reputation: 260
  • View blog
  • Posts: 9,437
  • Joined: 06-February 08


Dream Kudos: 3300

Expert In: Lots of things.

Re: How to use ctime functions(time.h)

Posted 23 September 2008 - 07:28 AM

To hold the execution window open. cin.get() waits until the user presses return (the enter key)

:)
Was This Post Helpful? 0
  • +
  • -

#8 alowais  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 13-September 08


Dream Kudos: 0

Re: How to use ctime functions(time.h)

Posted 23 September 2008 - 07:34 AM

Thanks guys both of you, :D
Was This Post Helpful? 0
  • +
  • -

#9 David W  Icon User is offline

  • DIC supporter
  • Icon

Reputation: 130
  • Posts: 1,013
  • Joined: 20-September 08


Dream Kudos: 1075

Re: How to use ctime functions(time.h)

Posted 23 September 2008 - 02:45 PM

View Postgabehabe, on 22 Sep, 2008 - 03:17 PM, said:

Just to get you started, a simple way to get the system time/date is the following:
#include <iostream>
#include <ctime>

using namespace std;

int main ()
{
    time_t now = time(0);
    cout << ctime(&now);
    cin.get();
    return EXIT_SUCCESS;
}

Hope this helps :)



Some work on the above can produce useful results, such as ...


// Another C++ way to get date/time info and format as you like ...
// Demos a table lookup and function 'atoi' ... 'alpha to int'

#include <iostream> 
#include <vector> 
#include <ctime>  
#include <sstream>
using namespace std;  

const string monthsTable[] =
{
	"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
	"01", "02", "03", "04", "05", "06", 
	"07", "08", "09", "10", "11", "12",
	"January", "Febuary", "March", "April", "May", "June", 
	"July", "August", "September", "October", "November", "December"
};

// If pass in 'Sep' finds 'n=8' and returns the string '09' ...
string getMonthNumStr( string mStr )
{
	// Now do table look up ...
	int n=0;
	while (n<12)
	{
		if(monthsTable[n] == mStr )
			break;
		++n;
	} 
	// ok ... so now we have 'n' ...
	return monthsTable[12+n];
} 

// If pass in 'Sep' finds 'n=8' and returns the string 'September'
string getLongNameStr( string mStr )
{
	// Now do table look up ...
	int n=0;
	while (n<12)
	{
		if(monthsTable[n] == mStr )
			break;
		++n;
	} 
	// ok ... so now we have 'n' ...
	return monthsTable[24+n];
}


int main()  
{
	time_t now = time(0);
	ostringstream s1;
	s1 <<  ctime(&now);
	string s2 = s1.str();
	
	// Now we have our date/time data in string s2 ... so
	// picking apart s2 gives ...
	string yearStr  = s2.substr(20,4);  // '2008'
	string monthStr = s2.substr(4,3);   // 'Aug'
	string dayStr   = s2.substr(8,2);   // '08'
	string timeStr  = s2.substr(11,8);  // '01:01:01'
	
	// and adding some more formats to be available for output gives ...
	string monthNumStr = getMonthNumStr(monthStr);  // 'Sep' gives '09'
	string mLongNameStr = getLongNameStr(monthStr);  // 'Sep' gives 'September'
	// first convert to 'c string'; then to 'int' ... to get rid of leading '0'
	int dayNum = atoi( dayStr.c_str() ); 
	
	cout << "Note where year, month, day, etc., "
		 << "occur in the following string ..." << endl << endl;
	cout << s2 << endl; 
	cout << "123456789012345678901234" << endl<<endl;   

	cout << "Now ... picking apart the string to reformat as ..." << endl;
	cout << mLongNameStr << " " << dayNum << ", " << yearStr<< " " << timeStr << endl;
	cout << "\nOr ..." << endl;
	cout << yearStr<< "-"<< monthNumStr << "-"<< dayStr<< " "<<timeStr<<endl;
	
	system("pause");
	return 0;  
}



Was This Post Helpful? 0
  • +
  • -

#13 David W  Icon User is offline

  • DIC supporter
  • Icon

Reputation: 130
  • Posts: 1,013
  • Joined: 20-September 08


Dream Kudos: 1075

Re: How to use ctime functions(time.h)

Posted 23 September 2008 - 02:55 PM

View Postsergio1, on 22 Sep, 2008 - 05:47 PM, said:

I made this just for you:

	SYSTEMTIME test;
	GetLocalTime(&test); // makes sure its GetLocalTime(&test) and not GetsystemTime(&test)
 ...  





// Another variation of date and time in C++ ...

#include <string>
#include <iostream>
#include <iomanip>	  // for setw(), setfill()
#include <windows.h>	// for SYSTEMTIME
using namespace std;

const string dayOfWeekName[] =
{
	"Sunday", "Monday", "Tuesday", "Wednesday", 
	"Thursday", "Friday", "Saturday"
};

int main()
{
	SYSTEMTIME t;
	// makes sure its GetLocalTime(&test) and not GetsystemTime(&test)
	GetLocalTime(&t); 
	int day = t.wDay;
	int month = t.wMonth;
	int dayOfWeekNum = t.wDayOfWeek;
	int year = t.wYear;
	int hour = t.wHour;
	int minute = t.wMinute;
	int second = t.wSecond;
	
	int adjHour = hour;
	string ampm = " AM";
	if (hour >= 12)
	{
		adjHour = hour - 12;
		ampm=" PM";
	}
	cout << "Today is " << dayOfWeekName[dayOfWeekNum] << " " 
		 << setfill('0')						// changes default '' until reset... 
		 << setw(2) << day << "-"			   // setw(x) needed for each ...
		 << setw(2) << month << "-" << year;	// year is already 4 digits
	cout << " and the local time right now is " 
		 << setw(2) << adjHour << ":" 
		 << setw(2) << minute << ":" 
		 << setw(2) << second << ampm <<". " << endl;
		 
	system("pause");
}



Was This Post Helpful? 1

#14 David W  Icon User is offline

  • DIC supporter
  • Icon

Reputation: 130
  • Posts: 1,013
  • Joined: 20-September 08


Dream Kudos: 1075

Re: How to use ctime functions(time.h)

Posted 27 November 2009 - 02:16 PM

To the person that just thanked me ... Thank You ... and you might like to see this update:

// Demo of C++ way to get date/time info and ...
// format as you like ( using #include <ctime> )
// Demo of table lookup and using stringstream

/*
    For BEGINNING COMPUTER PROGRAMMING using HLA, Python, C/C++
    goto [url="http://developers-heaven.net/forum/index.php/topic,46.0.html"]http://developers-heaven.net/forum/index.php/topic,46.0.html[/url]
*/

#include <iostream>
#include <ctime>
#include <sstream>
using namespace std;

const string monthsTable[] =
{
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    "01", "02", "03", "04", "05", "06",
    "07", "08", "09", "10", "11", "12",
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
};

const string daysTable[] =
{
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
    "Friday", "Saturday"
};

// If pass in 'Fri' ... finds 'n=5' and returns the string 'Friday' ...
string getLongWeekDayStr( string dStr )
{
    for( int n=0; n<7; ++n )
        if(daysTable[n] == dStr )
            return daysTable[7+n];
    return "";
}

// If pass in 'Sep' ... finds 'n=8' and returns the string '09' ...
string getMonthNumStr( string mStr )
{
    for( int n=0; n<12; ++n )
        if(monthsTable[n] == mStr )
            return monthsTable[12+n];
    return "";
}

// If pass in 'Sep' finds 'n=8' and returns the string 'September'
string getLongMonthStr( string mStr )
{
    for( int n=0; n<12; ++n )
        if(monthsTable[n] == mStr )
            return monthsTable[24+n];
    return "";
}


int main()
{
    time_t now = time(0);
    stringstream ss1;
    ss1 <<  ctime(&now); // "Fri Nov 27 16:22:24 2009"
    
    string weekDayStr, monthStr, mDayNumStr, timeStr, yearNumStr;

    // and ... picking apart ss1 gives ...
    ss1 >> weekDayStr >> monthStr >> mDayNumStr >> timeStr >> yearNumStr;
    
    // and adding some more formats to be available for output gives ...
    
    // string monthNumStr = getMonthNumStr(monthStr);   // 'Sep' gives '09'
    // string mLongNameStr = getLongMonthStr(monthStr); // 'Sep' gives 'September
    
    // convert mDayNumStr to an int to get rid of leading zeros
    stringstream ss2;
    ss2 << mDayNumStr;
    int dayNum;
    ss2 >> dayNum;

    cout << "Note order ... weekDay, month, mDayNum, time, yearNum ... in the\n"
         << "following string: " << ss1.str() << endl;

    cout << "\nNow, picking apart the string, we can reformat as ...\n\n"
         << getLongWeekDayStr( weekDayStr ) << ", "
         << getLongMonthStr( monthStr ) << " "
         << dayNum << ", "
         << yearNumStr<< " "
         << timeStr
         << "\n\n  or ...\n\n"
         << yearNumStr << "-"
         << getMonthNumStr( monthStr ) << "-"
         << mDayNumStr << " "
         << timeStr<<endl;

    cout << "\nPress 'Enter' to continue ... " << flush;
    cin.get();
}




and this ...


// Another variation of date and time in C++ ...

#include <string>
#include <iostream>
#include <iomanip>      // for setw(), setfill()
#include <windows.h>    // for SYSTEMTIME
using namespace std;

const string dayOfWeekName[] =
{
    "Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday"
};

const string monthName[] =
{
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
};



int main()
{
    SYSTEMTIME t;
    // makes sure it's GetLocalTime(&t) and not GetsystemTime(&t)
    GetLocalTime(&t);
    int year = t.wYear;
    int month = t.wMonth;
    int day = t.wDay;
    int dayOfWeek = t.wDayOfWeek;
    int hour = t.wHour;
    int minute = t.wMinute;
    int second = t.wSecond;

    int adjHour = hour;
    string ampm = " AM";
    if (hour >= 12)
    {
        adjHour = hour - 12;
        ampm=" PM";
    }
    
    cout << "The local date and time:\n\n"
         << dayOfWeekName[dayOfWeek] << ", "
         << monthName[month] << " "
         << day << ", "
         << year << " "
         << setfill('0')                // changes default '' until reset...
         << setw(2) << adjHour << ":"
         << setw(2) << minute << ":"
         << setw(2) << second << ampm
         << "\n\n  or ... \n\n"
         << year << "-"                 // year is already 4 digits
         << setw(2) << month << "-"     // setw(x) needed for each ...
         << setw(2) << day << " "
         << setw(2) << hour << ":"
         << setw(2) << minute << ":"
         << setw(2) << second << endl;

    cout << "\nPress 'Enter' to continue ... " << flush;
    cin.get();
}


This post has been edited by David W: 27 November 2009 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users