• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LOG_TIME_STAMP_H
16 #define LOG_TIME_STAMP_H
17 
18 #define NS_PER_SEC 1000000000ULL
19 class LogTimeStamp {
20 
21 public:
22     LogTimeStamp() = default;
23     ~LogTimeStamp() = default;
24 #ifdef __linux__
LogTimeStamp(clockid_t id)25     explicit LogTimeStamp(clockid_t id)
26     {
27         timespec time;
28         clock_gettime(id, &time);
29         tv_sec = static_cast<uint32_t>(time.tv_sec);
30         tv_nsec = static_cast<uint32_t>(time.tv_nsec);
31     }
32 #endif
LogTimeStamp(const timespec & time)33     explicit LogTimeStamp(const timespec& time)
34         : tv_sec(static_cast<uint32_t>(time.tv_sec)),
35         tv_nsec(static_cast<uint32_t>(time.tv_nsec)) {};
36     explicit LogTimeStamp(uint32_t sec, uint32_t nsec = 0)
tv_sec(sec)37         : tv_sec(sec), tv_nsec(nsec) {};
38     /* LogTimeStamp */
39     bool operator == (const LogTimeStamp& time) const
40     {
41         return (tv_sec == time.tv_sec) && (tv_nsec == time.tv_nsec);
42     }
43     bool operator != (const LogTimeStamp& time) const
44     {
45         return !(*this == time);
46     }
47     bool operator < (const LogTimeStamp& time) const
48     {
49         return (tv_sec < time.tv_sec) ||
50         ((tv_sec == time.tv_sec) && (tv_nsec < time.tv_nsec));
51     }
52     bool operator >= (const LogTimeStamp& time) const
53     {
54         return !(*this < time);
55     }
56     bool operator > (const LogTimeStamp& time) const
57     {
58         return (tv_sec > time.tv_sec) ||
59         ((tv_sec == time.tv_sec) && (tv_nsec > time.tv_nsec));
60     }
61     bool operator <= (const LogTimeStamp& time) const
62     {
63         return !(*this > time);
64     }
65     LogTimeStamp operator -= (const LogTimeStamp& time)
66     {
67         if (*this <= time) {
68             return *this = LogTimeStamp(epoch);
69         }
70         if (this->tv_nsec < time.tv_nsec) {
71             --this->tv_sec;
72             this->tv_nsec = NS_PER_SEC + this->tv_nsec - time.tv_nsec;
73         } else {
74             this->tv_nsec -= time.tv_nsec;
75         }
76         this->tv_sec -= time.tv_sec;
77         return *this;
78     }
79     LogTimeStamp operator += (const LogTimeStamp& time)
80     {
81         this->tv_nsec += time.tv_nsec;
82         if (this->tv_nsec >= NS_PER_SEC) {
83             this->tv_nsec -= NS_PER_SEC;
84             ++this->tv_sec;
85         }
86         this->tv_sec += time.tv_sec;
87         return *this;
88     }
SetTimeStamp(uint32_t sec,uint32_t nsec)89     void SetTimeStamp(uint32_t sec, uint32_t nsec)
90     {
91         this->tv_sec = sec;
92         this->tv_nsec = nsec;
93     }
94     static constexpr timespec epoch = {0, 0};
95     uint32_t tv_sec = 0;
96     uint32_t tv_nsec = 0;
97 };
98 
99 #endif