Skip to content
ZhreShold edited this page Sep 3, 2015 · 4 revisions

##Date and timer utility classes. namespace zz::time::

Date

zz::time::Date class is used for calendar events, such as year, month, week, hour, minute, etc...

  • Date support local time zone and UTC time zone.
  • Date::to_string() function support various formats.
Example code
// show current date in local time zone
zz::time::Date date;
std::cout << "Local time(Pacific) " << date.to_string() << std::endl;
// convert to UTC time
date.to_utc_time();
std::cout << "UTC time " << date.to_string() << std::endl;
// customize to_string format
auto str = date.to_string("%m/%d %a %H:%M:%S.%frac");
std::cout << "With format [%m / %d %a %H:%M : %S.%frac] : " << str << std::endl;
str = date.to_string("%c");
std::cout << "With format %c(standard, local specific): " << str << std::endl;
// another way is using static function
std::cout << "Call UTC directly " << zz::time::Date::utc_time().to_string() << std::endl;

#####Output:

Local time(Pacific) 15-08-26 16:48:03.520
UTC time 15-08-26 23:48:03.520
With format [%m / %d %a %H:%M : %S.%frac] : 08/26 Wed 23:48:03.520
With format %c(standard, local specific): 08/26/15 23:48:03
Call UTC directly 15-08-26 23:48:03.521
Supported format specifier

Please check std::put_time documentation for more specifiers.

Specifier Description
%frac fraction of time less than a second, 3 digits
%y writes last 2 digits of year as a decimal number (range [00,99])
%m writes month as a decimal number (range [01,12])
%d writes day of the month as a decimal number (range [01,31])
%a writes abbreviated weekday name, e.g. Fri (locale dependent)
%H writes hour as a decimal number, 24 hour clock (range [00-23])
%M writes minute as a decimal number (range [00,59])
%S writes second as a decimal number (range [00,60])
... many more options, check std::put_time documentation
%% use %% to skip %

Timer

zz::time::Timer class is high resolution timer mainly used for measuring elapsed time.

  • Timer support various quantization level, from nanosecond to second.
  • Timer also support double precision in nanosecond resolution(System dependent).
Example Code
const int repeat = 999999;
// create a timer
zz::time::Timer t;
// time consuming function
long long sum = 0;
for (int i = 0; i < repeat; ++i) sum += i;
std::cout << "Summation: " << sum << " elapsed time: " << t.to_string() << std::endl;
// reset timer, start new timer
t.reset();
// another time consuming function
for (int i = 0; i < repeat; ++i) sum -= i;
// use formatter
std::cout << "Subtraction: " << sum << t.to_string(" elapsed time: [%us us]") << std::endl;
// different quantization
std::cout << "sec: " << t.elapsed_sec() << std::endl;
std::cout << "msec: " << t.elapsed_ms() << std::endl;
std::cout << "usec: " << t.elapsed_us() << std::endl;
std::cout << "nsec: " << t.elapsed_ns() << std::endl;
// use double, no quantize
std::cout << "sec in double: " << t.elapsed_sec_double() << std::endl;
// sleep for 2000 ms
zz::time::sleep(2000);
std::cout << "After sleep for 2 sec: " << t.elapsed_sec_double() << std::endl;

#####Output

Summation: 499998500001 elapsed time: [3 ms]
Subtraction: 0 elapsed time: [3002 us]
sec: 0
msec: 3
usec: 3002
nsec: 3002300
sec in double: 0.0030023
After sleep for 2 sec: 2.00421
Supported format specifier
Specifier Description
%sec quantize in second resolution
%ms quantize in millisecond resolution
%us quantize in microsecond resolution
%ns quantize in nanosecond resolution
%% skip %
Clone this wiki locally