xref: /openbmc/bmcweb/features/redfish/include/utils/time_utils.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3081ebf06SWludzik, Jozef #pragma once
4081ebf06SWludzik, Jozef 
5a93e9c77SEd Tanous #include "http_response.hpp"
64dbb8aeaSWludzik, Jozef 
7081ebf06SWludzik, Jozef #include <chrono>
8*d7857201SEd Tanous #include <cstdint>
9*d7857201SEd Tanous #include <ctime>
104dbb8aeaSWludzik, Jozef #include <optional>
11*d7857201SEd Tanous #include <ratio>
12081ebf06SWludzik, Jozef #include <string>
139ea15c35SEd Tanous #include <string_view>
14*d7857201SEd Tanous #include <utility>
15081ebf06SWludzik, Jozef 
16081ebf06SWludzik, Jozef namespace redfish
17081ebf06SWludzik, Jozef {
18081ebf06SWludzik, Jozef 
19081ebf06SWludzik, Jozef namespace time_utils
20081ebf06SWludzik, Jozef {
21081ebf06SWludzik, Jozef 
22081ebf06SWludzik, Jozef /**
234dbb8aeaSWludzik, Jozef  * @brief Convert string that represents value in Duration Format to its numeric
244dbb8aeaSWludzik, Jozef  *        equivalent.
254dbb8aeaSWludzik, Jozef  */
26a93e9c77SEd Tanous std::optional<std::chrono::milliseconds> fromDurationString(std::string_view v);
274dbb8aeaSWludzik, Jozef 
284dbb8aeaSWludzik, Jozef /**
29081ebf06SWludzik, Jozef  * @brief Convert time value into duration format that is based on ISO 8601.
30081ebf06SWludzik, Jozef  *        Example output: "P12DT1M5.5S"
31081ebf06SWludzik, Jozef  *        Ref: Redfish Specification, Section 9.4.4. Duration values
32081ebf06SWludzik, Jozef  */
33a93e9c77SEd Tanous std::string toDurationString(std::chrono::milliseconds ms);
34081ebf06SWludzik, Jozef 
35a93e9c77SEd Tanous std::optional<std::string> toDurationStringFromUint(uint64_t timeMs);
362b82937eSEd Tanous 
372b82937eSEd Tanous // Returns the formatted date time string.
382b82937eSEd Tanous // Note that the maximum supported date is 9999-12-31T23:59:59+00:00, if
392b82937eSEd Tanous // the given |secondsSinceEpoch| is too large, we return the maximum supported
402b82937eSEd Tanous // date.
41a93e9c77SEd Tanous std::string getDateTimeUint(uint64_t secondsSinceEpoch);
422b82937eSEd Tanous 
432b82937eSEd Tanous // Returns the formatted date time string with millisecond precision
442b82937eSEd Tanous // Note that the maximum supported date is 9999-12-31T23:59:59+00:00, if
452b82937eSEd Tanous // the given |secondsSinceEpoch| is too large, we return the maximum supported
462b82937eSEd Tanous // date.
47a93e9c77SEd Tanous std::string getDateTimeUintMs(uint64_t milliSecondsSinceEpoch);
482b82937eSEd Tanous 
492b82937eSEd Tanous // Returns the formatted date time string with microsecond precision
50a93e9c77SEd Tanous std::string getDateTimeUintUs(uint64_t microSecondsSinceEpoch);
512b82937eSEd Tanous 
52a93e9c77SEd Tanous std::string getDateTimeStdtime(std::time_t secondsSinceEpoch);
532b82937eSEd Tanous 
542b82937eSEd Tanous /**
552b82937eSEd Tanous  * Returns the current Date, Time & the local Time Offset
568ece0e45SEd Tanous  * information in a pair
572b82937eSEd Tanous  *
582b82937eSEd Tanous  * @param[in] None
592b82937eSEd Tanous  *
602b82937eSEd Tanous  * @return std::pair<std::string, std::string>, which consist
612b82937eSEd Tanous  * of current DateTime & the TimeOffset strings respectively.
622b82937eSEd Tanous  */
63a93e9c77SEd Tanous std::pair<std::string, std::string> getDateTimeOffsetNow();
642b82937eSEd Tanous 
65c51afd54SEd Tanous using usSinceEpoch = std::chrono::duration<int64_t, std::micro>;
661b8b02a4SEd Tanous std::optional<usSinceEpoch> dateStringToEpoch(std::string_view datetime);
67b5190062SHieu Huynh 
68b5190062SHieu Huynh /**
69b5190062SHieu Huynh  * @brief Returns the datetime in ISO 8601 format
70b5190062SHieu Huynh  *
71b5190062SHieu Huynh  * @param[in] std::string_view the date of item manufacture in ISO 8601 format,
72b5190062SHieu Huynh  *            either as YYYYMMDD or YYYYMMDDThhmmssZ
73b5190062SHieu Huynh  * Ref: https://github.com/openbmc/phosphor-dbus-interfaces/blob/master/yaml/
74b5190062SHieu Huynh  *      xyz/openbmc_project/Inventory/Decorator/Asset.interface.yaml#L16
75b5190062SHieu Huynh  *
76b5190062SHieu Huynh  * @return std::string which consist the datetime
77b5190062SHieu Huynh  */
78a93e9c77SEd Tanous std::optional<std::string> getDateTimeIso8601(std::string_view datetime);
79b5190062SHieu Huynh 
80b5190062SHieu Huynh /**
81b5190062SHieu Huynh  * @brief ProductionDate report
82b5190062SHieu Huynh  */
83a93e9c77SEd Tanous void productionDateReport(crow::Response& res, const std::string& buildDate);
84b5190062SHieu Huynh 
85081ebf06SWludzik, Jozef } // namespace time_utils
86081ebf06SWludzik, Jozef } // namespace redfish
87