blob: 554a230e057a8593bdcdd7b391e79e5f632a405f [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 <time.h>
19
20#include <phosphor-logging/log.hpp>
21
Matt Spinlerdf797f22019-07-09 15:39:51 -050022namespace openpower
23{
24namespace pels
25{
26
27bool BCDTime::operator==(const BCDTime& right) const
28{
29 return (yearMSB == right.yearMSB) && (yearLSB == right.yearLSB) &&
30 (month == right.month) && (day == right.day) &&
31 (hour == right.hour) && (minutes == right.minutes) &&
32 (seconds == right.seconds) && (hundredths == right.hundredths);
33}
34
35bool BCDTime::operator!=(const BCDTime& right) const
36{
37 return !(*this == right);
38}
39
40BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time)
41{
42 BCDTime bcd;
43
44 using namespace std::chrono;
45 time_t t = system_clock::to_time_t(time);
46 tm* localTime = localtime(&t);
47 assert(localTime != nullptr);
48
49 int year = 1900 + localTime->tm_year;
50 bcd.yearMSB = toBCD(year / 100);
51 bcd.yearLSB = toBCD(year % 100);
52 bcd.month = toBCD(localTime->tm_mon + 1);
53 bcd.day = toBCD(localTime->tm_mday);
54 bcd.hour = toBCD(localTime->tm_hour);
55 bcd.minutes = toBCD(localTime->tm_min);
56 bcd.seconds = toBCD(localTime->tm_sec);
57
58 auto ms = duration_cast<milliseconds>(time.time_since_epoch()).count();
59 int hundredths = (ms % 1000) / 10;
60 bcd.hundredths = toBCD(hundredths);
61
62 return bcd;
63}
64
Matt Spinler5fa87f02019-08-27 16:31:57 -050065BCDTime getBCDTime(uint64_t epochMS)
66{
67 std::chrono::milliseconds ms{epochMS};
68 std::chrono::time_point<std::chrono::system_clock> time{ms};
69
70 return getBCDTime(time);
71}
72
Matt Spinler0bf04b52023-04-28 10:30:26 -050073uint64_t getMillisecondsSinceEpoch(const BCDTime& bcdTime)
74{
75 // Convert a UTC tm struct to a UTC time_t struct to a timepoint.
76 int year = (fromBCD(bcdTime.yearMSB) * 100) + fromBCD(bcdTime.yearLSB);
77 tm utcTime;
78 utcTime.tm_year = year - 1900;
79 utcTime.tm_mon = fromBCD(bcdTime.month) - 1;
80 utcTime.tm_mday = fromBCD(bcdTime.day);
81 utcTime.tm_hour = fromBCD(bcdTime.hour);
82 utcTime.tm_min = fromBCD(bcdTime.minutes);
83 utcTime.tm_sec = fromBCD(bcdTime.seconds);
84 utcTime.tm_isdst = 0;
85
86 time_t t = timegm(&utcTime);
87 auto timepoint = std::chrono::system_clock::from_time_t(t);
88 int milliseconds = fromBCD(bcdTime.hundredths) * 10;
89 timepoint += std::chrono::milliseconds(milliseconds);
90
91 return std::chrono::duration_cast<std::chrono::milliseconds>(
92 timepoint.time_since_epoch())
93 .count();
94}
95
Matt Spinlerdf797f22019-07-09 15:39:51 -050096Stream& operator>>(Stream& s, BCDTime& time)
97{
98 s >> time.yearMSB >> time.yearLSB >> time.month >> time.day >> time.hour;
99 s >> time.minutes >> time.seconds >> time.hundredths;
100 return s;
101}
102
Matt Spinlerd7b004b2019-11-06 10:23:34 -0600103Stream& operator<<(Stream& s, const BCDTime& time)
Matt Spinlerdf797f22019-07-09 15:39:51 -0500104{
105 s << time.yearMSB << time.yearLSB << time.month << time.day << time.hour;
106 s << time.minutes << time.seconds << time.hundredths;
107 return s;
108}
109
110} // namespace pels
111} // namespace openpower