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" |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 11 | |
| 12 | // C Includes |
Greg Clayton | 153ccd7 | 2011-08-10 02:10:13 +0000 | [diff] [blame^] | 13 | #include <stddef.h> |
| 14 | #include <time.h> |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 15 | // C++ Includes |
| 16 | // Other libraries and framework includes |
| 17 | // Project includes |
Greg Clayton | 153ccd7 | 2011-08-10 02:10:13 +0000 | [diff] [blame^] | 18 | #include "lldb/Core/Stream.h" |
| 19 | |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 20 | |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 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) : |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 40 | m_nano_seconds (ts.tv_sec * NanoSecPerSec + ts.tv_nsec) |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
| 44 | TimeValue::TimeValue(const struct timeval& tv) : |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 45 | m_nano_seconds (tv.tv_sec * NanoSecPerSec + tv.tv_usec * NanoSecPerMicroSec) |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 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 | { |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 66 | return m_nano_seconds / NanoSecPerMicroSec; |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Greg Clayton | 153ccd7 | 2011-08-10 02:10:13 +0000 | [diff] [blame^] | 69 | uint64_t |
| 70 | TimeValue::GetAsSecondsSinceJan1_1970() const |
| 71 | { |
| 72 | return m_nano_seconds / NanoSecPerSec; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 77 | struct timespec |
| 78 | TimeValue::GetAsTimeSpec () const |
| 79 | { |
| 80 | struct timespec ts; |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 81 | ts.tv_sec = m_nano_seconds / NanoSecPerSec; |
| 82 | ts.tv_nsec = m_nano_seconds % NanoSecPerSec; |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 83 | return ts; |
| 84 | } |
| 85 | |
| 86 | struct timeval |
| 87 | TimeValue::GetAsTimeVal () const |
| 88 | { |
| 89 | struct timeval tv; |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 90 | tv.tv_sec = m_nano_seconds / NanoSecPerSec; |
| 91 | tv.tv_usec = (m_nano_seconds % NanoSecPerSec) / NanoSecPerMicroSec; |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 92 | return tv; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | TimeValue::Clear () |
| 97 | { |
| 98 | m_nano_seconds = 0; |
| 99 | } |
| 100 | |
| 101 | bool |
| 102 | TimeValue::IsValid () const |
| 103 | { |
| 104 | return m_nano_seconds != 0; |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | TimeValue::OffsetWithSeconds (uint64_t sec) |
| 109 | { |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 110 | m_nano_seconds += sec * NanoSecPerSec; |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void |
| 114 | TimeValue::OffsetWithMicroSeconds (uint64_t usec) |
| 115 | { |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 116 | m_nano_seconds += usec * NanoSecPerMicroSec; |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void |
| 120 | TimeValue::OffsetWithNanoSeconds (uint64_t nsec) |
| 121 | { |
| 122 | m_nano_seconds += nsec; |
| 123 | } |
| 124 | |
| 125 | TimeValue |
| 126 | TimeValue::Now() |
| 127 | { |
| 128 | struct timeval tv; |
| 129 | gettimeofday(&tv, NULL); |
| 130 | TimeValue now(tv); |
| 131 | return now; |
| 132 | } |
| 133 | |
| 134 | //---------------------------------------------------------------------- |
| 135 | // TimeValue assignment operator |
| 136 | //---------------------------------------------------------------------- |
| 137 | const TimeValue& |
| 138 | TimeValue::operator=(const TimeValue& rhs) |
| 139 | { |
| 140 | m_nano_seconds = rhs.m_nano_seconds; |
| 141 | return *this; |
| 142 | } |
| 143 | |
Greg Clayton | 153ccd7 | 2011-08-10 02:10:13 +0000 | [diff] [blame^] | 144 | void |
| 145 | TimeValue::Dump (Stream *s, uint32_t width) const |
| 146 | { |
| 147 | if (s == NULL) |
| 148 | return; |
| 149 | |
| 150 | char time_buf[32]; |
| 151 | time_t time = GetAsSecondsSinceJan1_1970(); |
| 152 | char *time_cstr = ::ctime_r(&time, time_buf); |
| 153 | if (time_cstr) |
| 154 | { |
| 155 | char *newline = ::strpbrk(time_cstr, "\n\r"); |
| 156 | if (newline) |
| 157 | *newline = '\0'; |
| 158 | if (width > 0) |
| 159 | s->Printf("%-*s", width, time_cstr); |
| 160 | else |
| 161 | s->PutCString(time_cstr); |
| 162 | } |
| 163 | else if (width > 0) |
| 164 | s->Printf("%-*s", width, ""); |
| 165 | } |
Eli Friedman | 7c7c19d | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 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 | bool |
| 174 | lldb_private::operator != (const TimeValue &lhs, const TimeValue &rhs) |
| 175 | { |
| 176 | return lhs.GetAsNanoSecondsSinceJan1_1970() != rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 177 | } |
| 178 | |
| 179 | bool |
| 180 | lldb_private::operator < (const TimeValue &lhs, const TimeValue &rhs) |
| 181 | { |
| 182 | return lhs.GetAsNanoSecondsSinceJan1_1970() < rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 183 | } |
| 184 | |
| 185 | bool |
| 186 | lldb_private::operator <= (const TimeValue &lhs, const TimeValue &rhs) |
| 187 | { |
| 188 | return lhs.GetAsNanoSecondsSinceJan1_1970() <= rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 189 | } |
| 190 | |
| 191 | bool |
| 192 | lldb_private::operator > (const TimeValue &lhs, const TimeValue &rhs) |
| 193 | { |
| 194 | return lhs.GetAsNanoSecondsSinceJan1_1970() > rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 195 | } |
| 196 | |
| 197 | bool |
| 198 | lldb_private::operator >= (const TimeValue &lhs, const TimeValue &rhs) |
| 199 | { |
| 200 | return lhs.GetAsNanoSecondsSinceJan1_1970() >= rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 201 | } |
| 202 | |
| 203 | uint64_t |
| 204 | lldb_private::operator - (const TimeValue &lhs, const TimeValue &rhs) |
| 205 | { |
| 206 | return lhs.GetAsNanoSecondsSinceJan1_1970() - rhs.GetAsNanoSecondsSinceJan1_1970(); |
| 207 | } |
| 208 | |
| 209 | |