- Overview
- Table of Contents
- Special Member Functions: Constructors, Destructors, and the Assignment Operator
- Operator Overloading
- Memory Management
- Templates
- Namespaces
- Time and Date Library
- Streams
- Object-Oriented Programming and Design Principles
- The Standard Template Library (STL) and Generic Programming
- Exception Handling
- Runtime Type Information (RTTI)
- Signal Processing
- Creating Persistent Objects
- Bit Fields
- New Cast Operators
- Environment Variables
- Variadic Functions
- Pointers to Functions
- Function Objects
- Pointers to Members
- Lock Files
- Design Patterns
- Dynamic Linking
- Tips and Techniques
- A Tour of C99
- C++0X: The New Face of Standard C++
- C++0x Concurrency
- The Reflecting Circle New
- We Have Mail New
- The Soapbox
- Numeric Types and Arithmetic
- Careers
- Locales and Internationalization
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


Account Sign In
View your cart