| Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 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 | */ | 
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 16 | #include "bcd_time.hpp" | 
|  | 17 |  | 
| Matt Spinler | 0bf04b5 | 2023-04-28 10:30:26 -0500 | [diff] [blame] | 18 | #include <fmt/format.h> | 
|  | 19 | #include <time.h> | 
|  | 20 |  | 
|  | 21 | #include <phosphor-logging/log.hpp> | 
|  | 22 |  | 
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 23 | namespace openpower | 
|  | 24 | { | 
|  | 25 | namespace pels | 
|  | 26 | { | 
|  | 27 |  | 
|  | 28 | bool BCDTime::operator==(const BCDTime& right) const | 
|  | 29 | { | 
|  | 30 | return (yearMSB == right.yearMSB) && (yearLSB == right.yearLSB) && | 
|  | 31 | (month == right.month) && (day == right.day) && | 
|  | 32 | (hour == right.hour) && (minutes == right.minutes) && | 
|  | 33 | (seconds == right.seconds) && (hundredths == right.hundredths); | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | bool BCDTime::operator!=(const BCDTime& right) const | 
|  | 37 | { | 
|  | 38 | return !(*this == right); | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time) | 
|  | 42 | { | 
|  | 43 | BCDTime bcd; | 
|  | 44 |  | 
|  | 45 | using namespace std::chrono; | 
|  | 46 | time_t t = system_clock::to_time_t(time); | 
|  | 47 | tm* localTime = localtime(&t); | 
|  | 48 | assert(localTime != nullptr); | 
|  | 49 |  | 
|  | 50 | int year = 1900 + localTime->tm_year; | 
|  | 51 | bcd.yearMSB = toBCD(year / 100); | 
|  | 52 | bcd.yearLSB = toBCD(year % 100); | 
|  | 53 | bcd.month = toBCD(localTime->tm_mon + 1); | 
|  | 54 | bcd.day = toBCD(localTime->tm_mday); | 
|  | 55 | bcd.hour = toBCD(localTime->tm_hour); | 
|  | 56 | bcd.minutes = toBCD(localTime->tm_min); | 
|  | 57 | bcd.seconds = toBCD(localTime->tm_sec); | 
|  | 58 |  | 
|  | 59 | auto ms = duration_cast<milliseconds>(time.time_since_epoch()).count(); | 
|  | 60 | int hundredths = (ms % 1000) / 10; | 
|  | 61 | bcd.hundredths = toBCD(hundredths); | 
|  | 62 |  | 
|  | 63 | return bcd; | 
|  | 64 | } | 
|  | 65 |  | 
| Matt Spinler | 5fa87f0 | 2019-08-27 16:31:57 -0500 | [diff] [blame] | 66 | BCDTime getBCDTime(uint64_t epochMS) | 
|  | 67 | { | 
|  | 68 | std::chrono::milliseconds ms{epochMS}; | 
|  | 69 | std::chrono::time_point<std::chrono::system_clock> time{ms}; | 
|  | 70 |  | 
|  | 71 | return getBCDTime(time); | 
|  | 72 | } | 
|  | 73 |  | 
| Matt Spinler | 0bf04b5 | 2023-04-28 10:30:26 -0500 | [diff] [blame] | 74 | uint64_t getMillisecondsSinceEpoch(const BCDTime& bcdTime) | 
|  | 75 | { | 
|  | 76 | // Convert a UTC tm struct to a UTC time_t struct to a timepoint. | 
|  | 77 | int year = (fromBCD(bcdTime.yearMSB) * 100) + fromBCD(bcdTime.yearLSB); | 
|  | 78 | tm utcTime; | 
|  | 79 | utcTime.tm_year = year - 1900; | 
|  | 80 | utcTime.tm_mon = fromBCD(bcdTime.month) - 1; | 
|  | 81 | utcTime.tm_mday = fromBCD(bcdTime.day); | 
|  | 82 | utcTime.tm_hour = fromBCD(bcdTime.hour); | 
|  | 83 | utcTime.tm_min = fromBCD(bcdTime.minutes); | 
|  | 84 | utcTime.tm_sec = fromBCD(bcdTime.seconds); | 
|  | 85 | utcTime.tm_isdst = 0; | 
|  | 86 |  | 
|  | 87 | time_t t = timegm(&utcTime); | 
|  | 88 | auto timepoint = std::chrono::system_clock::from_time_t(t); | 
|  | 89 | int milliseconds = fromBCD(bcdTime.hundredths) * 10; | 
|  | 90 | timepoint += std::chrono::milliseconds(milliseconds); | 
|  | 91 |  | 
|  | 92 | return std::chrono::duration_cast<std::chrono::milliseconds>( | 
|  | 93 | timepoint.time_since_epoch()) | 
|  | 94 | .count(); | 
|  | 95 | } | 
|  | 96 |  | 
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 97 | Stream& operator>>(Stream& s, BCDTime& time) | 
|  | 98 | { | 
|  | 99 | s >> time.yearMSB >> time.yearLSB >> time.month >> time.day >> time.hour; | 
|  | 100 | s >> time.minutes >> time.seconds >> time.hundredths; | 
|  | 101 | return s; | 
|  | 102 | } | 
|  | 103 |  | 
| Matt Spinler | d7b004b | 2019-11-06 10:23:34 -0600 | [diff] [blame] | 104 | Stream& operator<<(Stream& s, const BCDTime& time) | 
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 105 | { | 
|  | 106 | s << time.yearMSB << time.yearLSB << time.month << time.day << time.hour; | 
|  | 107 | s << time.minutes << time.seconds << time.hundredths; | 
|  | 108 | return s; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | } // namespace pels | 
|  | 112 | } // namespace openpower |