blob: 42e2e73451e650ea4bedb52abeba7279c9ef8091 [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
2 * Copyright © 2019 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matt Spinlerdf797f22019-07-09 15:39:51 -050016#include "bcd_time.hpp"
17
Matt Spinler0bf04b52023-04-28 10:30:26 -050018#include <fmt/format.h>
19#include <time.h>
20
21#include <phosphor-logging/log.hpp>
22
Matt Spinlerdf797f22019-07-09 15:39:51 -050023namespace openpower
24{
25namespace pels
26{
27
28bool BCDTime::operator==(const BCDTime& right) const
29{
30 return (yearMSB == right.yearMSB) && (yearLSB == right.yearLSB) &&
31 (month == right.month) && (day == right.day) &&
32 (hour == right.hour) && (minutes == right.minutes) &&
33 (seconds == right.seconds) && (hundredths == right.hundredths);
34}
35
36bool BCDTime::operator!=(const BCDTime& right) const
37{
38 return !(*this == right);
39}
40
41BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time)
42{
43 BCDTime bcd;
44
45 using namespace std::chrono;
46 time_t t = system_clock::to_time_t(time);
47 tm* localTime = localtime(&t);
48 assert(localTime != nullptr);
49
50 int year = 1900 + localTime->tm_year;
51 bcd.yearMSB = toBCD(year / 100);
52 bcd.yearLSB = toBCD(year % 100);
53 bcd.month = toBCD(localTime->tm_mon + 1);
54 bcd.day = toBCD(localTime->tm_mday);
55 bcd.hour = toBCD(localTime->tm_hour);
56 bcd.minutes = toBCD(localTime->tm_min);
57 bcd.seconds = toBCD(localTime->tm_sec);
58
59 auto ms = duration_cast<milliseconds>(time.time_since_epoch()).count();
60 int hundredths = (ms % 1000) / 10;
61 bcd.hundredths = toBCD(hundredths);
62
63 return bcd;
64}
65
Matt Spinler5fa87f02019-08-27 16:31:57 -050066BCDTime getBCDTime(uint64_t epochMS)
67{
68 std::chrono::milliseconds ms{epochMS};
69 std::chrono::time_point<std::chrono::system_clock> time{ms};
70
71 return getBCDTime(time);
72}
73
Matt Spinler0bf04b52023-04-28 10:30:26 -050074uint64_t getMillisecondsSinceEpoch(const BCDTime& bcdTime)
75{
76 // Convert a UTC tm struct to a UTC time_t struct to a timepoint.
77 int year = (fromBCD(bcdTime.yearMSB) * 100) + fromBCD(bcdTime.yearLSB);
78 tm utcTime;
79 utcTime.tm_year = year - 1900;
80 utcTime.tm_mon = fromBCD(bcdTime.month) - 1;
81 utcTime.tm_mday = fromBCD(bcdTime.day);
82 utcTime.tm_hour = fromBCD(bcdTime.hour);
83 utcTime.tm_min = fromBCD(bcdTime.minutes);
84 utcTime.tm_sec = fromBCD(bcdTime.seconds);
85 utcTime.tm_isdst = 0;
86
87 time_t t = timegm(&utcTime);
88 auto timepoint = std::chrono::system_clock::from_time_t(t);
89 int milliseconds = fromBCD(bcdTime.hundredths) * 10;
90 timepoint += std::chrono::milliseconds(milliseconds);
91
92 return std::chrono::duration_cast<std::chrono::milliseconds>(
93 timepoint.time_since_epoch())
94 .count();
95}
96
Matt Spinlerdf797f22019-07-09 15:39:51 -050097Stream& operator>>(Stream& s, BCDTime& time)
98{
99 s >> time.yearMSB >> time.yearLSB >> time.month >> time.day >> time.hour;
100 s >> time.minutes >> time.seconds >> time.hundredths;
101 return s;
102}
103
Matt Spinlerd7b004b2019-11-06 10:23:34 -0600104Stream& operator<<(Stream& s, const BCDTime& time)
Matt Spinlerdf797f22019-07-09 15:39:51 -0500105{
106 s << time.yearMSB << time.yearLSB << time.month << time.day << time.hour;
107 s << time.minutes << time.seconds << time.hundredths;
108 return s;
109}
110
111} // namespace pels
112} // namespace openpower