blob: b77b88e2872e33595ebd250c22bb83a8ac551ebe [file] [log] [blame]
Alexander Hansen40fb5492025-10-28 17:56:12 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3
Matt Spinlerdf797f22019-07-09 15:39:51 -05004#include "bcd_time.hpp"
5
Matt Spinler0bf04b52023-04-28 10:30:26 -05006#include <time.h>
7
8#include <phosphor-logging/log.hpp>
9
Matt Spinlerdf797f22019-07-09 15:39:51 -050010namespace openpower
11{
12namespace pels
13{
14
15bool BCDTime::operator==(const BCDTime& right) const
16{
17 return (yearMSB == right.yearMSB) && (yearLSB == right.yearLSB) &&
18 (month == right.month) && (day == right.day) &&
19 (hour == right.hour) && (minutes == right.minutes) &&
20 (seconds == right.seconds) && (hundredths == right.hundredths);
21}
22
23bool BCDTime::operator!=(const BCDTime& right) const
24{
25 return !(*this == right);
26}
27
28BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time)
29{
30 BCDTime bcd;
31
32 using namespace std::chrono;
33 time_t t = system_clock::to_time_t(time);
Patrick Williams253bfb72024-09-30 22:22:46 -040034 tm* gmTime = gmtime(&t);
35 assert(gmTime != nullptr);
Matt Spinlerdf797f22019-07-09 15:39:51 -050036
Patrick Williams253bfb72024-09-30 22:22:46 -040037 int year = 1900 + gmTime->tm_year;
Matt Spinlerdf797f22019-07-09 15:39:51 -050038 bcd.yearMSB = toBCD(year / 100);
39 bcd.yearLSB = toBCD(year % 100);
Patrick Williams253bfb72024-09-30 22:22:46 -040040 bcd.month = toBCD(gmTime->tm_mon + 1);
41 bcd.day = toBCD(gmTime->tm_mday);
42 bcd.hour = toBCD(gmTime->tm_hour);
43 bcd.minutes = toBCD(gmTime->tm_min);
44 bcd.seconds = toBCD(gmTime->tm_sec);
Matt Spinlerdf797f22019-07-09 15:39:51 -050045
46 auto ms = duration_cast<milliseconds>(time.time_since_epoch()).count();
47 int hundredths = (ms % 1000) / 10;
48 bcd.hundredths = toBCD(hundredths);
49
50 return bcd;
51}
52
Matt Spinler5fa87f02019-08-27 16:31:57 -050053BCDTime getBCDTime(uint64_t epochMS)
54{
55 std::chrono::milliseconds ms{epochMS};
56 std::chrono::time_point<std::chrono::system_clock> time{ms};
57
58 return getBCDTime(time);
59}
60
Matt Spinler0bf04b52023-04-28 10:30:26 -050061uint64_t getMillisecondsSinceEpoch(const BCDTime& bcdTime)
62{
63 // Convert a UTC tm struct to a UTC time_t struct to a timepoint.
64 int year = (fromBCD(bcdTime.yearMSB) * 100) + fromBCD(bcdTime.yearLSB);
65 tm utcTime;
66 utcTime.tm_year = year - 1900;
67 utcTime.tm_mon = fromBCD(bcdTime.month) - 1;
68 utcTime.tm_mday = fromBCD(bcdTime.day);
69 utcTime.tm_hour = fromBCD(bcdTime.hour);
70 utcTime.tm_min = fromBCD(bcdTime.minutes);
71 utcTime.tm_sec = fromBCD(bcdTime.seconds);
72 utcTime.tm_isdst = 0;
73
74 time_t t = timegm(&utcTime);
75 auto timepoint = std::chrono::system_clock::from_time_t(t);
76 int milliseconds = fromBCD(bcdTime.hundredths) * 10;
77 timepoint += std::chrono::milliseconds(milliseconds);
78
79 return std::chrono::duration_cast<std::chrono::milliseconds>(
80 timepoint.time_since_epoch())
81 .count();
82}
83
Matt Spinlerdf797f22019-07-09 15:39:51 -050084Stream& operator>>(Stream& s, BCDTime& time)
85{
86 s >> time.yearMSB >> time.yearLSB >> time.month >> time.day >> time.hour;
87 s >> time.minutes >> time.seconds >> time.hundredths;
88 return s;
89}
90
Matt Spinlerd7b004b2019-11-06 10:23:34 -060091Stream& operator<<(Stream& s, const BCDTime& time)
Matt Spinlerdf797f22019-07-09 15:39:51 -050092{
93 s << time.yearMSB << time.yearLSB << time.month << time.day << time.hour;
94 s << time.minutes << time.seconds << time.hundredths;
95 return s;
96}
97
98} // namespace pels
99} // namespace openpower