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