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 12// C Includes 13#include <stddef.h> 14#include <time.h> 15#include <cstring> 16// C++ Includes 17// Other libraries and framework includes 18// Project includes 19#include "lldb/Core/Stream.h" 20 21 22using namespace lldb_private; 23 24//---------------------------------------------------------------------- 25// TimeValue constructor 26//---------------------------------------------------------------------- 27TimeValue::TimeValue() : 28 m_nano_seconds (0) 29{ 30} 31 32//---------------------------------------------------------------------- 33// TimeValue copy constructor 34//---------------------------------------------------------------------- 35TimeValue::TimeValue(const TimeValue& rhs) : 36 m_nano_seconds (rhs.m_nano_seconds) 37{ 38} 39 40TimeValue::TimeValue(const struct timespec& ts) : 41 m_nano_seconds ((uint64_t) ts.tv_sec * NanoSecPerSec + ts.tv_nsec) 42{ 43} 44 45TimeValue::TimeValue(const struct timeval& tv) : 46 m_nano_seconds ((uint64_t) tv.tv_sec * NanoSecPerSec + (uint64_t) tv.tv_usec * NanoSecPerMicroSec) 47{ 48} 49 50//---------------------------------------------------------------------- 51// Destructor 52//---------------------------------------------------------------------- 53TimeValue::~TimeValue() 54{ 55} 56 57 58uint64_t 59TimeValue::GetAsNanoSecondsSinceJan1_1970() const 60{ 61 return m_nano_seconds; 62} 63 64uint64_t 65TimeValue::GetAsMicroSecondsSinceJan1_1970() const 66{ 67 return m_nano_seconds / NanoSecPerMicroSec; 68} 69 70uint64_t 71TimeValue::GetAsSecondsSinceJan1_1970() const 72{ 73 return m_nano_seconds / NanoSecPerSec; 74} 75 76 77 78struct timespec 79TimeValue::GetAsTimeSpec () const 80{ 81 struct timespec ts; 82 ts.tv_sec = m_nano_seconds / NanoSecPerSec; 83 ts.tv_nsec = m_nano_seconds % NanoSecPerSec; 84 return ts; 85} 86 87struct timeval 88TimeValue::GetAsTimeVal () const 89{ 90 struct timeval tv; 91 tv.tv_sec = m_nano_seconds / NanoSecPerSec; 92 tv.tv_usec = (m_nano_seconds % NanoSecPerSec) / NanoSecPerMicroSec; 93 return tv; 94} 95 96void 97TimeValue::Clear () 98{ 99 m_nano_seconds = 0; 100} 101 102bool 103TimeValue::IsValid () const 104{ 105 return m_nano_seconds != 0; 106} 107 108void 109TimeValue::OffsetWithSeconds (uint64_t sec) 110{ 111 m_nano_seconds += sec * NanoSecPerSec; 112} 113 114void 115TimeValue::OffsetWithMicroSeconds (uint64_t usec) 116{ 117 m_nano_seconds += usec * NanoSecPerMicroSec; 118} 119 120void 121TimeValue::OffsetWithNanoSeconds (uint64_t nsec) 122{ 123 m_nano_seconds += nsec; 124} 125 126TimeValue 127TimeValue::Now() 128{ 129 struct timeval tv; 130 gettimeofday(&tv, NULL); 131 TimeValue now(tv); 132 return now; 133} 134 135//---------------------------------------------------------------------- 136// TimeValue assignment operator 137//---------------------------------------------------------------------- 138const TimeValue& 139TimeValue::operator=(const TimeValue& rhs) 140{ 141 m_nano_seconds = rhs.m_nano_seconds; 142 return *this; 143} 144 145void 146TimeValue::Dump (Stream *s, uint32_t width) const 147{ 148 if (s == NULL) 149 return; 150 151 char time_buf[32]; 152 time_t time = GetAsSecondsSinceJan1_1970(); 153 char *time_cstr = ::ctime_r(&time, time_buf); 154 if (time_cstr) 155 { 156 char *newline = ::strpbrk(time_cstr, "\n\r"); 157 if (newline) 158 *newline = '\0'; 159 if (width > 0) 160 s->Printf("%-*s", width, time_cstr); 161 else 162 s->PutCString(time_cstr); 163 } 164 else if (width > 0) 165 s->Printf("%-*s", width, ""); 166} 167 168bool 169lldb_private::operator == (const TimeValue &lhs, const TimeValue &rhs) 170{ 171 return lhs.GetAsNanoSecondsSinceJan1_1970() == rhs.GetAsNanoSecondsSinceJan1_1970(); 172} 173 174bool 175lldb_private::operator != (const TimeValue &lhs, const TimeValue &rhs) 176{ 177 return lhs.GetAsNanoSecondsSinceJan1_1970() != rhs.GetAsNanoSecondsSinceJan1_1970(); 178} 179 180bool 181lldb_private::operator < (const TimeValue &lhs, const TimeValue &rhs) 182{ 183 return lhs.GetAsNanoSecondsSinceJan1_1970() < rhs.GetAsNanoSecondsSinceJan1_1970(); 184} 185 186bool 187lldb_private::operator <= (const TimeValue &lhs, const TimeValue &rhs) 188{ 189 return lhs.GetAsNanoSecondsSinceJan1_1970() <= rhs.GetAsNanoSecondsSinceJan1_1970(); 190} 191 192bool 193lldb_private::operator > (const TimeValue &lhs, const TimeValue &rhs) 194{ 195 return lhs.GetAsNanoSecondsSinceJan1_1970() > rhs.GetAsNanoSecondsSinceJan1_1970(); 196} 197 198bool 199lldb_private::operator >= (const TimeValue &lhs, const TimeValue &rhs) 200{ 201 return lhs.GetAsNanoSecondsSinceJan1_1970() >= rhs.GetAsNanoSecondsSinceJan1_1970(); 202} 203 204uint64_t 205lldb_private::operator - (const TimeValue &lhs, const TimeValue &rhs) 206{ 207 return lhs.GetAsNanoSecondsSinceJan1_1970() - rhs.GetAsNanoSecondsSinceJan1_1970(); 208} 209 210 211