Skip to content

formatting chrono with precision #3219

Description

@brevzin
#include <chrono>
#include <fmt/chrono.h>

int main() {
    using namespace std::chrono;

    // this one prints 1.00s, as expected
    fmt::print("{:.2}\n", duration<double>(1.0));

    // this one prints 1s but shouldn't compile
    fmt::print("{:.2}\n", duration<int>(1));

    // these both print .2
    fmt::print("{:.2}\n", time_point<system_clock, duration<double>>(duration<double>(1.0)));
    fmt::print("{:.2}\n", time_point<system_clock, duration<int>>(duration<int>(1)));
}

If we look at the standard spec, it's clear that using precision with a duration is only valid for floating point (https://eel.is/c++draft/time.format#1.sentence-4), so the second call there should be rejected.

There's some ambiguity in the standard spec right now about what formatting a time_point<system_clock, duration<int>> with {:.2} actually means (valid or not? probably not?) but either way, printing .2 in either case is definitely incorrect.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions