1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef LOG_TIME_STAMP_H 17 #define LOG_TIME_STAMP_H 18 #include <ctime> 19 20 namespace OHOS { 21 namespace HiviewDFX { 22 #define NS_PER_SEC 1000000000ULL 23 class LogTimeStamp { 24 public: 25 LogTimeStamp() = default; 26 ~LogTimeStamp() = default; 27 28 #ifdef __linux__ LogTimeStamp(clockid_t id)29 explicit LogTimeStamp(clockid_t id) 30 { 31 timespec time; 32 clock_gettime(id, &time); 33 tv_sec = static_cast<uint32_t>(time.tv_sec); 34 tv_nsec = static_cast<uint32_t>(time.tv_nsec); 35 } 36 #endif LogTimeStamp(const timespec & time)37 explicit LogTimeStamp(const timespec& time) 38 : tv_sec(static_cast<uint32_t>(time.tv_sec)), 39 tv_nsec(static_cast<uint32_t>(time.tv_nsec)) {} 40 41 explicit LogTimeStamp(uint32_t sec, uint32_t nsec = 0) tv_sec(sec)42 : tv_sec(sec), tv_nsec(nsec) {} 43 44 bool operator == (const LogTimeStamp& time) const 45 { 46 return (tv_sec == time.tv_sec) && (tv_nsec == time.tv_nsec); 47 } 48 49 bool operator != (const LogTimeStamp& time) const 50 { 51 return !(*this == time); 52 } 53 54 bool operator < (const LogTimeStamp& time) const 55 { 56 return (tv_sec < time.tv_sec) || 57 ((tv_sec == time.tv_sec) && (tv_nsec < time.tv_nsec)); 58 } 59 60 bool operator >= (const LogTimeStamp& time) const 61 { 62 return !(*this < time); 63 } 64 65 bool operator > (const LogTimeStamp& time) const 66 { 67 return (tv_sec > time.tv_sec) || 68 ((tv_sec == time.tv_sec) && (tv_nsec > time.tv_nsec)); 69 } 70 bool operator <= (const LogTimeStamp& time) const 71 { 72 return !(*this > time); 73 } 74 75 LogTimeStamp operator -= (const LogTimeStamp& time) 76 { 77 if (*this <= time) { 78 tv_sec = tv_nsec = 0; 79 return *this; 80 } 81 if (this->tv_nsec < time.tv_nsec) { 82 --this->tv_sec; 83 this->tv_nsec = NS_PER_SEC + this->tv_nsec - time.tv_nsec; 84 } else { 85 this->tv_nsec -= time.tv_nsec; 86 } 87 this->tv_sec -= time.tv_sec; 88 return *this; 89 } 90 91 LogTimeStamp operator += (const LogTimeStamp& time) 92 { 93 this->tv_nsec += time.tv_nsec; 94 if (this->tv_nsec >= NS_PER_SEC) { 95 this->tv_nsec -= NS_PER_SEC; 96 ++this->tv_sec; 97 } 98 this->tv_sec += time.tv_sec; 99 return *this; 100 } 101 SetTimeStamp(uint32_t sec,uint32_t nsec)102 void SetTimeStamp(uint32_t sec, uint32_t nsec) 103 { 104 this->tv_sec = sec; 105 this->tv_nsec = nsec; 106 } 107 FloatSecs()108 float FloatSecs() 109 { 110 return static_cast<float>(tv_sec) + static_cast<float>(tv_nsec) / NS_PER_SEC; 111 } 112 113 uint32_t tv_sec = 0; 114 uint32_t tv_nsec = 0; 115 }; 116 } // namespace HiviewDFX 117 } // namespace OHOS 118 #endif 119