blob: 416db41236d17d6e5d1f9f227d92ea84d2aa71b2 [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
Eli Friedman7c7c19d2010-07-02 19:28:44 +000018using namespace lldb_private;
19
20//----------------------------------------------------------------------
21// TimeValue constructor
22//----------------------------------------------------------------------
23TimeValue::TimeValue() :
24 m_nano_seconds (0)
25{
26}
27
28//----------------------------------------------------------------------
29// TimeValue copy constructor
30//----------------------------------------------------------------------
31TimeValue::TimeValue(const TimeValue& rhs) :
32 m_nano_seconds (rhs.m_nano_seconds)
33{
34}
35
36TimeValue::TimeValue(const struct timespec& ts) :
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000037 m_nano_seconds (ts.tv_sec * NanoSecPerSec + ts.tv_nsec)
Eli Friedman7c7c19d2010-07-02 19:28:44 +000038{
39}
40
41TimeValue::TimeValue(const struct timeval& tv) :
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000042 m_nano_seconds (tv.tv_sec * NanoSecPerSec + tv.tv_usec * NanoSecPerMicroSec)
Eli Friedman7c7c19d2010-07-02 19:28:44 +000043{
44}
45
46//----------------------------------------------------------------------
47// Destructor
48//----------------------------------------------------------------------
49TimeValue::~TimeValue()
50{
51}
52
53
54uint64_t
55TimeValue::GetAsNanoSecondsSinceJan1_1970() const
56{
57 return m_nano_seconds;
58}
59
60uint64_t
61TimeValue::GetAsMicroSecondsSinceJan1_1970() const
62{
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000063 return m_nano_seconds / NanoSecPerMicroSec;
Eli Friedman7c7c19d2010-07-02 19:28:44 +000064}
65
66struct timespec
67TimeValue::GetAsTimeSpec () const
68{
69 struct timespec ts;
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000070 ts.tv_sec = m_nano_seconds / NanoSecPerSec;
71 ts.tv_nsec = m_nano_seconds % NanoSecPerSec;
Eli Friedman7c7c19d2010-07-02 19:28:44 +000072 return ts;
73}
74
75struct timeval
76TimeValue::GetAsTimeVal () const
77{
78 struct timeval tv;
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000079 tv.tv_sec = m_nano_seconds / NanoSecPerSec;
80 tv.tv_usec = (m_nano_seconds % NanoSecPerSec) / NanoSecPerMicroSec;
Eli Friedman7c7c19d2010-07-02 19:28:44 +000081 return tv;
82}
83
84void
85TimeValue::Clear ()
86{
87 m_nano_seconds = 0;
88}
89
90bool
91TimeValue::IsValid () const
92{
93 return m_nano_seconds != 0;
94}
95
96void
97TimeValue::OffsetWithSeconds (uint64_t sec)
98{
Peter Collingbourne20fe30c2011-06-18 23:52:14 +000099 m_nano_seconds += sec * NanoSecPerSec;
Eli Friedman7c7c19d2010-07-02 19:28:44 +0000100}
101
102void
103TimeValue::OffsetWithMicroSeconds (uint64_t usec)
104{
Peter Collingbourne20fe30c2011-06-18 23:52:14 +0000105 m_nano_seconds += usec * NanoSecPerMicroSec;
Eli Friedman7c7c19d2010-07-02 19:28:44 +0000106}
107
108void
109TimeValue::OffsetWithNanoSeconds (uint64_t nsec)
110{
111 m_nano_seconds += nsec;
112}
113
114TimeValue
115TimeValue::Now()
116{
117 struct timeval tv;
118 gettimeofday(&tv, NULL);
119 TimeValue now(tv);
120 return now;
121}
122
123//----------------------------------------------------------------------
124// TimeValue assignment operator
125//----------------------------------------------------------------------
126const TimeValue&
127TimeValue::operator=(const TimeValue& rhs)
128{
129 m_nano_seconds = rhs.m_nano_seconds;
130 return *this;
131}
132
133
134bool
135lldb_private::operator == (const TimeValue &lhs, const TimeValue &rhs)
136{
137 return lhs.GetAsNanoSecondsSinceJan1_1970() == rhs.GetAsNanoSecondsSinceJan1_1970();
138}
139
140bool
141lldb_private::operator != (const TimeValue &lhs, const TimeValue &rhs)
142{
143 return lhs.GetAsNanoSecondsSinceJan1_1970() != rhs.GetAsNanoSecondsSinceJan1_1970();
144}
145
146bool
147lldb_private::operator < (const TimeValue &lhs, const TimeValue &rhs)
148{
149 return lhs.GetAsNanoSecondsSinceJan1_1970() < rhs.GetAsNanoSecondsSinceJan1_1970();
150}
151
152bool
153lldb_private::operator <= (const TimeValue &lhs, const TimeValue &rhs)
154{
155 return lhs.GetAsNanoSecondsSinceJan1_1970() <= rhs.GetAsNanoSecondsSinceJan1_1970();
156}
157
158bool
159lldb_private::operator > (const TimeValue &lhs, const TimeValue &rhs)
160{
161 return lhs.GetAsNanoSecondsSinceJan1_1970() > rhs.GetAsNanoSecondsSinceJan1_1970();
162}
163
164bool
165lldb_private::operator >= (const TimeValue &lhs, const TimeValue &rhs)
166{
167 return lhs.GetAsNanoSecondsSinceJan1_1970() >= rhs.GetAsNanoSecondsSinceJan1_1970();
168}
169
170uint64_t
171lldb_private::operator - (const TimeValue &lhs, const TimeValue &rhs)
172{
173 return lhs.GetAsNanoSecondsSinceJan1_1970() - rhs.GetAsNanoSecondsSinceJan1_1970();
174}
175
176