xref: /openbmc/bmcweb/features/redfish/include/utils/time_utils.hpp (revision a93e9c77c09e4429b5c447dbc1480278bb2a28e3)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3081ebf06SWludzik, Jozef #pragma once
4081ebf06SWludzik, Jozef 
5*a93e9c77SEd Tanous #include "http_response.hpp"
64dbb8aeaSWludzik, Jozef #include "logging.hpp"
74dbb8aeaSWludzik, Jozef 
8081ebf06SWludzik, Jozef #include <chrono>
99ea15c35SEd Tanous #include <cstddef>
104dbb8aeaSWludzik, Jozef #include <optional>
11081ebf06SWludzik, Jozef #include <string>
129ea15c35SEd Tanous #include <string_view>
13081ebf06SWludzik, Jozef 
14081ebf06SWludzik, Jozef namespace redfish
15081ebf06SWludzik, Jozef {
16081ebf06SWludzik, Jozef 
17081ebf06SWludzik, Jozef namespace time_utils
18081ebf06SWludzik, Jozef {
19081ebf06SWludzik, Jozef 
20081ebf06SWludzik, Jozef /**
214dbb8aeaSWludzik, Jozef  * @brief Convert string that represents value in Duration Format to its numeric
224dbb8aeaSWludzik, Jozef  *        equivalent.
234dbb8aeaSWludzik, Jozef  */
24*a93e9c77SEd Tanous std::optional<std::chrono::milliseconds> fromDurationString(std::string_view v);
254dbb8aeaSWludzik, Jozef 
264dbb8aeaSWludzik, Jozef /**
27081ebf06SWludzik, Jozef  * @brief Convert time value into duration format that is based on ISO 8601.
28081ebf06SWludzik, Jozef  *        Example output: "P12DT1M5.5S"
29081ebf06SWludzik, Jozef  *        Ref: Redfish Specification, Section 9.4.4. Duration values
30081ebf06SWludzik, Jozef  */
31*a93e9c77SEd Tanous std::string toDurationString(std::chrono::milliseconds ms);
32081ebf06SWludzik, Jozef 
33*a93e9c77SEd Tanous std::optional<std::string> toDurationStringFromUint(uint64_t timeMs);
342b82937eSEd Tanous 
352b82937eSEd Tanous // Returns the formatted date time string.
362b82937eSEd Tanous // Note that the maximum supported date is 9999-12-31T23:59:59+00:00, if
372b82937eSEd Tanous // the given |secondsSinceEpoch| is too large, we return the maximum supported
382b82937eSEd Tanous // date.
39*a93e9c77SEd Tanous std::string getDateTimeUint(uint64_t secondsSinceEpoch);
402b82937eSEd Tanous 
412b82937eSEd Tanous // Returns the formatted date time string with millisecond precision
422b82937eSEd Tanous // Note that the maximum supported date is 9999-12-31T23:59:59+00:00, if
432b82937eSEd Tanous // the given |secondsSinceEpoch| is too large, we return the maximum supported
442b82937eSEd Tanous // date.
45*a93e9c77SEd Tanous std::string getDateTimeUintMs(uint64_t milliSecondsSinceEpoch);
462b82937eSEd Tanous 
472b82937eSEd Tanous // Returns the formatted date time string with microsecond precision
48*a93e9c77SEd Tanous std::string getDateTimeUintUs(uint64_t microSecondsSinceEpoch);
492b82937eSEd Tanous 
50*a93e9c77SEd Tanous std::string getDateTimeStdtime(std::time_t secondsSinceEpoch);
512b82937eSEd Tanous 
522b82937eSEd Tanous /**
532b82937eSEd Tanous  * Returns the current Date, Time & the local Time Offset
548ece0e45SEd Tanous  * information in a pair
552b82937eSEd Tanous  *
562b82937eSEd Tanous  * @param[in] None
572b82937eSEd Tanous  *
582b82937eSEd Tanous  * @return std::pair<std::string, std::string>, which consist
592b82937eSEd Tanous  * of current DateTime & the TimeOffset strings respectively.
602b82937eSEd Tanous  */
61*a93e9c77SEd Tanous std::pair<std::string, std::string> getDateTimeOffsetNow();
622b82937eSEd Tanous 
63c51afd54SEd Tanous using usSinceEpoch = std::chrono::duration<int64_t, std::micro>;
641b8b02a4SEd Tanous std::optional<usSinceEpoch> dateStringToEpoch(std::string_view datetime);
65b5190062SHieu Huynh 
66b5190062SHieu Huynh /**
67b5190062SHieu Huynh  * @brief Returns the datetime in ISO 8601 format
68b5190062SHieu Huynh  *
69b5190062SHieu Huynh  * @param[in] std::string_view the date of item manufacture in ISO 8601 format,
70b5190062SHieu Huynh  *            either as YYYYMMDD or YYYYMMDDThhmmssZ
71b5190062SHieu Huynh  * Ref: https://github.com/openbmc/phosphor-dbus-interfaces/blob/master/yaml/
72b5190062SHieu Huynh  *      xyz/openbmc_project/Inventory/Decorator/Asset.interface.yaml#L16
73b5190062SHieu Huynh  *
74b5190062SHieu Huynh  * @return std::string which consist the datetime
75b5190062SHieu Huynh  */
76*a93e9c77SEd Tanous std::optional<std::string> getDateTimeIso8601(std::string_view datetime);
77b5190062SHieu Huynh 
78b5190062SHieu Huynh /**
79b5190062SHieu Huynh  * @brief ProductionDate report
80b5190062SHieu Huynh  */
81*a93e9c77SEd Tanous void productionDateReport(crow::Response& res, const std::string& buildDate);
82b5190062SHieu Huynh 
83081ebf06SWludzik, Jozef } // namespace time_utils
84081ebf06SWludzik, Jozef } // namespace redfish
85