12498b56bSopenharmony_ci/* 22498b56bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 42498b56bSopenharmony_ci * you may not use this file except in compliance with the License. 52498b56bSopenharmony_ci * You may obtain a copy of the License at 62498b56bSopenharmony_ci * 72498b56bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 82498b56bSopenharmony_ci * 92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 122498b56bSopenharmony_ci * See the License for the specific language governing permissions and 132498b56bSopenharmony_ci * limitations under the License. 142498b56bSopenharmony_ci */ 152498b56bSopenharmony_ci 162498b56bSopenharmony_ci#ifndef LOG_TIME_STAMP_H 172498b56bSopenharmony_ci#define LOG_TIME_STAMP_H 182498b56bSopenharmony_ci#include <ctime> 192498b56bSopenharmony_ci 202498b56bSopenharmony_cinamespace OHOS { 212498b56bSopenharmony_cinamespace HiviewDFX { 222498b56bSopenharmony_ci#define NS_PER_SEC 1000000000ULL 232498b56bSopenharmony_ciclass LogTimeStamp { 242498b56bSopenharmony_cipublic: 252498b56bSopenharmony_ci LogTimeStamp() = default; 262498b56bSopenharmony_ci ~LogTimeStamp() = default; 272498b56bSopenharmony_ci 282498b56bSopenharmony_ci#ifdef __linux__ 292498b56bSopenharmony_ci explicit LogTimeStamp(clockid_t id) 302498b56bSopenharmony_ci { 312498b56bSopenharmony_ci timespec time; 322498b56bSopenharmony_ci clock_gettime(id, &time); 332498b56bSopenharmony_ci tv_sec = static_cast<uint32_t>(time.tv_sec); 342498b56bSopenharmony_ci tv_nsec = static_cast<uint32_t>(time.tv_nsec); 352498b56bSopenharmony_ci } 362498b56bSopenharmony_ci#endif 372498b56bSopenharmony_ci explicit LogTimeStamp(const timespec& time) 382498b56bSopenharmony_ci : tv_sec(static_cast<uint32_t>(time.tv_sec)), 392498b56bSopenharmony_ci tv_nsec(static_cast<uint32_t>(time.tv_nsec)) {} 402498b56bSopenharmony_ci 412498b56bSopenharmony_ci explicit LogTimeStamp(uint32_t sec, uint32_t nsec = 0) 422498b56bSopenharmony_ci : tv_sec(sec), tv_nsec(nsec) {} 432498b56bSopenharmony_ci 442498b56bSopenharmony_ci bool operator == (const LogTimeStamp& time) const 452498b56bSopenharmony_ci { 462498b56bSopenharmony_ci return (tv_sec == time.tv_sec) && (tv_nsec == time.tv_nsec); 472498b56bSopenharmony_ci } 482498b56bSopenharmony_ci 492498b56bSopenharmony_ci bool operator != (const LogTimeStamp& time) const 502498b56bSopenharmony_ci { 512498b56bSopenharmony_ci return !(*this == time); 522498b56bSopenharmony_ci } 532498b56bSopenharmony_ci 542498b56bSopenharmony_ci bool operator < (const LogTimeStamp& time) const 552498b56bSopenharmony_ci { 562498b56bSopenharmony_ci return (tv_sec < time.tv_sec) || 572498b56bSopenharmony_ci ((tv_sec == time.tv_sec) && (tv_nsec < time.tv_nsec)); 582498b56bSopenharmony_ci } 592498b56bSopenharmony_ci 602498b56bSopenharmony_ci bool operator >= (const LogTimeStamp& time) const 612498b56bSopenharmony_ci { 622498b56bSopenharmony_ci return !(*this < time); 632498b56bSopenharmony_ci } 642498b56bSopenharmony_ci 652498b56bSopenharmony_ci bool operator > (const LogTimeStamp& time) const 662498b56bSopenharmony_ci { 672498b56bSopenharmony_ci return (tv_sec > time.tv_sec) || 682498b56bSopenharmony_ci ((tv_sec == time.tv_sec) && (tv_nsec > time.tv_nsec)); 692498b56bSopenharmony_ci } 702498b56bSopenharmony_ci bool operator <= (const LogTimeStamp& time) const 712498b56bSopenharmony_ci { 722498b56bSopenharmony_ci return !(*this > time); 732498b56bSopenharmony_ci } 742498b56bSopenharmony_ci 752498b56bSopenharmony_ci LogTimeStamp operator -= (const LogTimeStamp& time) 762498b56bSopenharmony_ci { 772498b56bSopenharmony_ci if (*this <= time) { 782498b56bSopenharmony_ci tv_sec = tv_nsec = 0; 792498b56bSopenharmony_ci return *this; 802498b56bSopenharmony_ci } 812498b56bSopenharmony_ci if (this->tv_nsec < time.tv_nsec) { 822498b56bSopenharmony_ci --this->tv_sec; 832498b56bSopenharmony_ci this->tv_nsec = NS_PER_SEC + this->tv_nsec - time.tv_nsec; 842498b56bSopenharmony_ci } else { 852498b56bSopenharmony_ci this->tv_nsec -= time.tv_nsec; 862498b56bSopenharmony_ci } 872498b56bSopenharmony_ci this->tv_sec -= time.tv_sec; 882498b56bSopenharmony_ci return *this; 892498b56bSopenharmony_ci } 902498b56bSopenharmony_ci 912498b56bSopenharmony_ci LogTimeStamp operator += (const LogTimeStamp& time) 922498b56bSopenharmony_ci { 932498b56bSopenharmony_ci this->tv_nsec += time.tv_nsec; 942498b56bSopenharmony_ci if (this->tv_nsec >= NS_PER_SEC) { 952498b56bSopenharmony_ci this->tv_nsec -= NS_PER_SEC; 962498b56bSopenharmony_ci ++this->tv_sec; 972498b56bSopenharmony_ci } 982498b56bSopenharmony_ci this->tv_sec += time.tv_sec; 992498b56bSopenharmony_ci return *this; 1002498b56bSopenharmony_ci } 1012498b56bSopenharmony_ci 1022498b56bSopenharmony_ci void SetTimeStamp(uint32_t sec, uint32_t nsec) 1032498b56bSopenharmony_ci { 1042498b56bSopenharmony_ci this->tv_sec = sec; 1052498b56bSopenharmony_ci this->tv_nsec = nsec; 1062498b56bSopenharmony_ci } 1072498b56bSopenharmony_ci 1082498b56bSopenharmony_ci float FloatSecs() 1092498b56bSopenharmony_ci { 1102498b56bSopenharmony_ci return static_cast<float>(tv_sec) + static_cast<float>(tv_nsec) / NS_PER_SEC; 1112498b56bSopenharmony_ci } 1122498b56bSopenharmony_ci 1132498b56bSopenharmony_ci uint32_t tv_sec = 0; 1142498b56bSopenharmony_ci uint32_t tv_nsec = 0; 1152498b56bSopenharmony_ci}; 1162498b56bSopenharmony_ci} // namespace HiviewDFX 1172498b56bSopenharmony_ci} // namespace OHOS 1182498b56bSopenharmony_ci#endif 119