Home > Guides > Programming > C/C++

Retrieving the Current Time

Last updated Apr 25, 2003.

The function time() retrieves the current calendar time from the system's clock. It has the following prototype:

time_t time(time_t * tp);

In systems in which no clock is available, the function returns -1. If tp is not null, the value is also written to *tp. The following program retrieves the current time and displays it in its raw format:

#include <ctime>
using namespace std;
int main()
{
 time_t curr=time(0);
 cout << "current time is: " << curr <<endl;
}

The output is a number such as 980898685. This is the number of seconds that have elapsed since the epoch. As you can see, time_t isn't a human-readable format. To present the current time and date in a human-readable format you have to convert it to a string using the ctime() function, which is declared as follows:

char * ctime(const time_t * tp);

This function returns a null-terminated string of the form:

Wed Jan 31 01:51:25 2001\n\0 

To break the time into individual constituents such as year, month, and day of the week, use the tm struct:

struct tm
{
 int tm_sec; //seconds after the minute (0-61)
 int tm_min; //minutes after the hour (0-59)
 int tm_hour; //hours since midnight (0-23)
 int tm_mday; //day of the month (1-31)
 int tm_mon; // months since January (0-11)
 int tm_year; // elapsed years since 1900 
 int tm_wday; // days since Sunday (0-6)
 int tm_yday; //days since January 1st (0-365)
 int tm_isdst; //1 if daylight savings is on, zero if not,
        //-1 if unknown
};

Remember: the key to manipulating date and time is knowing how to convert time_t to tm and vice versa. To fill a tm struct with the local time, use the function localtime():

struct tm* localtime (const time_t *pt);

localtime() takes a pointer to a valid time_t object, converts it to a local static tm struct and returns its address. Note that subsequent invocations of localtime() override the previous value of its local static tm object. Therefore, you should copy the result immediately to your own tm object. The following program fills a tm object with the current local time:

#include <ctime>
using namespace std;
int main()
{
 time_t curr;
 tm local;
 time(&curr); // get current time_t value
 local=*(localtime(&curr)); // dereference and assign
}

To convert a tm struct to time_t, use the mktime() function:

time_t curr;
tm local;
time(&curr); // get current time_t value
local=*(localtime(&curr)); // dereference and assign
time_t temp=mktime(&local); // temp and curr are equal

Discussions

Bugzilla
Posted Nov 18, 2008 01:53 AM by cupu
2 Replies
auto_ptr issues
Posted Sep 14, 2007 07:43 AM by singh_siddhu
1 Replies
i want c++ book through net
Posted Aug 23, 2007 11:13 PM by harivilu
3 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevBjarne Stroustrups's Stevens Talk
By Danny KalevDecember 7, 2009 No Comments

On 2nd December Bjarne Stroustrup delivered a talk about the standardization process of C++0x at the Stevens institute. Here some of the key points from his talk.

Danny KalevMinutes from the October 2009 Meeting
By Danny KalevNovember 19, 2009 No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny KalevOctober 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

See More Blogs

Informit Network