Crisoberillo
Crisoberillo

Reputation: 301

How to concatenate a string with chrono::milliseconds?

I need to have a string with timestamp in milliseconds in it. I got the milliseconds that way (after looking for it here on stackoverflow):

milliseconds ms = duration_cast< milliseconds >(
    system_clock::now().time_since_epoch()
);

now I have to concatenate it like:

string = "something " + ms + " something else";

Any help? Thank you in advance :)

Upvotes: 8

Views: 14778

Answers (2)

NathanOliver
NathanOliver

Reputation: 180435

You need a way to convert ms into a string. The standard has std::to_string() but that wont work directly with a duration. To convert the duration to a integral type that to_string() can use you need to use the count() function

string = "something " + std::to_string(ms.count()) + " something else";

Upvotes: 18

user4180612
user4180612

Reputation:

Use count method and std::to_string. Example:

string = "something " + std::to_string(ms.count()) + " something else"

Upvotes: 4

Related Questions