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"
11 #include "lldb/Host/Config.h"
19 #include "lldb/Host/windows/windows.h"
23 // Other libraries and framework includes
25 #include "lldb/Core/Stream.h"
28 using namespace lldb_private;
30 //----------------------------------------------------------------------
31 // TimeValue constructor
32 //----------------------------------------------------------------------
33 TimeValue::TimeValue() :
38 //----------------------------------------------------------------------
39 // TimeValue copy constructor
40 //----------------------------------------------------------------------
41 TimeValue::TimeValue(const TimeValue& rhs) :
42 m_nano_seconds (rhs.m_nano_seconds)
46 TimeValue::TimeValue(const struct timespec& ts) :
47 m_nano_seconds ((uint64_t) ts.tv_sec * NanoSecPerSec + ts.tv_nsec)
51 TimeValue::TimeValue(uint32_t seconds, uint32_t nanos) :
52 m_nano_seconds((uint64_t) seconds * NanoSecPerSec + nanos)
56 //----------------------------------------------------------------------
58 //----------------------------------------------------------------------
59 TimeValue::~TimeValue()
65 TimeValue::GetAsNanoSecondsSinceJan1_1970() const
67 return m_nano_seconds;
71 TimeValue::GetAsMicroSecondsSinceJan1_1970() const
73 return m_nano_seconds / NanoSecPerMicroSec;
77 TimeValue::GetAsSecondsSinceJan1_1970() const
79 return m_nano_seconds / NanoSecPerSec;
85 TimeValue::GetAsTimeSpec () const
88 ts.tv_sec = m_nano_seconds / NanoSecPerSec;
89 ts.tv_nsec = m_nano_seconds % NanoSecPerSec;
100 TimeValue::IsValid () const
102 return m_nano_seconds != 0;
106 TimeValue::OffsetWithSeconds (uint64_t sec)
108 m_nano_seconds += sec * NanoSecPerSec;
112 TimeValue::OffsetWithMicroSeconds (uint64_t usec)
114 m_nano_seconds += usec * NanoSecPerMicroSec;
118 TimeValue::OffsetWithNanoSeconds (uint64_t nsec)
120 m_nano_seconds += nsec;
126 uint32_t seconds, nanoseconds;
130 nanoseconds = st.wMilliseconds * 1000000;
132 SystemTimeToFileTime(&st, &ft);
134 seconds = ((((uint64_t)ft.dwHighDateTime) << 32 | ft.dwLowDateTime) / 10000000) - 11644473600ULL;
137 gettimeofday(&tv, NULL);
139 nanoseconds = tv.tv_usec * NanoSecPerMicroSec;
141 TimeValue now(seconds, nanoseconds);
145 //----------------------------------------------------------------------
146 // TimeValue assignment operator
147 //----------------------------------------------------------------------
149 TimeValue::operator=(const TimeValue& rhs)
151 m_nano_seconds = rhs.m_nano_seconds;
156 TimeValue::Dump (Stream *s, uint32_t width) const
161 #ifndef LLDB_DISABLE_POSIX
163 time_t time = GetAsSecondsSinceJan1_1970();
164 char *time_cstr = ::ctime_r(&time, time_buf);
167 char *newline = ::strpbrk(time_cstr, "\n\r");
171 s->Printf("%-*s", width, time_cstr);
173 s->PutCString(time_cstr);
176 s->Printf("%-*s", width, "");
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();
211 lldb_private::operator >= (const TimeValue &lhs, const TimeValue &rhs)
213 return lhs.GetAsNanoSecondsSinceJan1_1970() >= rhs.GetAsNanoSecondsSinceJan1_1970();
217 lldb_private::operator - (const TimeValue &lhs, const TimeValue &rhs)
219 return lhs.GetAsNanoSecondsSinceJan1_1970() - rhs.GetAsNanoSecondsSinceJan1_1970();