Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
datetime.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_DATETIME_H_
20 #define ION_BASE_DATETIME_H_
21 
22 #include <chrono> // NOLINT
23 #include <cmath>
24 #include <sstream>
25 #include <string>
26 #include <type_traits>
27 
28 #include "base/integral_types.h"
29 #include "ion/base/referent.h"
30 #include "ion/external/gtest/gunit_prod.h" // For FRIEND_TEST().
31 
32 namespace ion {
33 namespace base {
34 
45 class DateTime {
46  public:
52  kYear = 0,
53  kMonth = 1,
54  kDay = 2,
55  kHour = 3,
56  kMinute = 4,
57  kSecond = 5,
60  };
61 
63  class Range {
64  public:
65  Range() {
66  begin_.reset(new DateTime);
67  end_.reset(new DateTime);
68  }
69  Range(const Range& rhs) {
70  *begin_ = rhs.begin();
71  *end_ = rhs.end();
72  }
73  ~Range() {}
74 
75  const DateTime& begin() const { return *begin_; }
76  const DateTime& end() const { return *end_; }
77 
78  void SetBegin(const DateTime& begin) { *begin_ = begin; }
79  void SetEnd(const DateTime& end) { *end_ = end; }
80 
82  const Range& end,
83  float t) {
84  *begin_ = DateTime::Interpolate(begin.begin(), end.begin(), t);
85  *end_ = DateTime::Interpolate(begin.end(), end.end(), t);
86  }
87 
88  static Range Interpolate(const Range& begin,
89  const Range& end,
90  float t) {
91  Range interpolated;
92  interpolated.SetInterpolation(begin, end, t);
93  return interpolated;
94  }
95 
96  private:
97  std::unique_ptr<DateTime> begin_;
98  std::unique_ptr<DateTime> end_;
99  };
100 
101  DateTime();
102  DateTime(const DateTime& rhs);
103 
106  DateTime(int64 year, uint8 month, uint8 day, uint8 hour, uint8 minute,
107  uint8 second, uint32 nanosec = 0, int8 zone_hours = 0,
108  int8 zone_minutes = 0);
109 
110  explicit DateTime(std::chrono::system_clock::time_point time,
111  int8 zone_hours = 0, int8 zone_minutes = 0);
112 
117  template <typename Rep>
118  static DateTime CreateFromPosixSeconds(Rep secs, int8 zone_hours = 0,
119  int8 zone_minutes = 0) {
120  DateTime date;
121  date.Reset();
123  date.SetFromPosixSecondsOnly(static_cast<int64>(secs), zone_hours,
124  zone_minutes);
125  } else {
128  secs += static_cast<Rep>(0.5e-9);
129  const Rep seconds = static_cast<Rep>(std::floor(secs));
130  const Rep nanoseconds = static_cast<Rep>(1e9) * (secs - seconds);
131  date.SetFromPosixSecondsOnly(static_cast<int64>(seconds), zone_hours,
132  zone_minutes);
133  date.nanosecond_ = static_cast<uint32>(nanoseconds);
134  }
135  return date;
136  }
137 
138  virtual ~DateTime() {}
139 
141  int64 GetYear() const { return year_; }
142  uint8 GetMonth() const { return month_; }
143  uint8 GetDay() const { return day_; }
144  uint8 GetHour() const { return hour_; }
145  uint8 GetMinute() const { return minute_; }
146  uint8 GetSecond() const { return second_; }
147  uint32 GetNanosecond() const { return nanosecond_; }
148  int8 GetZoneHours() const { return zone_hours_; }
149  int8 GetZoneMinutes() const { return zone_minutes_; }
150 
152  void SetYear(int64 year);
153  void SetMonth(uint8 month);
154  void SetDay(uint8 day);
155  void SetHour(uint8 hour);
156  void SetMinute(uint8 minute);
157  void SetSecond(uint8 second);
158  void SetNanosecond(uint32 nanosecond);
159  void SetZoneHours(int8 zone_hours);
160  void SetZoneMinutes(int8 zone_minutes);
161 
164  void Reset();
165 
166  void Set(int64 years,
167  uint8 months,
168  uint8 days,
169  uint8 hours,
170  uint8 minutes,
171  uint8 seconds,
172  int8 zone_hours,
173  int8 zone_minutes);
174 
175  void Set(int64 years,
176  uint8 months,
177  uint8 days,
178  uint8 hours,
179  uint8 minutes,
180  uint8 seconds,
181  uint32 nanoseconds,
182  int8 zone_hours,
183  int8 zone_minutes);
184 
185  void Set(const DateTime& other);
186 
188  void Normalize() { AdjustTimeZone(0, 0); }
189 
191  bool IsEqualByComponent(const DateTime& dtime) const;
192 
195  bool operator==(const DateTime& dtime) const;
196 
197  bool operator>(const DateTime& dtime) const;
198  bool operator<(const DateTime& dtime) const { return dtime > *this; }
199  bool operator>=(const DateTime& dtime) const { return !(*this < dtime); }
200  bool operator<=(const DateTime& dtime) const { return !(*this > dtime); }
201  bool operator!=(const DateTime& dtime) const { return !(*this == dtime); }
202  DateTime& operator=(const DateTime& rhs);
203 
205  std::chrono::system_clock::time_point GetTimePoint() const;
206 
209  template <typename Rep = double>
210  Rep GetPosixSeconds() const {
211  const int64 posix_seconds = GetPosixSecondsOnly();
212  return static_cast<Rep>(posix_seconds) +
213  static_cast<Rep>(1e-9) * nanosecond_;
214  }
215 
219  void Lerp(const DateTime& origin, const DateTime& target, double t);
220 
222  void AdjustTimeZone(int newHours, int newMins);
223 
228  std::string ToString() const;
229 
233  bool FromString(const std::string& str);
234 
242  };
247  };
248  void ComputeDateString(const DateStringEnum output_date_format,
249  std::string* out_string) const;
250  void ComputeTimeString(const TimeStringEnum output_time_format,
251  std::string* out_string) const;
252 
256  std::string ComputeDurationString(double fractional_seconds) const;
257 
266  double GetJulianDate() const;
267 
275  double GetTimeAsDecimal() const;
276 
279  double GetJ2000Date() const {
280  static const double kJulianDate2000 = 2451545.0;
281  double julian_date = GetJulianDate();
282  return julian_date - kJulianDate2000;
283  }
284 
286  bool IsUndefined() const { return year_ == kUndefinedYear; }
287 
289  void MakeUndefined() { year_ = kUndefinedYear; }
290 
291  void operator+=(int64 secs) {
292  uint32 nanoseconds = nanosecond_;
293  SetFromPosixSecondsOnly(GetPosixSecondsOnly() + secs, zone_hours_,
294  zone_minutes_);
295  nanosecond_ = nanoseconds;
296  }
297  void operator-=(int64 secs) { *this += -secs; }
298 
301  void operator+=(int secs) { *this += static_cast<int64>(secs); }
302  void operator+=(double secs);
303  void operator-=(int secs) { *this += -secs; }
304  void operator-=(double secs) { *this += -secs; }
305 
309  static DateTime Interpolate(const DateTime& begin,
310  const DateTime& end,
311  double t);
312 
318  static double GetDurationSecs(const DateTime& begin, const DateTime& end);
319 
323  static double GetInterpValue(const DateTime& now,
324  const DateTime& time_a,
325  const DateTime& time_b);
326 
330  static bool ParseYMString(const std::string& date, DateTime* date_out);
331 
334  int64 GetDateTimeField(DateTimeField field) const;
335 
337  int64 GetDateTimeField(uint8 field) const {
338  DCHECK(field < static_cast<uint8>(kNumFields));
339  return GetDateTimeField(static_cast<DateTimeField>(field));
340  }
341 
342  protected:
345  virtual bool Use24HourTime() const;
346 
347  private:
350  void SetFromPosixSecondsOnly(int64 secs, int8 requested_zone_hours,
351  int8 requested_zone_minutes);
352 
356  int64 GetPosixSecondsOnly() const;
357 
358  static const int64 kUndefinedYear = kint64max;
359 
361  int64 year_;
362  uint8 month_;
363  uint8 day_;
364  uint8 hour_; // Always a 24-hour representation internally.
365  uint8 minute_;
366  uint8 second_;
367  uint32 nanosecond_;
368  int8 zone_hours_;
369  int8 zone_minutes_;
370 
371  FRIEND_TEST(DateTime, Use24HourTime);
372  FRIEND_TEST(DateTime, NewestOldestDateTime);
373 };
374 
375 std::ostream& operator<<(std::ostream& os, const DateTime& dtime);
376 std::istream& operator>>(std::istream& in, DateTime& dtime);
377 
378 } // namespace base
379 } // namespace ion
380 
381 #endif // ION_BASE_DATETIME_H_
void operator-=(int64 secs)
Definition: datetime.h:297
const std::string & str
void SetEnd(const DateTime &end)
Definition: datetime.h:79
static double GetDurationSecs(const DateTime &begin, const DateTime &end)
Compute the duration from the first datetime to the second datetime in seconds, down to nanosecond re...
Definition: datetime.cc:830
void SetSecond(uint8 second)
Definition: datetime.cc:241
uint8 GetHour() const
Definition: datetime.h:144
int64 GetDateTimeField(DateTimeField field) const
Returns a specific field value in the DateTime object as defined by DateTimeField field (kYear...
Definition: datetime.cc:920
void Normalize()
Converts this DateTime to UTC time (+0:00 time zone).
Definition: datetime.h:188
bool operator==(const DateTime &dtime) const
This operator converts the date times to absolute and compares the absolute times for equality (hence...
Definition: datetime.cc:734
static Range Interpolate(const Range &begin, const Range &end, float t)
Definition: datetime.h:88
#define DCHECK(expr)
Definition: logging.h:331
static double GetInterpValue(const DateTime &now, const DateTime &time_a, const DateTime &time_b)
Returns a double value representing the interpolation of now with respect to time_a and time_b...
Definition: datetime.cc:848
double value
std::string ToString() const
Converts time to user-readable string.
Definition: datetime.cc:310
void SetNanosecond(uint32 nanosecond)
Definition: datetime.cc:248
int64 GetDateTimeField(uint8 field) const
A convenience function to iterate through DateTime fields.
Definition: datetime.h:337
std::istream & operator>>(std::istream &in, DateTime &dtime)
Definition: datetime.cc:516
bool operator>(const DateTime &dtime) const
Definition: datetime.cc:697
A class to contain a beginning and ending DateTime.
Definition: datetime.h:63
void SetDay(uint8 day)
Definition: datetime.cc:217
bool operator!=(const DateTime &dtime) const
Definition: datetime.h:201
bool operator>=(const DateTime &dtime) const
Definition: datetime.h:199
std::ostream & operator<<(std::ostream &os, const DateTime &dtime)
Definition: datetime.cc:361
std::string ComputeDurationString(double fractional_seconds) const
Returns string for 'this', e.g., "2y3m18d", interpreted as a duration.
Definition: datetime.cc:654
uint8 GetMinute() const
Definition: datetime.h:145
double GetJ2000Date() const
Returns a day decimal value relative to J2000, the epoch relative to 2000 Jan 1.5 (12h on January 1) ...
Definition: datetime.h:279
bool operator<=(const DateTime &dtime) const
Definition: datetime.h:200
const DateTime & begin() const
Definition: datetime.h:75
int8 GetZoneHours() const
Definition: datetime.h:148
static bool ParseYMString(const std::string &date, DateTime *date_out)
Checks if given string is in YYYY-MM formatr.
Definition: datetime.cc:896
int64 GetYear() const
Accessors for all fields.
Definition: datetime.h:141
void SetZoneMinutes(int8 zone_minutes)
Definition: datetime.cc:264
DateTimeField
Enumeration of the time-value fields of DateTime (used for iteration and numerical access)...
Definition: datetime.h:51
void SetBegin(const DateTime &begin)
Definition: datetime.h:78
double GetTimeAsDecimal() const
Convert "Standard" time to decimal time (or "French Revolutionary" time).
Definition: datetime.cc:792
void SetYear(int64 year)
Mutators for individual fields.
Definition: datetime.cc:203
bool IsEqualByComponent(const DateTime &dtime) const
Checks each component of the time object, including time zone.
Definition: datetime.cc:750
void MakeUndefined()
Changes this DateTime to mark it as undefined.
Definition: datetime.h:289
void ComputeDateString(const DateStringEnum output_date_format, std::string *out_string) const
Definition: datetime.cc:529
DateTime represents a particular date and time down to the nanosecond level along with timezone infor...
Definition: datetime.h:45
Rep GetPosixSeconds() const
Return the number of seconds offset from the POSIX epoch, as a given type Rep.
Definition: datetime.h:210
virtual ~DateTime()
Definition: datetime.h:138
void operator-=(double secs)
Definition: datetime.h:304
double GetJulianDate() const
The Julian Day is the integer number of days that have elapsed since noon on Monday, January 1, 4713 BC.
Definition: datetime.cc:768
void SetZoneHours(int8 zone_hours)
Definition: datetime.cc:256
uint8 GetMonth() const
Definition: datetime.h:142
uint8 GetDay() const
Definition: datetime.h:143
void SetMonth(uint8 month)
Definition: datetime.cc:209
static DateTime CreateFromPosixSeconds(Rep secs, int8 zone_hours=0, int8 zone_minutes=0)
Create a DateTime as an offset (in seconds) from the POSIX epoch, i.e., 00:00:00 01 January 1970...
Definition: datetime.h:118
void SetMinute(uint8 minute)
Definition: datetime.cc:234
virtual bool Use24HourTime() const
Determines whether to render 24-hour time strings based on a value set by translators.
Definition: datetime.cc:647
void Lerp(const DateTime &origin, const DateTime &target, double t)
Set the current time to the interpolation of the two given times according to the interpolant...
Definition: datetime.cc:916
void operator-=(int secs)
Definition: datetime.h:303
void operator+=(int secs)
Regular int operators provided for compiler convenience so that you don't have to static cast...
Definition: datetime.h:301
void AdjustTimeZone(int newHours, int newMins)
Converts time to another time zone.
Definition: datetime.cc:801
bool operator<(const DateTime &dtime) const
Definition: datetime.h:198
void ComputeTimeString(const TimeStringEnum output_time_format, std::string *out_string) const
Definition: datetime.cc:598
uint32 GetNanosecond() const
Definition: datetime.h:147
Range(const Range &rhs)
Definition: datetime.h:69
uint8 GetSecond() const
Definition: datetime.h:146
void Reset()
Set the DateTime to default values (<year>/1/1T00:00:00.0Z00:00, where <year> is set as kUndefinedYea...
Definition: datetime.cc:169
DateStringEnum
ComputeDateString() and ComputeTimeString() render the DateTime object to a std::string.
Definition: datetime.h:238
DateTime & operator=(const DateTime &rhs)
Definition: datetime.cc:812
bool IsUndefined() const
Checks if the date is unset or marked undefined.
Definition: datetime.h:286
void SetInterpolation(const Range &begin, const Range &end, float t)
Definition: datetime.h:81
static DateTime Interpolate(const DateTime &begin, const DateTime &end, double t)
Returns a linearly-interpolated DateTime between begin and end as defined by parameter t...
Definition: datetime.cc:819
const DateTime & end() const
Definition: datetime.h:76
void operator+=(int64 secs)
Definition: datetime.h:291
void Set(int64 years, uint8 months, uint8 days, uint8 hours, uint8 minutes, uint8 seconds, int8 zone_hours, int8 zone_minutes)
Definition: datetime.cc:273
bool FromString(const std::string &str)
Parses str into this DateTime object.
Definition: datetime.cc:366
void SetHour(uint8 hour)
Definition: datetime.cc:226
int8 GetZoneMinutes() const
Definition: datetime.h:149
std::chrono::system_clock::time_point GetTimePoint() const
Return a std::chrono::system_clock::time_point.
Definition: datetime.cc:758