aboutsummaryrefslogtreecommitdiff
path: root/lldb/include/lldb/Host/TimeValue.h
blob: e85642380e6ddb735d35af9a810dc4b0b8915939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//===-- TimeValue.h ---------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_TimeValue_h_
#define liblldb_TimeValue_h_

// C Includes
#include "lldb/Host/PosixApi.h"
#include <stdint.h>

// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/lldb-private.h"

namespace lldb_private {

class TimeValue {
public:
  static const uint64_t MicroSecPerSec = 1000000UL;
  static const uint64_t NanoSecPerSec = 1000000000UL;
  static const uint64_t NanoSecPerMicroSec = 1000U;
  static const uint64_t NanoSecPerMilliSec = 1000000UL;

  //------------------------------------------------------------------
  // Constructors and Destructors
  //------------------------------------------------------------------
  TimeValue();
  TimeValue(const TimeValue &rhs);
  TimeValue(const struct timespec &ts);
  explicit TimeValue(uint32_t seconds, uint64_t nanos = 0);
  ~TimeValue();

  //------------------------------------------------------------------
  // Operators
  //------------------------------------------------------------------
  const TimeValue &operator=(const TimeValue &rhs);

  void Clear();

  uint64_t GetAsNanoSecondsSinceJan1_1970() const;

  uint64_t GetAsMicroSecondsSinceJan1_1970() const;

  uint64_t GetAsSecondsSinceJan1_1970() const;

  struct timespec GetAsTimeSpec() const;

  bool IsValid() const;

  void OffsetWithSeconds(uint64_t sec);

  void OffsetWithMicroSeconds(uint64_t usec);

  void OffsetWithNanoSeconds(uint64_t nsec);

  static TimeValue Now();

  void Dump(Stream *s, uint32_t width = 0) const;

  /// Returns only the seconds component of the TimeValue. The nanoseconds
  /// portion is ignored. No rounding is performed.
  /// @brief Retrieve the seconds component
  uint32_t seconds() const { return m_nano_seconds / NanoSecPerSec; }

  /// Returns only the nanoseconds component of the TimeValue. The seconds
  /// portion is ignored.
  /// @brief Retrieve the nanoseconds component.
  uint32_t nanoseconds() const { return m_nano_seconds % NanoSecPerSec; }

  /// Returns only the fractional portion of the TimeValue rounded down to the
  /// nearest microsecond (divide by one thousand).
  /// @brief Retrieve the fractional part as microseconds;
  uint32_t microseconds() const {
    return (m_nano_seconds % NanoSecPerSec) / NanoSecPerMicroSec;
  }

  /// Returns only the fractional portion of the TimeValue rounded down to the
  /// nearest millisecond (divide by one million).
  /// @brief Retrieve the milliseconds component;
  uint32_t milliseconds() const { return m_nano_seconds / NanoSecPerMilliSec; }

protected:
  //------------------------------------------------------------------
  // Classes that inherit from TimeValue can see and modify these
  //------------------------------------------------------------------
  uint64_t m_nano_seconds;
};

bool operator==(const TimeValue &lhs, const TimeValue &rhs);
bool operator!=(const TimeValue &lhs, const TimeValue &rhs);
bool operator<(const TimeValue &lhs, const TimeValue &rhs);
bool operator<=(const TimeValue &lhs, const TimeValue &rhs);
bool operator>(const TimeValue &lhs, const TimeValue &rhs);
bool operator>=(const TimeValue &lhs, const TimeValue &rhs);

uint64_t operator-(const TimeValue &lhs, const TimeValue &rhs);

} // namespace lldb_private

#endif // liblldb_TimeValue_h_