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