Tux
Tux

Reputation: 1916

Alternatives to <ctime>

I'm looking for a C++11 version of the <ctime> library. Were any like this included with C++11?

EDIT: Anything with more functionality would be perfect!

EDIT 2: I'm looking to use this with a game I'm making so that I can keep track of total time played. Anything that would help me with that is what I'm looking for.

Upvotes: 6

Views: 6652

Answers (2)

Qaz
Qaz

Reputation: 61970

C++11 includes the <chrono> header, which provides different types of clocks (I'll use the high resolution one), that have a now function. You can subtract two of these times received from now() to get the total number of <unit>s (I'll use seconds) between them:

using clock = std::chrono::high_resolution_clock;
using unit = std::chrono::seconds;
std::chrono::time_point<clock> startTime = clock::now(); //get starting time

... //do whatever stuff you have to do

std::chrono::time_point<clock> thisTime = clock::now();
long long secondsElapsed = std::chrono::duration_cast<unit>(thisTime - startTime).count();

//now use secondsElapsed however you wish
//you can also use other units, such as milliseconds or nanoseconds

Do note, however, that secondsElapsed is not guaranteed to be positive unless the is_steady member of the clock is true because that member being true means that a subsequent call to now() will give a larger number than a former call to now().

Upvotes: 11

jogojapan
jogojapan

Reputation: 70007

Many of the functions in <ctime>, notably the ctime function itself, have to do with formatting dates and times as strings.

C++11 provides a new io-manipulator std::put_time, which indeed replaces C-style functions, and is fully compatible with the locale-related features of C++.

Specifically, given a time-point in C-style tm format:

std::time_t t = std::time(NULL);
std::tm tm = *std::localtime(&t);

std::put_time enables printing this according to any chosen locale, if locale-specific formatting parameters, such as %c (locale-specific date/time), %Ec (locale specific, extended date/time, such as imperial years for Japan), or %Z (time-zone) are used:

std::cout.imbue(std::locale("ja_JP.utf8"));
std::cout << "ja_JP: " << std::put_time(&tm, "%c %Z") << '\n';
std::cout << "ja_JP: " << std::put_time(&tm, "%Ec %Z") << '\n';

These calls print something like:

2012年11月15日 11時49分04秒 JST     // first call
平成24年11月15日 10時49分05秒 JST   // second call

The time-point retrieval functions from <chrono>, mentioned in the other answer, can also be converted to a tm struct, using the to_time_t method, and then used together with put_time. This makes the code independent of any C-style function calls, at least on the surface:

using namespace std;
auto now = chrono::system_clock::now();

time_t now_c = chrono::system_clock::to_time_t(now);
cout << "Locale-specific time now: "
     << put_time(localtime(&now_c), "%c %Z") << '\n';

Combined with <chrono> duration types, this gives great flexibility in calculating and printing dates and times:

time_t now_c = chrono::system_clock::to_time_t(now - chrono::hours(48));
cout << "Locale-specific time on the day before yesterday: "
     << put_time(localtime(&now_c), "%c %Z") << '\n';

These are the headers you need for all the function calls above:

#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>

Availability note I am unsure about MSVC and Clang, but GCC, unfortunately, does not provide the std::put_time function just yet: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54354.

Upvotes: 9

Related Questions