blob: 27aad5f7bc7de573e70a8177b51ba0f4b66b4015 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +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 "TimeValue.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16
17#define NSEC_PER_USEC 1000ull
18#define USEC_PER_SEC 1000000ull
19#define NSEC_PER_SEC 1000000000ull
20
21using 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) :
40 m_nano_seconds (ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec)
41{
42}
43
44TimeValue::TimeValue(const struct timeval& tv) :
45 m_nano_seconds (tv.tv_sec * NSEC_PER_SEC + tv.tv_usec * NSEC_PER_USEC)
46{
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{
66 return m_nano_seconds / NSEC_PER_USEC;
67}
68
69struct timespec
70TimeValue::GetAsTimeSpec () const
71{
72 struct timespec ts;
73 ts.tv_sec = m_nano_seconds / NSEC_PER_SEC;
74 ts.tv_nsec = m_nano_seconds % NSEC_PER_SEC;
75 return ts;
76}
77
78struct timeval
79TimeValue::GetAsTimeVal () const
80{
81 struct timeval tv;
82 tv.tv_sec = m_nano_seconds / NSEC_PER_SEC;
83 tv.tv_usec = (m_nano_seconds % NSEC_PER_SEC) / NSEC_PER_USEC;
84 return tv;
85}
86
87void
88TimeValue::Clear ()
89{
90 m_nano_seconds = 0;
91}
92
93bool
94TimeValue::IsValid () const
95{
96 return m_nano_seconds != 0;
97}
98
99void
100TimeValue::OffsetWithSeconds (uint32_t sec)
101{
102 m_nano_seconds += sec * NSEC_PER_SEC;
103}
104
105void
106TimeValue::OffsetWithMicroSeconds (uint32_t usec)
107{
108 m_nano_seconds += usec * NSEC_PER_USEC;
109}
110
111void
112TimeValue::OffsetWithNanoSeconds (uint32_t nsec)
113{
114 m_nano_seconds += nsec;
115}
116
117TimeValue
118TimeValue::Now()
119{
120 struct timeval tv;
121 gettimeofday(&tv, NULL);
122 TimeValue now(tv);
123 return now;
124}
125
126//----------------------------------------------------------------------
127// TimeValue assignment operator
128//----------------------------------------------------------------------
129const TimeValue&
130TimeValue::operator=(const TimeValue& rhs)
131{
132 m_nano_seconds = rhs.m_nano_seconds;
133 return *this;
134}
135
136
137bool
138lldb_private::operator == (const TimeValue &lhs, const TimeValue &rhs)
139{
140 return lhs.GetAsNanoSecondsSinceJan1_1970() == rhs.GetAsNanoSecondsSinceJan1_1970();
141}
142
143bool
144lldb_private::operator != (const TimeValue &lhs, const TimeValue &rhs)
145{
146 return lhs.GetAsNanoSecondsSinceJan1_1970() != rhs.GetAsNanoSecondsSinceJan1_1970();
147}
148
149bool
150lldb_private::operator < (const TimeValue &lhs, const TimeValue &rhs)
151{
152 return lhs.GetAsNanoSecondsSinceJan1_1970() < rhs.GetAsNanoSecondsSinceJan1_1970();
153}
154
155bool
156lldb_private::operator <= (const TimeValue &lhs, const TimeValue &rhs)
157{
158 return lhs.GetAsNanoSecondsSinceJan1_1970() <= rhs.GetAsNanoSecondsSinceJan1_1970();
159}
160
161bool
162lldb_private::operator > (const TimeValue &lhs, const TimeValue &rhs)
163{
164 return lhs.GetAsNanoSecondsSinceJan1_1970() > rhs.GetAsNanoSecondsSinceJan1_1970();
165}
166
167bool
168lldb_private::operator >= (const TimeValue &lhs, const TimeValue &rhs)
169{
170 return lhs.GetAsNanoSecondsSinceJan1_1970() >= rhs.GetAsNanoSecondsSinceJan1_1970();
171}
172
173uint64_t
174lldb_private::operator - (const TimeValue &lhs, const TimeValue &rhs)
175{
176 return lhs.GetAsNanoSecondsSinceJan1_1970() - rhs.GetAsNanoSecondsSinceJan1_1970();
177}
178
179