3

Here's an example adapted from cppreference.com :

#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>

int main() {
    std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
    std::time_t now_c = std::chrono::system_clock::to_time_t(now);
    std::cout << "The time was just "
              << std::put_time(std::localtime(&now_c), "%F %T") << '\n';
}

I don't like this. I want to print my time point without having to go through time_t. Can I do so...:

  1. at all?
  2. with an arbitrary format like put_time supports?

Notes:

  • Standard library solutions are best; non-standard but robust solutions are also relevant.
  • Not a dupe of this - since I'm also interested in arbitrary format printing; but it should be noted that @HowardHinnant's answer to that one resolves the first part of this one.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • @inf: Seriously, did you read the two questions before marking as a dupe? Now, it's true that one of the _answers_ to that question also answers this one, but you wouldn't know it from reading the question or most of the answers. – einpoklum Jul 21 '18 at 21:15
  • So all good then? – Stephan Dollberg Jul 21 '18 at 21:16
  • @inf: No, see my first answer. – einpoklum Jul 21 '18 at 21:19
  • The answer to your questions is that you obviously can't. Otherwise those other answers would be outdated. The other question asks exactly the same question of how to print a time point. As depicted in the answers using time_t is the way to go. You should also read the comments regarding standardisation of a nicer way. – Stephan Dollberg Jul 21 '18 at 21:25
  • 1
    I reopened this answer. The previously indicated dupe did not have the "without going through a `time_t` clause, which is significant because the trip through `time_t` looses fractional second information. – Howard Hinnant Jul 22 '18 at 03:27
  • @inf: You (and other close-voters) were too quick to judge my question. More generally, we should not use overly expansive criteria to decide a question is a dupe. If two questions have a partial answer share, that's not enough. Please be more patient with related questions and consider question asker's perception of dupeness more. – einpoklum Jul 22 '18 at 09:09
  • Nope, if you would have read the question & answers of the other questions you would have found Howard's library as well. Also if you are looking for a non-standard solution you should say so. – Stephan Dollberg Jul 22 '18 at 09:14
  • 1
    @inf: 1. A person looking for an answer to my question is not likely / not likely enough to find the other question + think the answers would address their question + scour everything + follow links to see whether the information there solves their question. There are multiple highly-voted question pairs which meet your criterion for dupeness. 2. About your second point - fair enough, but even if I had said "standard solution only", it is still worthwhile having that part of the question answered by "no solution in the standard library up to C++17". – einpoklum Jul 22 '18 at 09:18

2 Answers2

7

Howard Hinnant's library - which has been voted to become part of C++20 - also supports put_time-like formatting.

#include "date/date.h"
#include <iostream>

int
main()
{
    std::cout << date::format("%m/%d/%Y %I:%M:%S %p\n", std::chrono::system_clock::now());
}

Example output:

07/22/2018 03:30:35.001865 AM
einpoklum
  • 118,144
  • 57
  • 340
  • 684
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
1

A partial solution which doesn't allow you a choice of printing format or timezone, thanks to @inf's suggestion, is found in this answer: you can simply pipe a timepoint to the standard output to get a UTC timestamp string for it:

std::cout << std::chrono::system_clock::now() << " UTC\n";

but the question remains open for arbitrary formats.

einpoklum
  • 118,144
  • 57
  • 340
  • 684