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