1170613Sbms//===-- TimeValue.cpp -------------------------------------------*- C++ -*-===// 2189592Sbms// 3170613Sbms// The LLVM Compiler Infrastructure 4170613Sbms// 5170613Sbms// This file is distributed under the University of Illinois Open Source 6170613Sbms// License. See LICENSE.TXT for details. 7170613Sbms// 8170613Sbms//===----------------------------------------------------------------------===// 9170613Sbms 10170613Sbms#include "lldb/Host/TimeValue.h" 11170613Sbms#include "lldb/Host/Config.h" 12170613Sbms 13170613Sbms// C Includes 14170613Sbms#include <stddef.h> 15170613Sbms#include <time.h> 16170613Sbms#include <cstring> 17170613Sbms 18170613Sbms#ifdef _MSC_VER 19170613Sbms#include "lldb/Host/windows/windows.h" 20170613Sbms#endif 21170613Sbms 22170613Sbms// C++ Includes 23170613Sbms// Other libraries and framework includes 24170613Sbms// Project includes 25170613Sbms#include "lldb/Core/Stream.h" 26170613Sbms 27170613Sbms 28170613Sbmsusing namespace lldb_private; 29170613Sbms 30170613Sbms//---------------------------------------------------------------------- 31170613Sbms// TimeValue constructor 32170613Sbms//---------------------------------------------------------------------- 33170613SbmsTimeValue::TimeValue() : 34170613Sbms m_nano_seconds (0) 35170613Sbms{ 36170613Sbms} 37170613Sbms 38170613Sbms//---------------------------------------------------------------------- 39170613Sbms// TimeValue copy constructor 40170613Sbms//---------------------------------------------------------------------- 41170613SbmsTimeValue::TimeValue(const TimeValue& rhs) : 42170613Sbms m_nano_seconds (rhs.m_nano_seconds) 43171746Scsjp{ 44170613Sbms} 45170613Sbms 46189592SbmsTimeValue::TimeValue(const struct timespec& ts) : 47170613Sbms m_nano_seconds ((uint64_t) ts.tv_sec * NanoSecPerSec + ts.tv_nsec) 48189592Sbms{ 49228969Sjhb} 50189592Sbms 51170613SbmsTimeValue::TimeValue(uint32_t seconds, uint32_t nanos) : 52170613Sbms m_nano_seconds((uint64_t) seconds * NanoSecPerSec + nanos) 53170613Sbms{ 54170613Sbms} 55185571Sbz 56170613Sbms//---------------------------------------------------------------------- 57170613Sbms// Destructor 58170613Sbms//---------------------------------------------------------------------- 59170613SbmsTimeValue::~TimeValue() 60170613Sbms{ 61170613Sbms} 62170613Sbms 63170613Sbms 64189592Sbmsuint64_t 65191659SbmsTimeValue::GetAsNanoSecondsSinceJan1_1970() const 66189592Sbms{ 67189592Sbms return m_nano_seconds; 68170613Sbms} 69170613Sbms 70170613Sbmsuint64_t 71170613SbmsTimeValue::GetAsMicroSecondsSinceJan1_1970() const 72170613Sbms{ 73170613Sbms return m_nano_seconds / NanoSecPerMicroSec; 74170613Sbms} 75170613Sbms 76170613Sbmsuint64_t 77170613SbmsTimeValue::GetAsSecondsSinceJan1_1970() const 78170613Sbms{ 79189592Sbms return m_nano_seconds / NanoSecPerSec; 80189592Sbms} 81170613Sbms 82170613Sbms 83189592Sbms 84189592Sbmsstruct timespec 85170613SbmsTimeValue::GetAsTimeSpec () const 86170613Sbms{ 87189592Sbms struct timespec ts; 88189592Sbms ts.tv_sec = m_nano_seconds / NanoSecPerSec; 89189592Sbms ts.tv_nsec = m_nano_seconds % NanoSecPerSec; 90189592Sbms return ts; 91189592Sbms} 92189592Sbms 93189592Sbmsvoid 94189592SbmsTimeValue::Clear () 95189592Sbms{ 96170613Sbms m_nano_seconds = 0; 97170613Sbms} 98189592Sbms 99170613Sbmsbool 100170613SbmsTimeValue::IsValid () const 101170613Sbms{ 102170613Sbms return m_nano_seconds != 0; 103189592Sbms} 104170613Sbms 105170613Sbmsvoid 106189592SbmsTimeValue::OffsetWithSeconds (uint64_t sec) 107189592Sbms{ 108189592Sbms m_nano_seconds += sec * NanoSecPerSec; 109189592Sbms} 110170613Sbms 111170613Sbmsvoid 112170613SbmsTimeValue::OffsetWithMicroSeconds (uint64_t usec) 113170613Sbms{ 114189592Sbms m_nano_seconds += usec * NanoSecPerMicroSec; 115189592Sbms} 116189592Sbms 117170613Sbmsvoid 118189592SbmsTimeValue::OffsetWithNanoSeconds (uint64_t nsec) 119189592Sbms{ 120189592Sbms m_nano_seconds += nsec; 121189592Sbms} 122189592Sbms 123189592SbmsTimeValue 124189592SbmsTimeValue::Now() 125189592Sbms{ 126189592Sbms uint32_t seconds, nanoseconds; 127189592Sbms#if _MSC_VER 128189592Sbms SYSTEMTIME st; 129189592Sbms GetSystemTime(&st); 130170613Sbms nanoseconds = st.wMilliseconds * 1000000; 131189592Sbms FILETIME ft; 132189592Sbms SystemTimeToFileTime(&st, &ft); 133189592Sbms 134189592Sbms seconds = ((((uint64_t)ft.dwHighDateTime) << 32 | ft.dwLowDateTime) / 10000000) - 11644473600ULL; 135189592Sbms#else 136189592Sbms struct timeval tv; 137189592Sbms gettimeofday(&tv, NULL); 138189592Sbms seconds = tv.tv_sec; 139189592Sbms nanoseconds = tv.tv_usec * NanoSecPerMicroSec; 140189592Sbms#endif 141189592Sbms TimeValue now(seconds, nanoseconds); 142259982Sdim return now; 143189592Sbms} 144259982Sdim 145189592Sbms//---------------------------------------------------------------------- 146189592Sbms// TimeValue assignment operator 147189592Sbms//---------------------------------------------------------------------- 148170613Sbmsconst TimeValue& 149170613SbmsTimeValue::operator=(const TimeValue& rhs) 150228969Sjhb{ 151228969Sjhb m_nano_seconds = rhs.m_nano_seconds; 152170613Sbms return *this; 153170613Sbms} 154170613Sbms 155189592Sbmsvoid 156189592SbmsTimeValue::Dump (Stream *s, uint32_t width) const 157189592Sbms{ 158189592Sbms if (s == NULL) 159170613Sbms return; 160170613Sbms 161189592Sbms#ifndef LLDB_DISABLE_POSIX 162170613Sbms char time_buf[32]; 163227309Sed time_t time = GetAsSecondsSinceJan1_1970(); 164227309Sed char *time_cstr = ::ctime_r(&time, time_buf); 165189357Sbms if (time_cstr) 166189592Sbms { 167189592Sbms char *newline = ::strpbrk(time_cstr, "\n\r"); 168189592Sbms if (newline) 169189592Sbms *newline = '\0'; 170189592Sbms if (width > 0) 171189592Sbms s->Printf("%-*s", width, time_cstr); 172189592Sbms else 173189592Sbms s->PutCString(time_cstr); 174189592Sbms } 175189592Sbms else if (width > 0) 176189592Sbms s->Printf("%-*s", width, ""); 177189592Sbms#endif 178189357Sbms} 179189357Sbms 180189357Sbmsbool 181189357Sbmslldb_private::operator == (const TimeValue &lhs, const TimeValue &rhs) 182189357Sbms{ 183227309Sed return lhs.GetAsNanoSecondsSinceJan1_1970() == rhs.GetAsNanoSecondsSinceJan1_1970(); 184189592Sbms} 185189592Sbms 186189592Sbmsbool 187228969Sjhblldb_private::operator != (const TimeValue &lhs, const TimeValue &rhs) 188228969Sjhb{ 189228969Sjhb return lhs.GetAsNanoSecondsSinceJan1_1970() != rhs.GetAsNanoSecondsSinceJan1_1970(); 190228969Sjhb} 191259982Sdim 192170613Sbmsbool 193189592Sbmslldb_private::operator < (const TimeValue &lhs, const TimeValue &rhs) 194189592Sbms{ 195189592Sbms return lhs.GetAsNanoSecondsSinceJan1_1970() < rhs.GetAsNanoSecondsSinceJan1_1970(); 196189592Sbms} 197189592Sbms 198189592Sbmsbool 199189592Sbmslldb_private::operator <= (const TimeValue &lhs, const TimeValue &rhs) 200189592Sbms{ 201189592Sbms return lhs.GetAsNanoSecondsSinceJan1_1970() <= rhs.GetAsNanoSecondsSinceJan1_1970(); 202189592Sbms} 203189592Sbms 204189592Sbmsbool 205189592Sbmslldb_private::operator > (const TimeValue &lhs, const TimeValue &rhs) 206189592Sbms{ 207189592Sbms return lhs.GetAsNanoSecondsSinceJan1_1970() > rhs.GetAsNanoSecondsSinceJan1_1970(); 208189592Sbms} 209189592Sbms 210189592Sbmsbool 211189592Sbmslldb_private::operator >= (const TimeValue &lhs, const TimeValue &rhs) 212189592Sbms{ 213189592Sbms return lhs.GetAsNanoSecondsSinceJan1_1970() >= rhs.GetAsNanoSecondsSinceJan1_1970(); 214259982Sdim} 215189592Sbms 216189592Sbmsuint64_t 217189592Sbmslldb_private::operator - (const TimeValue &lhs, const TimeValue &rhs) 218189592Sbms{ 219189592Sbms return lhs.GetAsNanoSecondsSinceJan1_1970() - rhs.GetAsNanoSecondsSinceJan1_1970(); 220189592Sbms} 221189592Sbms 222189592Sbms 223189592Sbms