blob: d87222982842f807e19604061552aef0a73ff6e2 [file] [log] [blame]
Eli Friedman7c7c19d2010-07-02 19:28:44 +00001//===-- 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 Friedman7c7c19d2010-07-02 19:28:44 +000011
12// C Includes
Greg Clayton153ccd72011-08-10 02:10:13 +000013#include <stddef.h>
14#include <time.h>
Eli Friedman7c7c19d2010-07-02 19:28:44 +000015// C++ Includes
16// Other libraries and framework includes
17// Project includes
Greg Clayton153ccd72011-08-10 02:10:13 +000018#include "lldb/Core/Stream.h"
19
Eli Friedman7c7c19d2010-07-02 19:28:44 +000020
Eli Friedman7c7c19d2010-07-02 19:28:44 +000021using namespace lldb_private;
22
23//----------------------------------------------------------------------
24// TimeValue constructor
25//----------------------------------------------------------------------
26TimeValue::TimeValue() :
27 m_nano_seconds (0)
28{
29}
30
31//----------------------------------------------------------------------
32// TimeValue copy constructor
33//----------------------------------------------------------------------
34TimeValue::TimeValue(const TimeValue& rhs) :
35 m_nano_seconds (rhs.m_nano_seconds)
36{
37}
38
39TimeValue::TimeValue(const struct timespec& ts) :
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000040 m_nano_seconds (ts.tv_sec * NanoSecPerSec + ts.tv_nsec)
Eli Friedman7c7c19d2010-07-02 19:28:44 +000041{
42}
43
44TimeValue::TimeValue(const struct timeval& tv) :
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000045 m_nano_seconds (tv.tv_sec * NanoSecPerSec + tv.tv_usec * NanoSecPerMicroSec)
Eli Friedman7c7c19d2010-07-02 19:28:44 +000046{
47}
48
49//----------------------------------------------------------------------
50// Destructor
51//----------------------------------------------------------------------
52TimeValue::~TimeValue()
53{
54}
55
56
57uint64_t
58TimeValue::GetAsNanoSecondsSinceJan1_1970() const
59{
60 return m_nano_seconds;
61}
62
63uint64_t
64TimeValue::GetAsMicroSecondsSinceJan1_1970() const
65{
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000066 return m_nano_seconds / NanoSecPerMicroSec;
Eli Friedman7c7c19d2010-07-02 19:28:44 +000067}
68
Greg Clayton153ccd72011-08-10 02:10:13 +000069uint64_t
70TimeValue::GetAsSecondsSinceJan1_1970() const
71{
72 return m_nano_seconds / NanoSecPerSec;
73}
74
75
76
Eli Friedman7c7c19d2010-07-02 19:28:44 +000077struct timespec
78TimeValue::GetAsTimeSpec () const
79{
80 struct timespec ts;
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000081 ts.tv_sec = m_nano_seconds / NanoSecPerSec;
82 ts.tv_nsec = m_nano_seconds % NanoSecPerSec;
Eli Friedman7c7c19d2010-07-02 19:28:44 +000083 return ts;
84}
85
86struct timeval
87TimeValue::GetAsTimeVal () const
88{
89 struct timeval tv;
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000090 tv.tv_sec = m_nano_seconds / NanoSecPerSec;
91 tv.tv_usec = (m_nano_seconds % NanoSecPerSec) / NanoSecPerMicroSec;
Eli Friedman7c7c19d2010-07-02 19:28:44 +000092 return tv;
93}
94
95void
96TimeValue::Clear ()
97{
98 m_nano_seconds = 0;
99}
100
101bool
102TimeValue::IsValid () const
103{
104 return m_nano_seconds != 0;
105}
106
107void
108TimeValue::OffsetWithSeconds (uint64_t sec)
109{
Peter Collingbourne20fe30c2011-06-18 23:52:14 +0000110 m_nano_seconds += sec * NanoSecPerSec;
Eli Friedman7c7c19d2010-07-02 19:28:44 +0000111}
112
113void
114TimeValue::OffsetWithMicroSeconds (uint64_t usec)
115{
Peter Collingbourne20fe30c2011-06-18 23:52:14 +0000116 m_nano_seconds += usec * NanoSecPerMicroSec;
Eli Friedman7c7c19d2010-07-02 19:28:44 +0000117}
118
119void
120TimeValue::OffsetWithNanoSeconds (uint64_t nsec)
121{
122 m_nano_seconds += nsec;
123}
124
125TimeValue
126TimeValue::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//----------------------------------------------------------------------
137const TimeValue&
138TimeValue::operator=(const TimeValue& rhs)
139{
140 m_nano_seconds = rhs.m_nano_seconds;
141 return *this;
142}
143
Greg Clayton153ccd72011-08-10 02:10:13 +0000144void
145TimeValue::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 Friedman7c7c19d2010-07-02 19:28:44 +0000166
167bool
168lldb_private::operator == (const TimeValue &lhs, const TimeValue &rhs)
169{
170 return lhs.GetAsNanoSecondsSinceJan1_1970() == rhs.GetAsNanoSecondsSinceJan1_1970();
171}
172
173bool
174lldb_private::operator != (const TimeValue &lhs, const TimeValue &rhs)
175{
176 return lhs.GetAsNanoSecondsSinceJan1_1970() != rhs.GetAsNanoSecondsSinceJan1_1970();
177}
178
179bool
180lldb_private::operator < (const TimeValue &lhs, const TimeValue &rhs)
181{
182 return lhs.GetAsNanoSecondsSinceJan1_1970() < rhs.GetAsNanoSecondsSinceJan1_1970();
183}
184
185bool
186lldb_private::operator <= (const TimeValue &lhs, const TimeValue &rhs)
187{
188 return lhs.GetAsNanoSecondsSinceJan1_1970() <= rhs.GetAsNanoSecondsSinceJan1_1970();
189}
190
191bool
192lldb_private::operator > (const TimeValue &lhs, const TimeValue &rhs)
193{
194 return lhs.GetAsNanoSecondsSinceJan1_1970() > rhs.GetAsNanoSecondsSinceJan1_1970();
195}
196
197bool
198lldb_private::operator >= (const TimeValue &lhs, const TimeValue &rhs)
199{
200 return lhs.GetAsNanoSecondsSinceJan1_1970() >= rhs.GetAsNanoSecondsSinceJan1_1970();
201}
202
203uint64_t
204lldb_private::operator - (const TimeValue &lhs, const TimeValue &rhs)
205{
206 return lhs.GetAsNanoSecondsSinceJan1_1970() - rhs.GetAsNanoSecondsSinceJan1_1970();
207}
208
209