3 #ifndef INCLUDE_NN_NLIB_DATETIME_H_
4 #define INCLUDE_NN_NLIB_DATETIME_H_
8 #ifdef NLIB_CXX11_STDLIB_CHRONO
18 NLIB_EINVAL_IFNULL(t);
23 *t = time / 10000 - (10957LL * 24LL * 60LL * 60LL * 1000LL);
31 return static_cast<uint64_t
>(t / 10000);
50 if (tick < 0) tick = -tick;
60 return lhs.tick == rhs.tick;
63 return lhs.tick < rhs.tick;
91 NLIB_CEXPR const int64_t tick_1d = (10LL * 1000 * 1000 * 60 * 60 * 24);
98 : m_Value(static_cast<int64_t>(days) * tick_1d) {}
100 : m_Value(days* tick_1d + seconds*(10LL * 1000 * 1000)) {}
104 : m_Value(days* tick_1d + seconds*(10LL * 1000 * 1000) + milliseconds*(10LL * 1000) +
105 microseconds * 10LL) {}
112 return static_cast<int>(m_Value.tick / (10LL * 1000 * 1000 * 60 * 60 * 24));
115 return static_cast<int>(m_Value.tick / (10LL * 1000 * 1000 * 60 * 60));
118 return m_Value.tick / (10 * 1000 * 1000 * 60);
121 return m_Value.tick / (10 * 1000 * 1000);
124 return m_Value.tick / (10 * 1000);
127 return m_Value.tick / (10);
129 #ifdef NLIB_HAS_STDHEADER_TIME
130 template <
class TIMEVAL>
131 void ToTimeVal(TIMEVAL* tv)
const NLIB_NOEXCEPT {
132 int64_t usec = ToMicroSeconds();
133 int64_t sec = usec / (1000 * 1000);
134 tv->tv_sec =
static_cast<time_t
>(sec);
135 tv->tv_usec =
static_cast<int32_t
>(usec - sec * (1000 * 1000));
137 template <
class TIMESPEC>
138 void ToTimeSpec(TIMESPEC* tp)
const NLIB_NOEXCEPT {
139 int64_t usec = ToMicroSeconds();
140 int64_t sec = usec / (1000 * 1000);
141 tp->tv_sec =
static_cast<time_t
>(sec);
142 tp->tv_nsec =
static_cast<long>(usec - sec * (1000 * 1000)) * 1000;
145 #ifdef NLIB_CXX11_STDLIB_CHRONO
146 template <
class Rep,
class Period>
147 TimeSpan(
const std::chrono::duration<Rep, Period>& rhs) {
148 auto tmp = std::chrono::duration_cast<std::chrono::microseconds>(rhs);
153 auto tmp = std::chrono::microseconds(m_Value.tick / 10);
154 return std::chrono::duration_cast<T>(tmp);
160 m_Value += rhs.m_Value;
164 m_Value -= rhs.m_Value;
172 m_Value.tick =
static_cast<int64_t
>(m_Value.tick * d);
175 void Abs() NLIB_NOEXCEPT { m_Value.Abs(); }
179 return m_Value == rhs.m_Value;
182 return m_Value != rhs.m_Value;
185 return m_Value < rhs.m_Value;
188 return m_Value > rhs.m_Value;
191 return m_Value <= rhs.m_Value;
194 return m_Value >= rhs.m_Value;
267 int min = 0,
int sec = 0,
int msec = 0,
268 int usec = 0) NLIB_NOEXCEPT;
273 return IsValid() ? 0 : EINVAL;
324 bool operator==(const DateTime& rhs) const NLIB_NOEXCEPT {
325 return m_Value == rhs.m_Value;
328 return m_Value != rhs.m_Value;
331 return m_Value < rhs.m_Value;
334 return m_Value > rhs.m_Value;
337 return m_Value <= rhs.m_Value;
340 return m_Value >= rhs.m_Value;
343 #ifdef NLIB_CXX11_STDLIB_CHRONO
344 template <
class Clock,
class Duration>
345 errno_t Init(
const std::chrono::time_point<Clock, Duration>& rhs) {
346 auto tmp = std::chrono::duration_cast<std::chrono::microseconds>(rhs.time_since_epoch());
352 auto tmp = std::chrono::microseconds(m_Value.tick / 10);
359 return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
373 return this->Init(rhs.year, rhs.month, rhs.day, rhs.hour, rhs.min, rhs.sec, rhs.msec, rhs.usec);
377 m_Value += rhs.ToTimeValue();
378 return IsValid() ? 0 : ERANGE;
381 inline errno_t DateTime::AddDays(
double value) NLIB_NOEXCEPT {
387 inline errno_t DateTime::AddHours(
double value) NLIB_NOEXCEPT {
393 inline errno_t DateTime::AddMinutes(
double value) NLIB_NOEXCEPT {
399 inline errno_t DateTime::AddSeconds(
double value) NLIB_NOEXCEPT {
405 inline errno_t DateTime::AddMilliSeconds(
double value) NLIB_NOEXCEPT {
411 inline errno_t DateTime::AddMicroSeconds(
double value) NLIB_NOEXCEPT {
440 return TimeSpan(lhs.ToTimeValue() - rhs.ToTimeValue());
445 #endif // INCLUDE_NN_NLIB_DATETIME_H_
bool operator<(const TimeValue &lhs, const TimeValue &rhs) noexcept
A relational operator.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
int64_t tick
These can be used for nlib_time and nlib_duration.
TimeSpan & operator*=(int i) noexcept
Multiplies the time by i.
int min
The minute (0-59).
TimeSpan & operator-=(const TimeSpan &rhs) noexcept
Subtracts time.
bool operator==(const TimeSpan &rhs) const noexcept
Returns true if the time is the same.
static bool IsLeapYear(int year) noexcept
Checks whether the specified year is a leap year.
int year
The year (1-9999).
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
errno_t ToW3cDtf(char(&str)[32]) const noexcept
Calls ToW3cDtf(str, TimeSpan()).
bool operator<(const TimeSpan &rhs) const noexcept
Returns true if smaller than rhs.
TimeSpan operator-() const noexcept
A unary operator.
int64_t ToMinutes() const noexcept
Returns the time as a number of minutes. Fractions are truncated.
TimeSpan & operator+=(const TimeSpan &rhs) noexcept
Adds time.
errno_t ToRfc2822(char(&str)[32]) const noexcept
Calls ToRfc2822(str, TimeSpan()).
bool operator<=(const TimeSpan &rhs) const noexcept
Returns true if equal to or smaller than rhs.
TimeSpan operator*(const TimeSpan &lhs, double d) noexcept
Increases rhs by a factor of d.
int msec
The millisecond (0-999).
bool operator<=(const TimeValue &lhs, const TimeValue &rhs) noexcept
A relational operator.
int64_t ToSeconds() const noexcept
Returns the time in terms of a number of seconds. Fractions are truncated.
bool operator==(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are equal.
bool operator!=(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are not equal.
bool operator<=(const DateTime &rhs) const noexcept
Returns true if the same or further in the past than rhs.
DateTime operator+(const DateTime &lhs, const TimeSpan &rhs) noexcept
Returns the time that is the rhs amount of time into the future from lhs.
The class for representing the date and time.
uint64_t GetTickTime() noexcept
Returns the time in units of milliseconds that has elapsed since the system started.
int sec
The second (0-59).
constexpr TimeSpan() noexcept
Instantiates the object and initializes the time to 0.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Week
Constants representing the days of the week.
The structure for setting date and time information in DateTime and for getting that information from...
TimeSpan operator-(const DateTime &lhs, const DateTime &rhs) noexcept
Returns the duration between the times of rhs and lhs.
errno_t GetNintendoTime(int64_t *t) noexcept
Returns the amount of time that has elapsed, in units of milliseconds, starting from 2000-01-01...
bool operator>=(const TimeValue &lhs, const TimeValue &rhs) noexcept
A relational operator.
bool operator>=(const DateTime &rhs) const noexcept
Returns true if the same or further in the future than rhs.
A file that contains the configuration information for each development environment.
int usec
The microsecond (0-999).
int ToDays() const noexcept
Returns the time in terms of a number of days. Fractions are truncated.
TimeValue operator-() const noexcept
A unary operator.
TimeValue ToTimeValue() const noexcept
Returns a TimeValue object.
bool operator>(const TimeValue &lhs, const TimeValue &rhs) noexcept
A relational operator.
constexpr TimeSpan(int days) noexcept
Initializes the object with a number of days specified for the period of time.
TimeSpan operator+() const noexcept
A unary operator.
TimeValue ToTimeValue() const noexcept
Converts to a TimeValue object.
int month
The month (1-12).
int64_t ToMicroSeconds() const noexcept
Returns the time in terms of a number of microseconds. Fractions are truncated.
bool operator>=(const TimeSpan &rhs) const noexcept
Returns true if equal to or larger than rhs.
int64_t ToMilliSeconds() const noexcept
Returns the time in terms of a number of milliseconds. Fractions are truncated.
TimeSpan(const TimeValue &rhs) noexcept
Initializes using a TimeValue object.
The class for representing the time.
constexpr DateTime() noexcept
Instantiates the object with default parameters (default constructor). Initializes with an invalid ti...
bool operator<(const DateTime &rhs) const noexcept
Returns true if further in the past than rhs.
constexpr TimeSpan(int days, int seconds) noexcept
Initializes the object with a number of days and a number of seconds specified for the period of time...
constexpr TimeValue() noexcept
Instantiates the object with default parameters (default constructor).
bool operator>(const TimeSpan &rhs) const noexcept
Returns true if larger than rhs.
constexpr TimeValue(int64_t tick_) noexcept
Initializes with the values of nlib_time and nlib_duration taken as the arguments.
bool operator!=(const TimeSpan &rhs) const noexcept
Returns true if the time is different.
TimeSpan & operator*=(double d) noexcept
Multiplies the time by d.
int ToHours() const noexcept
Returns the time span as a number of hours. Fractions are truncated.
constexpr TimeSpan(int days, int seconds, int milliseconds, int microseconds=0) noexcept
Initializes the object with a number of days, seconds, milliseconds, and microseconds specified for t...
The class that wraps 64-bit signed integers.
TimeValue & operator-=(const TimeValue &rhs) noexcept
Subtracts.
TimeValue & operator+=(const TimeValue &rhs) noexcept
Adds.
TimeValue operator+() const noexcept
A unary operator.
bool operator>(const DateTime &rhs) const noexcept
Returns true if further in the future than rhs.
bool operator!=(const DateTime &rhs) const noexcept
Returns true if the time is not equal to rhs.