Fix Idle count
[samplevnf.git] / VNFs / DPPD-PROX / tools / flow_extract / timestamp.cpp
1 /*
2 // Copyright (c) 2010-2017 Intel 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 */
16
17 #include <cstdio>
18 #include <iostream>
19 #include <iomanip>
20
21 #include "timestamp.hpp"
22
23 Timestamp Timestamp::operator-(const Timestamp& other) const
24 {
25         uint64_t sec;
26         uint64_t nsec;
27
28         if (other.m_nsec <= m_nsec) {
29                 nsec = m_nsec - other.m_nsec;
30                 sec = m_sec - other.m_sec;
31         } else {
32                 nsec = (1000000000 + m_nsec) - other.m_nsec;
33                 sec = m_sec - 1 - other.m_sec;
34         }
35
36         return Timestamp(sec, nsec);
37 }
38
39 bool Timestamp::operator>(const Timestamp& other)
40 {
41         return m_sec > other.m_sec ||
42                 (m_sec == other.m_sec && m_nsec > other.m_nsec);
43 }
44
45 bool Timestamp::operator<(const Timestamp& other)
46 {
47         return m_sec < other.m_sec ||
48                 (m_sec == other.m_sec && m_nsec < other.m_nsec);
49 }
50
51 ostream& operator<<(ostream& stream, const Timestamp& ts)
52 {
53         stream << ts.m_sec << "." << setw(9) << setfill('0') << ts.m_nsec;
54         return stream;
55 }
56
57 double operator/(double d, const Timestamp &denominator)
58 {
59         return d * 1000000000 / (denominator.m_sec * 1000000000 + denominator.m_nsec);
60 }
61
62 bool Timestamp::operator==(const Timestamp &other) const
63 {
64         return m_sec == other.m_sec && m_nsec == other.m_nsec;
65 }