Skip to content
Joshua Z. Zhang edited this page Aug 6, 2017 · 4 revisions

DateTime

zz::time::DateTime 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
time::DateTime date;
std::cout << "Local time(Pacific): " << date.to_string() << std::endl;
// or UTC time
date.to_utc_time();
std::cout << "UTC time: " << date.to_string() << std::endl;
// another way is using static function
std::cout << "Call UTC directly: " << zz::time::DateTime::utc_time().to_string() << std::endl;
	
// customize to_string format
std::string str = date.to_string("%m/%d %a Hour:%H, Min:%M, Second:%S");
std::cout << "With format '%m/%d %a Hour:%H, Min:%M, Second:%S': " << str << std::endl;
str = date.to_string("%c");
std::cout << "With format %c(standard, locale dependent): " << str << std::endl;

Output:

Local time(Pacific): 15-09-14 16:10:37.255
UTC time: 15-09-14 23:10:37.255
Call UTC directly: 15-09-14 23:10:37.255
With format '%m/%d %a Hour:%H, Min:%M, Second:%S': 09/14 Mon Hour:23, Min:10, Second:37
With format %c(standard, locale dependent): 09/14/15 23:10:37

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

// create a timer before time consuming function
time::Timer t;
costy_function();
std::cout << "func1 elapsed time: " << t.to_string() << std::endl;

t.reset(); // reset timer, start new timer
costy_function2(); // another function
std::cout << t.to_string("func2 elapsed time: [%us us]") << std::endl; // use formatter
// pause timer
t.pause();	
// different quantizations
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 200 ms
t.resume();	// resume timer recording
zz::time::sleep(500);
std::cout << "After sleep for 0.5 sec: " << t.elapsed_sec_double() << std::endl;

Output

func1 elapsed time: [46 ms]
func2 elapsed time: [46857 us]
sec: 0
msec: 46
usec: 46857
nsec: 46857700
sec in double: 0.0468577
After sleep for 0.5 sec: 0.562546

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 %