blob: 24de908b497ac005d54a64f6d532d128c573eb46 [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
18namespace openpower
19{
20namespace pels
21{
22
23bool BCDTime::operator==(const BCDTime& right) const
24{
25 return (yearMSB == right.yearMSB) && (yearLSB == right.yearLSB) &&
26 (month == right.month) && (day == right.day) &&
27 (hour == right.hour) && (minutes == right.minutes) &&
28 (seconds == right.seconds) && (hundredths == right.hundredths);
29}
30
31bool BCDTime::operator!=(const BCDTime& right) const
32{
33 return !(*this == right);
34}
35
36BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time)
37{
38 BCDTime bcd;
39
40 using namespace std::chrono;
41 time_t t = system_clock::to_time_t(time);
42 tm* localTime = localtime(&t);
43 assert(localTime != nullptr);
44
45 int year = 1900 + localTime->tm_year;
46 bcd.yearMSB = toBCD(year / 100);
47 bcd.yearLSB = toBCD(year % 100);
48 bcd.month = toBCD(localTime->tm_mon + 1);
49 bcd.day = toBCD(localTime->tm_mday);
50 bcd.hour = toBCD(localTime->tm_hour);
51 bcd.minutes = toBCD(localTime->tm_min);
52 bcd.seconds = toBCD(localTime->tm_sec);
53
54 auto ms = duration_cast<milliseconds>(time.time_since_epoch()).count();
55 int hundredths = (ms % 1000) / 10;
56 bcd.hundredths = toBCD(hundredths);
57
58 return bcd;
59}
60
Matt Spinler5fa87f02019-08-27 16:31:57 -050061BCDTime getBCDTime(uint64_t epochMS)
62{
63 std::chrono::milliseconds ms{epochMS};
64 std::chrono::time_point<std::chrono::system_clock> time{ms};
65
66 return getBCDTime(time);
67}
68
Matt Spinlerdf797f22019-07-09 15:39:51 -050069Stream& operator>>(Stream& s, BCDTime& time)
70{
71 s >> time.yearMSB >> time.yearLSB >> time.month >> time.day >> time.hour;
72 s >> time.minutes >> time.seconds >> time.hundredths;
73 return s;
74}
75
76Stream& operator<<(Stream& s, BCDTime& time)
77{
78 s << time.yearMSB << time.yearLSB << time.month << time.day << time.hour;
79 s << time.minutes << time.seconds << time.hundredths;
80 return s;
81}
82
83} // namespace pels
84} // namespace openpower