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