1 #include "bcd_time.hpp"
2 
3 namespace openpower
4 {
5 namespace pels
6 {
7 
8 bool BCDTime::operator==(const BCDTime& right) const
9 {
10     return (yearMSB == right.yearMSB) && (yearLSB == right.yearLSB) &&
11            (month == right.month) && (day == right.day) &&
12            (hour == right.hour) && (minutes == right.minutes) &&
13            (seconds == right.seconds) && (hundredths == right.hundredths);
14 }
15 
16 bool BCDTime::operator!=(const BCDTime& right) const
17 {
18     return !(*this == right);
19 }
20 
21 BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time)
22 {
23     BCDTime bcd;
24 
25     using namespace std::chrono;
26     time_t t = system_clock::to_time_t(time);
27     tm* localTime = localtime(&t);
28     assert(localTime != nullptr);
29 
30     int year = 1900 + localTime->tm_year;
31     bcd.yearMSB = toBCD(year / 100);
32     bcd.yearLSB = toBCD(year % 100);
33     bcd.month = toBCD(localTime->tm_mon + 1);
34     bcd.day = toBCD(localTime->tm_mday);
35     bcd.hour = toBCD(localTime->tm_hour);
36     bcd.minutes = toBCD(localTime->tm_min);
37     bcd.seconds = toBCD(localTime->tm_sec);
38 
39     auto ms = duration_cast<milliseconds>(time.time_since_epoch()).count();
40     int hundredths = (ms % 1000) / 10;
41     bcd.hundredths = toBCD(hundredths);
42 
43     return bcd;
44 }
45 
46 BCDTime getBCDTime(uint64_t epochMS)
47 {
48     std::chrono::milliseconds ms{epochMS};
49     std::chrono::time_point<std::chrono::system_clock> time{ms};
50 
51     return getBCDTime(time);
52 }
53 
54 Stream& operator>>(Stream& s, BCDTime& time)
55 {
56     s >> time.yearMSB >> time.yearLSB >> time.month >> time.day >> time.hour;
57     s >> time.minutes >> time.seconds >> time.hundredths;
58     return s;
59 }
60 
61 Stream& operator<<(Stream& s, BCDTime& time)
62 {
63     s << time.yearMSB << time.yearLSB << time.month << time.day << time.hour;
64     s << time.minutes << time.seconds << time.hundredths;
65     return s;
66 }
67 
68 } // namespace pels
69 } // namespace openpower
70