Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 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 | Stream& operator>>(Stream& s, BCDTime& time) |
| 47 | { |
| 48 | s >> time.yearMSB >> time.yearLSB >> time.month >> time.day >> time.hour; |
| 49 | s >> time.minutes >> time.seconds >> time.hundredths; |
| 50 | return s; |
| 51 | } |
| 52 | |
| 53 | Stream& operator<<(Stream& s, BCDTime& time) |
| 54 | { |
| 55 | s << time.yearMSB << time.yearLSB << time.month << time.day << time.hour; |
| 56 | s << time.minutes << time.seconds << time.hundredths; |
| 57 | return s; |
| 58 | } |
| 59 | |
| 60 | } // namespace pels |
| 61 | } // namespace openpower |