| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | timestamp.hpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains timestamp and duration helper function declarations. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | |
| 17 | #include <chrono> |
| 18 | #include <optional> |
| 19 | #include <string> |
| 20 | |
| 21 | namespace wsl::windows::common::timestamp { |
| 22 | |
| 23 | // Expands a partial timestamp into a full RFC 3339 one. Hour-only, minute-only and date-only values |
| 24 | // are padded out to a complete time, and a value with no zone designator is resolved against the |
| 25 | // offset currently in effect locally. The input is not validated, so an unrecognized value is |
| 26 | // expanded as-is and left for the parser to reject. |
| 27 | std::string ExpandToRfc3339(const std::string& timestamp); |
| 28 | |
| 29 | // Converts an RFC 3339 timestamp to seconds since the unix epoch. Accepts a 'Z' designator or a |
| 30 | // numeric +HH:MM offset, with optional fractional seconds. Timestamps that predate the epoch convert |
| 31 | // to a negative value. Throws E_INVALIDARG if the timestamp is malformed, names an invalid date, or |
| 32 | // has trailing characters. |
| 33 | std::int64_t Rfc3339ToEpoch(const std::string& timestamp); |
| 34 | |
| 35 | // Parses a Go duration such as "1h30m", "-1.5h" or "300ms": an optional sign followed by one or more |
| 36 | // decimal values that each carry a unit of ns, us, ms, s, m or h. Returns nothing if the value does |
| 37 | // not match that grammar or overflows. |
| 38 | std::optional<std::chrono::nanoseconds> TryParseDuration(const std::string& duration); |
| 39 | |
| 40 | // Renders seconds since the unix epoch in the local time zone, using the layout |
| 41 | // "2006-01-02 15:04:05 -0700 MST". Falls back to UTC when the time zone database is unavailable. |
| 42 | std::string EpochToLocalDisplayTime(LONGLONG timestamp); |
| 43 | |
| 44 | // Renders an RFC 3339 timestamp in the same layout, but as UTC and with its fractional seconds |
| 45 | // preserved. An empty input returns an empty string; anything else that cannot be parsed throws. |
| 46 | std::string Rfc3339ToUtcDisplayTime(std::string_view timestamp); |
| 47 | |
| 48 | // Renders an elapsed number of seconds as a coarse, localized description such as "About a minute" |
| 49 | // or "3 weeks". Negative values are treated as zero. |
| 50 | std::wstring FormatElapsedSeconds(LONGLONG elapsedSeconds); |
| 51 | |
| 52 | // The invariant English form of FormatElapsedSeconds, matching the strings docker produces through |
| 53 | // go-units HumanDuration. Machine readable output uses this so its values do not vary by display |
| 54 | // language. |
| 55 | std::wstring FormatInvariantElapsedSeconds(LONGLONG elapsedSeconds); |
| 56 | |
| 57 | // Renders how long ago a timestamp given in seconds since the unix epoch occurred. A timestamp of |
| 58 | // zero means "unset" and returns an empty string. |
| 59 | std::wstring FormatRelativeTime(LONGLONG timestamp); |
| 60 | |
| 61 | // The invariant English form of FormatRelativeTime. |
| 62 | std::wstring FormatInvariantRelativeTime(LONGLONG timestamp); |
| 63 | |
| 64 | } // namespace wsl::windows::common::timestamp |