xref: /openbmc/bmcweb/redfish-core/include/utils/time_utils.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "http_response.hpp"
6 
7 #include <chrono>
8 #include <cstdint>
9 #include <ctime>
10 #include <optional>
11 #include <ratio>
12 #include <string>
13 #include <string_view>
14 #include <utility>
15 
16 namespace redfish
17 {
18 
19 namespace time_utils
20 {
21 
22 /**
23  * @brief Convert string that represents value in Duration Format to its numeric
24  *        equivalent.
25  */
26 std::optional<std::chrono::milliseconds> fromDurationString(std::string_view v);
27 
28 /**
29  * @brief Convert time value into duration format that is based on ISO 8601.
30  *        Example output: "P12DT1M5.5S"
31  *        Ref: Redfish Specification, Section 9.4.4. Duration values
32  */
33 std::string toDurationString(std::chrono::milliseconds ms);
34 
35 std::optional<std::string> toDurationStringFromUint(uint64_t timeMs);
36 
37 // Returns the formatted date time string.
38 // Note that the maximum supported date is 9999-12-31T23:59:59+00:00, if
39 // the given |secondsSinceEpoch| is too large, we return the maximum supported
40 // date.
41 std::string getDateTimeUint(uint64_t secondsSinceEpoch);
42 
43 // Returns the formatted date time string with millisecond precision
44 // Note that the maximum supported date is 9999-12-31T23:59:59+00:00, if
45 // the given |secondsSinceEpoch| is too large, we return the maximum supported
46 // date.
47 std::string getDateTimeUintMs(uint64_t milliSecondsSinceEpoch);
48 
49 // Returns the formatted date time string with microsecond precision
50 std::string getDateTimeUintUs(uint64_t microSecondsSinceEpoch);
51 
52 std::string getDateTimeStdtime(std::time_t secondsSinceEpoch);
53 
54 /**
55  * Returns the current Date, Time & the local Time Offset
56  * information in a pair
57  *
58  * @param[in] None
59  *
60  * @return std::pair<std::string, std::string>, which consist
61  * of current DateTime & the TimeOffset strings respectively.
62  */
63 std::pair<std::string, std::string> getDateTimeOffsetNow();
64 
65 using usSinceEpoch = std::chrono::duration<int64_t, std::micro>;
66 std::optional<usSinceEpoch> dateStringToEpoch(std::string_view datetime);
67 
68 /**
69  * @brief Returns the datetime in ISO 8601 format
70  *
71  * @param[in] std::string_view the date of item manufacture in ISO 8601 format,
72  *            either as YYYYMMDD or YYYYMMDDThhmmssZ
73  * Ref: https://github.com/openbmc/phosphor-dbus-interfaces/blob/master/yaml/
74  *      xyz/openbmc_project/Inventory/Decorator/Asset.interface.yaml#L16
75  *
76  * @return std::string which consist the datetime
77  */
78 std::optional<std::string> getDateTimeIso8601(std::string_view datetime);
79 
80 /**
81  * @brief ProductionDate report
82  */
83 void productionDateReport(crow::Response& res, const std::string& buildDate);
84 
85 } // namespace time_utils
86 } // namespace redfish
87