0

For a given duration of 203443 milliseconds (this is 3 minutes, 23 seconds and 443 milliseconds), a pattern like e.g.

This took about' m 'minutes and' s 'seconds.

would produce the following formatted output:

This took about 3 minutes and 23 seconds.

It is different from format timestamp to current date-time. Is there any C++ standard Library (under C++14) or a solution that I can follow. I'm new to C++.

JeJo
  • 30,635
  • 6
  • 49
  • 88
Nguyễn Đức Tâm
  • 1,017
  • 2
  • 10
  • 24
  • Do you have a timestamp or a duration? They are different things (different classes in the standard library). And what *do* you have? Please try to create a [mcve] to show what you have and what you're trying to do, and tell us what problems you have with that example. And perhaps take some to read or refresh [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Nov 05 '19 at 09:07
  • I think that I have a duration. – Nguyễn Đức Tâm Nov 05 '19 at 09:08
  • I didn't come up with any solution so that I made a question here. – Nguyễn Đức Tâm Nov 05 '19 at 09:09
  • Does this answer your question? [Convert milliseconds to hours:minutes:seconds:milliseconds in C++](https://stackoverflow.com/questions/50727304/convert-milliseconds-to-hoursminutessecondsmilliseconds-in-c) – sephiroth Nov 05 '19 at 09:14
  • @sephiroth yes, but my milliseconds need to convert to years:months:days:hours:minutes:seconds: milliseconds. I have already read that post. – Nguyễn Đức Tâm Nov 05 '19 at 09:26

1 Answers1

1
#include <chrono>
#include <iostream>

int
main()
{
    using namespace std::chrono;
    auto d = 203443ms;
    auto m = duration_cast<minutes>(d);
    d -= m;
    auto s = duration_cast<seconds>(d);
    std::cout << "This took about " << m.count() << " minutes and "
                                    << s.count() << " seconds.\n";
}
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • Can I convert milliseconds to years:months:days:hours:minutes:seconds: milliseconds? Using what you just showed me?? – Nguyễn Đức Tâm Nov 05 '19 at 09:56
  • You seem like a master about the date-time format library in C++. – Nguyễn Đức Tâm Nov 05 '19 at 09:56
  • Yes. However the duration type aliases `years`, `months`, and `days` don't exist in C++14. But you can create such duration as is done here: https://github.com/HowardHinnant/date/blob/master/include/date/date.h *However*, `years` and `months` duration types would be _average_ years and months in length. So the "about" in your message would be apropos. – Howard Hinnant Nov 05 '19 at 10:00
  • I have visited your GitHub a thousand times, but I'm not allowed to use open source just only standard library. Thanks for your help. – Nguyễn Đức Tâm Nov 05 '19 at 10:03
  • You can just write your own `years`, `months` and `days` type aliases using mine as a guide. I can't claim a copyright on a typedef. – Howard Hinnant Nov 05 '19 at 10:04
  • 1
    Yes, that what I'm doing right now. Actually my problem is with day month and year, the other types already solved. – Nguyễn Đức Tâm Nov 05 '19 at 10:06