TimeValue.cpp revision 263363
1//===-- 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 "lldb/Host/Config.h"
12
13// C Includes
14#include <stddef.h>
15#include <time.h>
16#include <cstring>
17
18#ifdef _MSC_VER
19#include "lldb/Host/windows/windows.h"
20#endif
21
22// C++ Includes
23// Other libraries and framework includes
24// Project includes
25#include "lldb/Core/Stream.h"
26
27
28using namespace lldb_private;
29
30//----------------------------------------------------------------------
31// TimeValue constructor
32//----------------------------------------------------------------------
33TimeValue::TimeValue() :
34    m_nano_seconds (0)
35{
36}
37
38//----------------------------------------------------------------------
39// TimeValue copy constructor
40//----------------------------------------------------------------------
41TimeValue::TimeValue(const TimeValue& rhs) :
42    m_nano_seconds (rhs.m_nano_seconds)
43{
44}
45
46TimeValue::TimeValue(const struct timespec& ts) :
47    m_nano_seconds ((uint64_t) ts.tv_sec * NanoSecPerSec + ts.tv_nsec)
48{
49}
50
51TimeValue::TimeValue(uint32_t seconds, uint32_t nanos) :
52    m_nano_seconds((uint64_t) seconds * NanoSecPerSec + nanos)
53{
54}
55
56//----------------------------------------------------------------------
57// Destructor
58//----------------------------------------------------------------------
59TimeValue::~TimeValue()
60{
61}
62
63
64uint64_t
65TimeValue::GetAsNanoSecondsSinceJan1_1970() const
66{
67    return m_nano_seconds;
68}
69
70uint64_t
71TimeValue::GetAsMicroSecondsSinceJan1_1970() const
72{
73    return m_nano_seconds / NanoSecPerMicroSec;
74}
75
76uint64_t
77TimeValue::GetAsSecondsSinceJan1_1970() const
78{
79    return m_nano_seconds / NanoSecPerSec;
80}
81
82
83
84struct timespec
85TimeValue::GetAsTimeSpec () const
86{
87    struct timespec ts;
88    ts.tv_sec = m_nano_seconds / NanoSecPerSec;
89    ts.tv_nsec = m_nano_seconds % NanoSecPerSec;
90    return ts;
91}
92
93void
94TimeValue::Clear ()
95{
96    m_nano_seconds = 0;
97}
98
99bool
100TimeValue::IsValid () const
101{
102    return m_nano_seconds != 0;
103}
104
105void
106TimeValue::OffsetWithSeconds (uint64_t sec)
107{
108    m_nano_seconds += sec * NanoSecPerSec;
109}
110
111void
112TimeValue::OffsetWithMicroSeconds (uint64_t usec)
113{
114    m_nano_seconds += usec * NanoSecPerMicroSec;
115}
116
117void
118TimeValue::OffsetWithNanoSeconds (uint64_t nsec)
119{
120    m_nano_seconds += nsec;
121}
122
123TimeValue
124TimeValue::Now()
125{
126    uint32_t seconds, nanoseconds;
127#if _MSC_VER
128    SYSTEMTIME st;
129    GetSystemTime(&st);
130    nanoseconds = st.wMilliseconds * 1000000;
131    FILETIME ft;
132    SystemTimeToFileTime(&st, &ft);
133
134    seconds = ((((uint64_t)ft.dwHighDateTime) << 32 | ft.dwLowDateTime) / 10000000) - 11644473600ULL;
135#else
136    struct timeval tv;
137    gettimeofday(&tv, NULL);
138    seconds = tv.tv_sec;
139    nanoseconds = tv.tv_usec * NanoSecPerMicroSec;
140#endif
141    TimeValue now(seconds, nanoseconds);
142    return now;
143}
144
145//----------------------------------------------------------------------
146// TimeValue assignment operator
147//----------------------------------------------------------------------
148const TimeValue&
149TimeValue::operator=(const TimeValue& rhs)
150{
151    m_nano_seconds = rhs.m_nano_seconds;
152    return *this;
153}
154
155void
156TimeValue::Dump (Stream *s, uint32_t width) const
157{
158    if (s == NULL)
159        return;
160
161#ifndef LLDB_DISABLE_POSIX
162    char time_buf[32];
163    time_t time = GetAsSecondsSinceJan1_1970();
164    char *time_cstr = ::ctime_r(&time, time_buf);
165    if (time_cstr)
166    {
167        char *newline = ::strpbrk(time_cstr, "\n\r");
168        if (newline)
169            *newline = '\0';
170        if (width > 0)
171            s->Printf("%-*s", width, time_cstr);
172        else
173            s->PutCString(time_cstr);
174    }
175    else if (width > 0)
176        s->Printf("%-*s", width, "");
177#endif
178}
179
180bool
181lldb_private::operator == (const TimeValue &lhs, const TimeValue &rhs)
182{
183    return lhs.GetAsNanoSecondsSinceJan1_1970() == rhs.GetAsNanoSecondsSinceJan1_1970();
184}
185
186bool
187lldb_private::operator != (const TimeValue &lhs, const TimeValue &rhs)
188{
189    return lhs.GetAsNanoSecondsSinceJan1_1970() != rhs.GetAsNanoSecondsSinceJan1_1970();
190}
191
192bool
193lldb_private::operator <  (const TimeValue &lhs, const TimeValue &rhs)
194{
195    return lhs.GetAsNanoSecondsSinceJan1_1970() < rhs.GetAsNanoSecondsSinceJan1_1970();
196}
197
198bool
199lldb_private::operator <= (const TimeValue &lhs, const TimeValue &rhs)
200{
201    return lhs.GetAsNanoSecondsSinceJan1_1970() <= rhs.GetAsNanoSecondsSinceJan1_1970();
202}
203
204bool
205lldb_private::operator >  (const TimeValue &lhs, const TimeValue &rhs)
206{
207    return lhs.GetAsNanoSecondsSinceJan1_1970() > rhs.GetAsNanoSecondsSinceJan1_1970();
208}
209
210bool
211lldb_private::operator >= (const TimeValue &lhs, const TimeValue &rhs)
212{
213    return lhs.GetAsNanoSecondsSinceJan1_1970() >= rhs.GetAsNanoSecondsSinceJan1_1970();
214}
215
216uint64_t
217lldb_private::operator - (const TimeValue &lhs, const TimeValue &rhs)
218{
219    return lhs.GetAsNanoSecondsSinceJan1_1970() - rhs.GetAsNanoSecondsSinceJan1_1970();
220}
221
222
223