Config: check for 'long long' support
[sol.git] / sol / date_time / utc_time.hpp
blob7954ec706a35aa26786d767b1f06de80d29ddf48
1 #if !defined(sol_date_time_utc_time_hpp_included)
2 #define sol_date_time_utc_time_hpp_included
3 /// \addtogroup sol_date_time
4 /// \{
7 /**
8 * \file sol/date_time/utc_time.hpp
9 * \brief UTC time similar to time_t but with nanosecond resolution
10 * \author Sven Suursoho
14 #include <sol/date_time/duration.hpp>
17 namespace sol { namespace date_time {
20 /**
21 * UTC time similar to std::time_t but with higher resolution
22 * (internally nanosecond, but depending on operating system and hardware
23 * platform it is probably lower)
25 class utc_time
27 public:
29 /// Storage type for number of nanoseconds since Epoch
30 typedef nanoseconds::tick_type tick_type;
33 /// Number of ticks per second
34 static tick_type const ticks_per_second = nanoseconds::ticks_per_second;
37 /// Number of seconds per tick
38 static tick_type const seconds_per_tick = nanoseconds::seconds_per_tick;
41 /// True if resolution is less than 1 second
42 static bool const is_subsecond = nanoseconds::is_subsecond;
45 /// Zero time (i.e. Epoch)
46 utc_time ()
47 : time_(0)
51 /// Dtor
52 ~utc_time ()
56 /// Return number of seconds since Epoch
57 time_t
58 seconds_since_epoch () const
60 return time_/nanoseconds::ticks_per_second;
64 /// Return number of nanoseconds since Epoch
65 nanoseconds
66 nanoseconds_since_epoch () const
68 return time_;
72 /// Return true if this == that
73 bool
74 operator== (utc_time const &that) const
76 return time_ == that.time_;
80 /// Return true if this != that
81 bool
82 operator!= (utc_time const &that) const
84 return time_ != that.time_;
88 /// Return true if this > that
89 bool
90 operator> (utc_time const &that) const
92 return time_ > that.time_;
96 /// Return true if this >= that
97 bool
98 operator>= (utc_time const &that) const
100 return time_ >= that.time_;
104 /// Return true if this < that
105 bool
106 operator< (utc_time const &that) const
108 return time_ < that.time_;
112 /// Return true if this <= that
113 bool
114 operator<= (utc_time const &that) const
116 return time_ <= that.time_;
120 /// Return duration between this and that
121 nanoseconds
122 operator- (utc_time const &that) const
124 return nanoseconds(time_ - that.time_);
128 /// Shift time point by \a d
129 template <typename DurationType>
130 utc_time
131 operator+ (DurationType const &d) const
133 nanoseconds ns(d);
134 return utc_time(time_ + ns.get_count());
138 /// Shift this time point by \a d
139 template <typename DurationType>
140 utc_time &
141 operator+= (DurationType const &d)
143 nanoseconds ns(d);
144 time_ += ns.get_count();
145 return *this;
149 /// Shift time point by \a d
150 template <typename DurationType>
151 utc_time
152 operator- (DurationType const &d) const
154 nanoseconds ns(d);
155 return utc_time(time_ - ns.get_count());
159 /// Shift this time point by \a d
160 template <typename DurationType>
161 utc_time &
162 operator-= (DurationType const &d)
164 nanoseconds ns(d);
165 time_ -= ns.get_count();
166 return *this;
170 private:
172 friend class hiresolution_clock;
174 utc_time (nanoseconds::tick_type t)
175 : time_(t)
178 nanoseconds::tick_type time_;
182 }} // namespace sol::date_time
185 /// \}
186 #endif // sol_date_time_utc_time_hpp_included