18 #ifndef ION_BASE_DATETIME_H_
20 #define ION_BASE_DATETIME_H_
26 #include <type_traits>
28 #include "base/integral_types.h"
30 #include "ion/external/gtest/gunit_prod.h"
70 *begin_ = rhs.
begin();
97 std::unique_ptr<DateTime> begin_;
98 std::unique_ptr<DateTime> end_;
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);
110 explicit DateTime(std::chrono::system_clock::time_point time,
111 int8 zone_hours = 0, int8 zone_minutes = 0);
117 template <
typename Rep>
119 int8 zone_minutes = 0) {
123 date.SetFromPosixSecondsOnly(static_cast<int64>(secs), zone_hours,
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,
133 date.nanosecond_ =
static_cast<uint32
>(nanoseconds);
166 void Set(int64 years,
175 void Set(int64 years,
205 std::chrono::system_clock::time_point
GetTimePoint()
const;
209 template <
typename Rep =
double>
211 const int64 posix_seconds = GetPosixSecondsOnly();
212 return static_cast<Rep
>(posix_seconds) +
213 static_cast<Rep>(1e-9) * nanosecond_;
249 std::string* out_string)
const;
251 std::string* out_string)
const;
280 static const double kJulianDate2000 = 2451545.0;
282 return julian_date - kJulianDate2000;
292 uint32 nanoseconds = nanosecond_;
293 SetFromPosixSecondsOnly(GetPosixSecondsOnly() + secs, zone_hours_,
295 nanosecond_ = nanoseconds;
301 void operator+=(
int secs) { *
this +=
static_cast<int64
>(secs); }
350 void SetFromPosixSecondsOnly(int64 secs, int8 requested_zone_hours,
351 int8 requested_zone_minutes);
356 int64 GetPosixSecondsOnly()
const;
358 static const int64 kUndefinedYear = kint64max;
372 FRIEND_TEST(
DateTime, NewestOldestDateTime);
375 std::ostream&
operator<<(std::ostream& os,
const DateTime& dtime);
376 std::istream&
operator>>(std::istream& in, DateTime& dtime);
381 #endif // ION_BASE_DATETIME_H_
void operator-=(int64 secs)
void SetEnd(const DateTime &end)
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...
void SetSecond(uint8 second)
int64 GetDateTimeField(DateTimeField field) const
Returns a specific field value in the DateTime object as defined by DateTimeField field (kYear...
void Normalize()
Converts this DateTime to UTC time (+0:00 time zone).
bool operator==(const DateTime &dtime) const
This operator converts the date times to absolute and compares the absolute times for equality (hence...
static Range Interpolate(const Range &begin, const Range &end, float t)
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...
std::string ToString() const
Converts time to user-readable string.
void SetNanosecond(uint32 nanosecond)
int64 GetDateTimeField(uint8 field) const
A convenience function to iterate through DateTime fields.
std::istream & operator>>(std::istream &in, DateTime &dtime)
bool operator>(const DateTime &dtime) const
A class to contain a beginning and ending DateTime.
bool operator!=(const DateTime &dtime) const
bool operator>=(const DateTime &dtime) const
std::ostream & operator<<(std::ostream &os, const DateTime &dtime)
std::string ComputeDurationString(double fractional_seconds) const
Returns string for 'this', e.g., "2y3m18d", interpreted as a duration.
double GetJ2000Date() const
Returns a day decimal value relative to J2000, the epoch relative to 2000 Jan 1.5 (12h on January 1) ...
bool operator<=(const DateTime &dtime) const
const DateTime & begin() const
int8 GetZoneHours() const
static bool ParseYMString(const std::string &date, DateTime *date_out)
Checks if given string is in YYYY-MM formatr.
int64 GetYear() const
Accessors for all fields.
void SetZoneMinutes(int8 zone_minutes)
DateTimeField
Enumeration of the time-value fields of DateTime (used for iteration and numerical access)...
void SetBegin(const DateTime &begin)
double GetTimeAsDecimal() const
Convert "Standard" time to decimal time (or "French Revolutionary" time).
void SetYear(int64 year)
Mutators for individual fields.
bool IsEqualByComponent(const DateTime &dtime) const
Checks each component of the time object, including time zone.
void MakeUndefined()
Changes this DateTime to mark it as undefined.
void ComputeDateString(const DateStringEnum output_date_format, std::string *out_string) const
DateTime represents a particular date and time down to the nanosecond level along with timezone infor...
Rep GetPosixSeconds() const
Return the number of seconds offset from the POSIX epoch, as a given type Rep.
void operator-=(double secs)
double GetJulianDate() const
The Julian Day is the integer number of days that have elapsed since noon on Monday, January 1, 4713 BC.
void SetZoneHours(int8 zone_hours)
void SetMonth(uint8 month)
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...
void SetMinute(uint8 minute)
virtual bool Use24HourTime() const
Determines whether to render 24-hour time strings based on a value set by translators.
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...
void operator-=(int secs)
void operator+=(int secs)
Regular int operators provided for compiler convenience so that you don't have to static cast...
void AdjustTimeZone(int newHours, int newMins)
Converts time to another time zone.
bool operator<(const DateTime &dtime) const
void ComputeTimeString(const TimeStringEnum output_time_format, std::string *out_string) const
uint32 GetNanosecond() const
void Reset()
Set the DateTime to default values (<year>/1/1T00:00:00.0Z00:00, where <year> is set as kUndefinedYea...
DateStringEnum
ComputeDateString() and ComputeTimeString() render the DateTime object to a std::string.
DateTime & operator=(const DateTime &rhs)
bool IsUndefined() const
Checks if the date is unset or marked undefined.
void SetInterpolation(const Range &begin, const Range &end, float t)
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...
const DateTime & end() const
void operator+=(int64 secs)
void Set(int64 years, uint8 months, uint8 days, uint8 hours, uint8 minutes, uint8 seconds, int8 zone_hours, int8 zone_minutes)
bool FromString(const std::string &str)
Parses str into this DateTime object.
int8 GetZoneMinutes() const
std::chrono::system_clock::time_point GetTimePoint() const
Return a std::chrono::system_clock::time_point.