Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 1 | //===-- TimeValue.cpp -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Host/TimeValue.h" |
| 11 | #include <stddef.h> |
| 12 | |
| 13 | // C Includes |
| 14 | // C++ Includes |
| 15 | // Other libraries and framework includes |
| 16 | // Project includes |
| 17 | |
| 18 | #define NSEC_PER_USEC 1000ull |
| 19 | #define USEC_PER_SEC 1000000ull |
| 20 | #define NSEC_PER_SEC 1000000000ull |
| 21 | |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | //---------------------------------------------------------------------- |
| 25 | // TimeValue constructor |
| 26 | //---------------------------------------------------------------------- |
| 27 | TimeValue::TimeValue() : |
| 28 | m_nano_seconds (0) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | //---------------------------------------------------------------------- |
| 33 | // TimeValue copy constructor |
| 34 | //---------------------------------------------------------------------- |
| 35 | TimeValue::TimeValue(const TimeValue& rhs) : |
| 36 | m_nano_seconds (rhs.m_nano_seconds) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | TimeValue::TimeValue(const struct timespec& ts) : |
| 41 | m_nano_seconds (ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | TimeValue::TimeValue(const struct timeval& tv) : |
| 46 | m_nano_seconds (tv.tv_sec * NSEC_PER_SEC + tv.tv_usec * NSEC_PER_USEC) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | //---------------------------------------------------------------------- |
| 51 | // Destructor |
| 52 | //---------------------------------------------------------------------- |
| 53 | TimeValue::~TimeValue() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | |
| 58 | uint64_t |
| 59 | TimeValue::GetAsNanoSecondsSinceJan1_1970() const |
| 60 | { |
| 61 | return m_nano_seconds; |
| 62 | } |
| 63 | |
| 64 | uint64_t |
| 65 | TimeValue::GetAsMicroSecondsSinceJan1_1970() const |
| 66 | { |
| 67 | return m_nano_seconds / NSEC_PER_USEC; |
| 68 | } |
| 69 | |
| 70 | struct timespec |
| 71 | TimeValue::GetAsTimeSpec () const |
| 72 | { |
| 73 | struct timespec ts; |
| 74 | ts.tv_sec = m_nano_seconds / NSEC_PER_SEC; |
| 75 | ts.tv_nsec = m_nano_seconds % NSEC_PER_SEC; |
| 76 | return ts; |
| 77 | } |
| 78 | |
| 79 | struct timeval |
| 80 | TimeValue::GetAsTimeVal () const |
| 81 | { |
| 82 | struct timeval tv; |
| 83 | tv.tv_sec = m_nano_seconds / NSEC_PER_SEC; |
| 84 | tv.tv_usec = (m_nano_seconds % NSEC_PER_SEC) / NSEC_PER_USEC; |
| 85 | return tv; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | TimeValue::Clear () |
| 90 | { |
| 91 | m_nano_seconds = 0; |
| 92 | } |
| 93 | |
| 94 | bool |
| 95 | TimeValue::IsValid () const |
| 96 | { |
| 97 | return m_nano_seconds != 0; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | TimeValue::OffsetWithSeconds (uint64_t sec) |
| 102 | { |
| 103 | m_nano_seconds += sec * NSEC_PER_SEC; |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | TimeValue::OffsetWithMicroSeconds (uint64_t usec) |
| 108 | { |
| 109 | m_nano_seconds += usec * NSEC_PER_USEC; |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | TimeValue::OffsetWithNanoSeconds (uint64_t nsec) |
| 114 | { |
| 115 | m_nano_seconds += nsec; |
| 116 | } |
| 117 | |
| 118 | TimeValue |
| 119 | TimeValue::Now() |
| 120 | { |
| 121 | struct timeval tv; |
| 122 | gettimeofday(&tv, NULL); |
| 123 | TimeValue now(tv); |
| 124 | return now; |
| 125 | } |
| 126 | |
| 127 | //---------------------------------------------------------------------- |
| 128 | // TimeValue assignment operator |
| 129 | //---------------------------------------------------------------------- |
| 130 | const TimeValue& |
| 131 | TimeValue::operator=(const TimeValue& rhs) |
| 132 | { |
| 133 | m_nano_seconds = rhs.m_nano_seconds; |
| 134 | return *this; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | bool |
| 139 | lldb_private::operator == (const TimeValue &lhs, const TimeValue &rhs) |
| 140 | { |
| 141 | return lhs.GetAsNanoSecondsSinceJan1_1970() == rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 142 | } |
| 143 | |
| 144 | bool |
| 145 | lldb_private::operator != (const TimeValue &lhs, const TimeValue &rhs) |
| 146 | { |
| 147 | return lhs.GetAsNanoSecondsSinceJan1_1970() != rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 148 | } |
| 149 | |
| 150 | bool |
| 151 | lldb_private::operator < (const TimeValue &lhs, const TimeValue &rhs) |
| 152 | { |
| 153 | return lhs.GetAsNanoSecondsSinceJan1_1970() < rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 154 | } |
| 155 | |
| 156 | bool |
| 157 | lldb_private::operator <= (const TimeValue &lhs, const TimeValue &rhs) |
| 158 | { |
| 159 | return lhs.GetAsNanoSecondsSinceJan1_1970() <= rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 160 | } |
| 161 | |
| 162 | bool |
| 163 | lldb_private::operator > (const TimeValue &lhs, const TimeValue &rhs) |
| 164 | { |
| 165 | return lhs.GetAsNanoSecondsSinceJan1_1970() > rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 166 | } |
| 167 | |
| 168 | bool |
| 169 | lldb_private::operator >= (const TimeValue &lhs, const TimeValue &rhs) |
| 170 | { |
| 171 | return lhs.GetAsNanoSecondsSinceJan1_1970() >= rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 172 | } |
| 173 | |
| 174 | uint64_t |
| 175 | lldb_private::operator - (const TimeValue &lhs, const TimeValue &rhs) |
| 176 | { |
| 177 | return lhs.GetAsNanoSecondsSinceJan1_1970() - rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 178 | } |
| 179 | |
| 180 | |