| Alexander Hansen | 40fb549 | 2025-10-28 17:56:12 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2019 IBM Corporation |
| 3 | |
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 4 | #include "bcd_time.hpp" |
| 5 | |
| Matt Spinler | 0bf04b5 | 2023-04-28 10:30:26 -0500 | [diff] [blame] | 6 | #include <time.h> |
| 7 | |
| 8 | #include <phosphor-logging/log.hpp> |
| 9 | |
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 10 | namespace openpower |
| 11 | { |
| 12 | namespace pels |
| 13 | { |
| 14 | |
| 15 | bool BCDTime::operator==(const BCDTime& right) const |
| 16 | { |
| 17 | return (yearMSB == right.yearMSB) && (yearLSB == right.yearLSB) && |
| 18 | (month == right.month) && (day == right.day) && |
| 19 | (hour == right.hour) && (minutes == right.minutes) && |
| 20 | (seconds == right.seconds) && (hundredths == right.hundredths); |
| 21 | } |
| 22 | |
| 23 | bool BCDTime::operator!=(const BCDTime& right) const |
| 24 | { |
| 25 | return !(*this == right); |
| 26 | } |
| 27 | |
| 28 | BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time) |
| 29 | { |
| 30 | BCDTime bcd; |
| 31 | |
| 32 | using namespace std::chrono; |
| 33 | time_t t = system_clock::to_time_t(time); |
| Patrick Williams | 253bfb7 | 2024-09-30 22:22:46 -0400 | [diff] [blame] | 34 | tm* gmTime = gmtime(&t); |
| 35 | assert(gmTime != nullptr); |
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 36 | |
| Patrick Williams | 253bfb7 | 2024-09-30 22:22:46 -0400 | [diff] [blame] | 37 | int year = 1900 + gmTime->tm_year; |
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 38 | bcd.yearMSB = toBCD(year / 100); |
| 39 | bcd.yearLSB = toBCD(year % 100); |
| Patrick Williams | 253bfb7 | 2024-09-30 22:22:46 -0400 | [diff] [blame] | 40 | bcd.month = toBCD(gmTime->tm_mon + 1); |
| 41 | bcd.day = toBCD(gmTime->tm_mday); |
| 42 | bcd.hour = toBCD(gmTime->tm_hour); |
| 43 | bcd.minutes = toBCD(gmTime->tm_min); |
| 44 | bcd.seconds = toBCD(gmTime->tm_sec); |
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 45 | |
| 46 | auto ms = duration_cast<milliseconds>(time.time_since_epoch()).count(); |
| 47 | int hundredths = (ms % 1000) / 10; |
| 48 | bcd.hundredths = toBCD(hundredths); |
| 49 | |
| 50 | return bcd; |
| 51 | } |
| 52 | |
| Matt Spinler | 5fa87f0 | 2019-08-27 16:31:57 -0500 | [diff] [blame] | 53 | BCDTime getBCDTime(uint64_t epochMS) |
| 54 | { |
| 55 | std::chrono::milliseconds ms{epochMS}; |
| 56 | std::chrono::time_point<std::chrono::system_clock> time{ms}; |
| 57 | |
| 58 | return getBCDTime(time); |
| 59 | } |
| 60 | |
| Matt Spinler | 0bf04b5 | 2023-04-28 10:30:26 -0500 | [diff] [blame] | 61 | uint64_t getMillisecondsSinceEpoch(const BCDTime& bcdTime) |
| 62 | { |
| 63 | // Convert a UTC tm struct to a UTC time_t struct to a timepoint. |
| 64 | int year = (fromBCD(bcdTime.yearMSB) * 100) + fromBCD(bcdTime.yearLSB); |
| 65 | tm utcTime; |
| 66 | utcTime.tm_year = year - 1900; |
| 67 | utcTime.tm_mon = fromBCD(bcdTime.month) - 1; |
| 68 | utcTime.tm_mday = fromBCD(bcdTime.day); |
| 69 | utcTime.tm_hour = fromBCD(bcdTime.hour); |
| 70 | utcTime.tm_min = fromBCD(bcdTime.minutes); |
| 71 | utcTime.tm_sec = fromBCD(bcdTime.seconds); |
| 72 | utcTime.tm_isdst = 0; |
| 73 | |
| 74 | time_t t = timegm(&utcTime); |
| 75 | auto timepoint = std::chrono::system_clock::from_time_t(t); |
| 76 | int milliseconds = fromBCD(bcdTime.hundredths) * 10; |
| 77 | timepoint += std::chrono::milliseconds(milliseconds); |
| 78 | |
| 79 | return std::chrono::duration_cast<std::chrono::milliseconds>( |
| 80 | timepoint.time_since_epoch()) |
| 81 | .count(); |
| 82 | } |
| 83 | |
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 84 | Stream& operator>>(Stream& s, BCDTime& time) |
| 85 | { |
| 86 | s >> time.yearMSB >> time.yearLSB >> time.month >> time.day >> time.hour; |
| 87 | s >> time.minutes >> time.seconds >> time.hundredths; |
| 88 | return s; |
| 89 | } |
| 90 | |
| Matt Spinler | d7b004b | 2019-11-06 10:23:34 -0600 | [diff] [blame] | 91 | Stream& operator<<(Stream& s, const BCDTime& time) |
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 92 | { |
| 93 | s << time.yearMSB << time.yearLSB << time.month << time.day << time.hour; |
| 94 | s << time.minutes << time.seconds << time.hundredths; |
| 95 | return s; |
| 96 | } |
| 97 | |
| 98 | } // namespace pels |
| 99 | } // namespace openpower |