Skip to content

Add functions for time formatting #5

Closed

Description

@alt-graph

In the DOOCS clientlib we have functions for formatting local time:

/**
 * Convert a timestamp into local time and format it into a std::string using strftime()
 * format specifiers.
 *
 * Due to the limitations of strftime(), this function does not support sub-second
 * output precision.
 *
 * \param ts   The timestamp to be formatted.
 * \param fmt  A format string following the specifications of strftime(), see
 *             https://en.cppreference.com/w/cpp/chrono/c/strftime . If fmt is a null
 *             pointer, an empty string is returned. The default format generates an
               ISO 8601 compliant date-time string.
 * \returns a string with the formatted output or an empty string if an error occurred.
 */
std::string format_localtime (Timestamp ts, const char *fmt = "%Y-%m-%dT%H:%M:%S");
std::string format_localtime (std::time_t t, const char *fmt = "%Y-%m-%dT%H:%M:%S");

Such functions are useful also for other projects (both for UTC and local times). I'd say we could use them in GUL14.

Activity

  1. Finii commented on Jul 18, 2022

    @Finii
    Member

    Hmm, I'd like to see Howards date as a 'backport-ish' thing, like optional.
    It's single header. I usually just grab that one file and throw it into my source-tree, but would rather like to have it in GUL instead.

    Newer description with other date-time stuff here: https://github.com/HowardHinnant/date

    And it has subsecond resolution. Usually use it like this

        auto tp = system_clock::now();
        std::cout << tp << '\n';
        2015-04-19 18:24:42.770911
    

    Of course you can generate the unreadable ISO 8601 or, as I prefer RFCs over ISO, RFC 3389 strings.

    So, if you look again at the RFC document. RFC 3339 use/follow the ISO 8601 profile for the Internet DateTime. Clearly stated in chapter 5.6

    So actually, there are no big differences between Date ISO 8601. The only small thing that makes these 2 different is the “T” syntax between date and time. ISO 8601 uses the “T” character to separate the date and time. In RFC 3339, you can replace the “T” character with only using space.

    For example:

    # This is acceptable in ISO 8601 and RFC 3339 (with T)
    2019-10-12T07:20:50.52Z
    # This is only accepted in RFC 3339 (without T)
    2019-10-12 07:20:50.52Z
    

    Source here for an interested reader

    Of course any formatting is possible, as with the C style format string, by std::formatter I mean, no that is C++20, but date::format

    If we need timezones, that is another header file. I never used this.

  2. locked and limited conversation to collaborators on Jul 19, 2022
  3. converted this issue into a discussion #7 on Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions