Apache Mesos
time.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_TIME_HPP__
14 #define __PROCESS_TIME_HPP__
15 
16 #include <iomanip>
17 
18 #include <stout/duration.hpp>
19 
20 namespace process {
21 
22 // Represents an instant in time.
23 class Time
24 {
25 public:
26  // Constructs a time at the Epoch. It is needed because collections
27  // (e.g., std::map) require a default constructor to construct
28  // empty values.
29  Time() : sinceEpoch(Duration::zero()) {}
30 
31  static Time epoch();
32  static Time max();
33 
34  static Try<Time> create(double seconds);
35 
36  Duration duration() const { return sinceEpoch; }
37 
38  double secs() const { return sinceEpoch.secs(); }
39 
40  bool operator<(const Time& t) const { return sinceEpoch < t.sinceEpoch; }
41  bool operator<=(const Time& t) const { return sinceEpoch <= t.sinceEpoch; }
42  bool operator>(const Time& t) const { return sinceEpoch > t.sinceEpoch; }
43  bool operator>=(const Time& t) const { return sinceEpoch >= t.sinceEpoch; }
44  bool operator==(const Time& t) const { return sinceEpoch == t.sinceEpoch; }
45  bool operator!=(const Time& t) const { return sinceEpoch != t.sinceEpoch; }
46 
48  {
49  sinceEpoch += d;
50  return *this;
51  }
52 
54  {
55  sinceEpoch -= d;
56  return *this;
57  }
58 
59  Duration operator-(const Time& that) const
60  {
61  return sinceEpoch - that.sinceEpoch;
62  }
63 
65  {
66  Time new_ = *this;
67  new_ += duration;
68  return new_;
69  }
70 
72  {
73  Time new_ = *this;
74  new_ -= duration;
75  return new_;
76  }
77 
78 private:
79  Duration sinceEpoch;
80 
81  // Made it private to avoid the confusion between Time and Duration.
82  // Users should explicitly use Clock::now() and Time::create() to
83  // create a new time instance.
84  explicit Time(const Duration& _sinceEpoch) : sinceEpoch(_sinceEpoch) {}
85 };
86 
87 inline Time Time::epoch() { return Time(Duration::zero()); }
88 inline Time Time::max() { return Time(Duration::max()); }
89 
90 
91 // Stream manipulator class which serializes Time objects in RFC 1123
92 // format (Also known as HTTP Date format).
93 // The serialization is independent from the locale and ready to be
94 // used in HTTP Headers.
95 // Example: Wed, 15 Nov 1995 04:58:08 GMT
96 // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
97 // section 14.18.
98 // See https://www.ietf.org/rfc/rfc1123.txt section 5.2.14
99 class RFC1123
100 {
101 public:
102  explicit RFC1123(const Time& _time) : time(_time) {}
103 
104 private:
105  friend std::ostream& operator<<(
106  std::ostream& stream,
107  const RFC1123& formatter);
108 
109  const Time time;
110 };
111 
112 
113 std::ostream& operator<<(std::ostream& stream, const RFC1123& formatter);
114 
115 
116 // Stream manipulator class which serializes Time objects in RFC 3339
117 // format.
118 // Example: 1996-12-19T16:39:57-08:00,234
119 class RFC3339
120 {
121 public:
122  explicit RFC3339(const Time& _time) : time(_time) {}
123 
124 private:
125  friend std::ostream& operator<<(std::ostream& stream, const RFC3339& format);
126 
127  const Time time;
128 };
129 
130 
131 std::ostream& operator<<(std::ostream& stream, const RFC3339& formatter);
132 
133 
134 // Outputs the time in RFC 3339 Format.
135 inline std::ostream& operator<<(std::ostream& stream, const Time& time)
136 {
137  stream << RFC3339(time);
138  return stream;
139 }
140 
141 } // namespace process {
142 
143 #endif // __PROCESS_TIME_HPP__
bool operator<(const Time &t) const
Definition: time.hpp:40
std::ostream & operator<<(std::ostream &stream, const Future< T > &future)
Definition: future.hpp:1829
Time operator-(const Duration &duration) const
Definition: time.hpp:71
Definition: check.hpp:33
bool operator>(const Time &t) const
Definition: time.hpp:42
RFC1123(const Time &_time)
Definition: time.hpp:102
Definition: duration.hpp:32
static Try< Time > create(double seconds)
bool operator>=(const Time &t) const
Definition: time.hpp:43
Time()
Definition: time.hpp:29
Time & operator+=(const Duration &d)
Definition: time.hpp:47
bool operator!=(const Time &t) const
Definition: time.hpp:45
double secs() const
Definition: duration.hpp:49
Definition: time.hpp:23
double secs() const
Definition: time.hpp:38
bool operator==(const Time &t) const
Definition: time.hpp:44
static constexpr Duration zero()
Definition: duration.hpp:136
Time operator+(const Duration &duration) const
Definition: time.hpp:64
Definition: executor.hpp:48
static Time max()
Definition: time.hpp:88
RFC3339(const Time &_time)
Definition: time.hpp:122
Definition: time.hpp:119
Duration duration() const
Definition: time.hpp:36
Try< std::string > format(const std::string &fmt, va_list args)
Definition: format.hpp:68
Definition: time.hpp:99
Duration operator-(const Time &that) const
Definition: time.hpp:59
Time & operator-=(const Duration &d)
Definition: time.hpp:53
static constexpr Duration max()
Definition: duration.hpp:429
Try< std::vector< Value > > time(const std::string &hierarchy, const std::string &cgroup)
static Time epoch()
Definition: time.hpp:87
bool operator<=(const Time &t) const
Definition: time.hpp:41