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