11

How can I print the current time using the std::chrono library?

I would like the format to be: hour:minute:second:millisecond:microsecond:nanosecond.

425nesp
  • 6,936
  • 9
  • 50
  • 61
user3665224
  • 1,349
  • 3
  • 16
  • 34

1 Answers1

24

[EDIT]

Changing the code to C++ style:

#include <chrono>
#include <ctime>

std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();

typedef std::chrono::duration<int, std::ratio_multiply<std::chrono::hours::period, std::ratio<8>
>::type> Days; /* UTC: +8:00 */

Days days = std::chrono::duration_cast<Days>(duration);
    duration -= days;
auto hours = std::chrono::duration_cast<std::chrono::hours>(duration);
    duration -= hours;
auto minutes = std::chrono::duration_cast<std::chrono::minutes>(duration);
    duration -= minutes;
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
    duration -= seconds;
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
    duration -= milliseconds;
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(duration);
    duration -= microseconds;
auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(duration);

std::cout << hours.count() << ":"
          << minutes.count() << ":"
          << seconds.count() << ":"
          << milliseconds.count() << ":"
          << microseconds.count() << ":"
          << nanoseconds.count() << std::endl;

Result:

17:56:14:94:451:679

[Old]

A simply example:

#include <sys/time.h>

char fmt[64];
char buf[64];
struct timeval tv;
struct tm *tm;

gettimeofday (&tv, NULL);
tm = localtime (&tv.tv_sec);
strftime (fmt, sizeof (fmt), "%H:%M:%S:%%06u", tm);
snprintf (buf, sizeof (buf), fmt, tv.tv_usec);
printf ("%s\n", buf);

Result:

00:01:32:695240
Kir Chou
  • 2,980
  • 1
  • 36
  • 48
  • Thank you. But, may i please know what is %%06u. Is it possible to get microseconds in same format? – user3665224 Dec 28 '14 at 16:13
  • 9
    —1: Where is the `std::chrono` usage here? This is some C nonsense from 120 years ago! – Lightness Races in Orbit Dec 28 '14 at 16:14
  • @LightnessRacesinOrbit: Thanks, the code is really old. I wrote it few years ago. I already modified the code into new C++11 style. – Kir Chou Dec 28 '14 at 18:06
  • You don't like C++ very much, do you? :) +1 – Armen Tsirunyan Dec 28 '14 at 18:06
  • Right, I usually write C/script language to solve system problem. – Kir Chou Dec 28 '14 at 18:07
  • Ok this is much better now. You should explain how displaying nanoseconds places doesn't actually mean the clock has nanosecond accuracy (it almost certainly does not), so many of these digits will be completely meaningless. – Lightness Races in Orbit Dec 28 '14 at 18:45
  • @LightnessRacesinOrbit Because in OS, time is maintained by `struct timeval` which only consist seconds(tv_sec) and microseconds(tv_usec). Consequently, it means nanoseconds in a high-level language level is totally meaningless. – Kir Chou Dec 28 '14 at 18:54
  • Can you please confirm if "17:56:14:94:451:679" is the local system time? When i run the same code, I get some time but that is not my system time. For instance, my local time is 00:25:9:40:240:900 (approx time). But this program gives 18:55:9:40:830:845 – user3665224 Dec 28 '14 at 19:01
  • 1
    @user3665224 `time_point` will return the value from (1970.01.01 00:00:00 UTC) to present. To be human-readable, value should minus (Today 00:00 UTC). You need to manipulate it by yourself, because I don't know your UTC time. – Kir Chou Dec 28 '14 at 19:08
  • @KirChou: Um, yes, _I_ know that. What I meant was, you should add this into the answer because at the moment the answer doesn't even briefly mention it. – Lightness Races in Orbit Dec 28 '14 at 21:34
  • @KirChou: Those two timestamps are blatantly not offset by any known national timezone. – Lightness Races in Orbit Dec 28 '14 at 21:34
  • so 20 lines of code and dozen allocations is now preferred solution to 4 lines of code because its `old` ? good to know – Boppity Bop Apr 09 '22 at 12:12