1 /**
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  */
16 #include "bcd_time.hpp"
17 
18 #include <time.h>
19 
20 #include <phosphor-logging/log.hpp>
21 
22 namespace openpower
23 {
24 namespace pels
25 {
26 
operator ==(const BCDTime & right) const27 bool 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 
operator !=(const BCDTime & right) const35 bool BCDTime::operator!=(const BCDTime& right) const
36 {
37     return !(*this == right);
38 }
39 
getBCDTime(std::chrono::time_point<std::chrono::system_clock> & time)40 BCDTime 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* gmTime = gmtime(&t);
47     assert(gmTime != nullptr);
48 
49     int year = 1900 + gmTime->tm_year;
50     bcd.yearMSB = toBCD(year / 100);
51     bcd.yearLSB = toBCD(year % 100);
52     bcd.month = toBCD(gmTime->tm_mon + 1);
53     bcd.day = toBCD(gmTime->tm_mday);
54     bcd.hour = toBCD(gmTime->tm_hour);
55     bcd.minutes = toBCD(gmTime->tm_min);
56     bcd.seconds = toBCD(gmTime->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 
getBCDTime(uint64_t epochMS)65 BCDTime 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 
getMillisecondsSinceEpoch(const BCDTime & bcdTime)73 uint64_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 
operator >>(Stream & s,BCDTime & time)96 Stream& 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 
operator <<(Stream & s,const BCDTime & time)103 Stream& operator<<(Stream& s, const BCDTime& time)
104 {
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
112