1 //===-- TimeValue.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/TimeValue.h"
17 // Other libraries and framework includes
19 #include "lldb/Core/Stream.h"
22 using namespace lldb_private;
24 //----------------------------------------------------------------------
25 // TimeValue constructor
26 //----------------------------------------------------------------------
27 TimeValue::TimeValue() :
32 //----------------------------------------------------------------------
33 // TimeValue copy constructor
34 //----------------------------------------------------------------------
35 TimeValue::TimeValue(const TimeValue& rhs) :
36 m_nano_seconds (rhs.m_nano_seconds)
40 TimeValue::TimeValue(const struct timespec& ts) :
41 m_nano_seconds ((uint64_t) ts.tv_sec * NanoSecPerSec + ts.tv_nsec)
45 TimeValue::TimeValue(const struct timeval& tv) :
46 m_nano_seconds ((uint64_t) tv.tv_sec * NanoSecPerSec + (uint64_t) tv.tv_usec * NanoSecPerMicroSec)
50 //----------------------------------------------------------------------
52 //----------------------------------------------------------------------
53 TimeValue::~TimeValue()
59 TimeValue::GetAsNanoSecondsSinceJan1_1970() const
61 return m_nano_seconds;
65 TimeValue::GetAsMicroSecondsSinceJan1_1970() const
67 return m_nano_seconds / NanoSecPerMicroSec;
71 TimeValue::GetAsSecondsSinceJan1_1970() const
73 return m_nano_seconds / NanoSecPerSec;
79 TimeValue::GetAsTimeSpec () const
82 ts.tv_sec = m_nano_seconds / NanoSecPerSec;
83 ts.tv_nsec = m_nano_seconds % NanoSecPerSec;
88 TimeValue::GetAsTimeVal () const
91 tv.tv_sec = m_nano_seconds / NanoSecPerSec;
92 tv.tv_usec = (m_nano_seconds % NanoSecPerSec) / NanoSecPerMicroSec;
103 TimeValue::IsValid () const
105 return m_nano_seconds != 0;
109 TimeValue::OffsetWithSeconds (uint64_t sec)
111 m_nano_seconds += sec * NanoSecPerSec;
115 TimeValue::OffsetWithMicroSeconds (uint64_t usec)
117 m_nano_seconds += usec * NanoSecPerMicroSec;
121 TimeValue::OffsetWithNanoSeconds (uint64_t nsec)
123 m_nano_seconds += nsec;
130 gettimeofday(&tv, NULL);
135 //----------------------------------------------------------------------
136 // TimeValue assignment operator
137 //----------------------------------------------------------------------
139 TimeValue::operator=(const TimeValue& rhs)
141 m_nano_seconds = rhs.m_nano_seconds;
146 TimeValue::Dump (Stream *s, uint32_t width) const
152 time_t time = GetAsSecondsSinceJan1_1970();
153 char *time_cstr = ::ctime_r(&time, time_buf);
156 char *newline = ::strpbrk(time_cstr, "\n\r");
160 s->Printf("%-*s", width, time_cstr);
162 s->PutCString(time_cstr);
165 s->Printf("%-*s", width, "");
169 lldb_private::operator == (const TimeValue &lhs, const TimeValue &rhs)
171 return lhs.GetAsNanoSecondsSinceJan1_1970() == rhs.GetAsNanoSecondsSinceJan1_1970();
175 lldb_private::operator != (const TimeValue &lhs, const TimeValue &rhs)
177 return lhs.GetAsNanoSecondsSinceJan1_1970() != rhs.GetAsNanoSecondsSinceJan1_1970();
181 lldb_private::operator < (const TimeValue &lhs, const TimeValue &rhs)
183 return lhs.GetAsNanoSecondsSinceJan1_1970() < rhs.GetAsNanoSecondsSinceJan1_1970();
187 lldb_private::operator <= (const TimeValue &lhs, const TimeValue &rhs)
189 return lhs.GetAsNanoSecondsSinceJan1_1970() <= rhs.GetAsNanoSecondsSinceJan1_1970();
193 lldb_private::operator > (const TimeValue &lhs, const TimeValue &rhs)
195 return lhs.GetAsNanoSecondsSinceJan1_1970() > rhs.GetAsNanoSecondsSinceJan1_1970();
199 lldb_private::operator >= (const TimeValue &lhs, const TimeValue &rhs)
201 return lhs.GetAsNanoSecondsSinceJan1_1970() >= rhs.GetAsNanoSecondsSinceJan1_1970();
205 lldb_private::operator - (const TimeValue &lhs, const TimeValue &rhs)
207 return lhs.GetAsNanoSecondsSinceJan1_1970() - rhs.GetAsNanoSecondsSinceJan1_1970();