16 #ifndef INCLUDE_NN_NLIB_DATETIME_H_ 17 #define INCLUDE_NN_NLIB_DATETIME_H_ 22 #ifdef NLIB_CXX11_STDLIB_CHRONO 32 NLIB_EINVAL_IFNULL(t);
37 *t = time / 10000 - (10957LL * 24LL * 60LL * 60LL * 1000LL);
45 return static_cast<uint64_t
>(t / 10000);
64 if (tick < 0) tick = -tick;
74 return lhs.tick == rhs.tick;
77 return lhs.tick < rhs.tick;
95 NLIB_CEXPR const int64_t tick_1d = (10LL * 1000 * 1000 * 60 * 60 * 24);
102 : value_(static_cast<int64_t>(days) * tick_1d) {}
104 : value_(days* tick_1d + seconds*(10LL * 1000 * 1000)) {}
108 : value_(days* tick_1d + seconds*(10LL * 1000 * 1000) + milliseconds*(10LL * 1000) +
109 microseconds * 10LL) {}
118 return static_cast<int>(value_.tick / (10LL * 1000 * 1000 * 60 * 60 * 24));
121 return static_cast<int>(value_.tick / (10LL * 1000 * 1000 * 60 * 60));
124 return value_.tick / (10 * 1000 * 1000 * 60);
127 return value_.tick / (10 * 1000 * 1000);
130 return value_.tick / (10 * 1000);
133 return value_.tick / (10);
135 template <
class TIMEVAL>
136 void ToTimeVal(TIMEVAL* tv)
const NLIB_NOEXCEPT {
137 int64_t usec = ToMicroSeconds();
138 int64_t sec = usec / (1000 * 1000);
139 tv->tv_sec =
static_cast<time_t
>(sec);
140 tv->tv_usec =
static_cast<int32_t
>(usec - sec * (1000 * 1000));
142 explicit TimeSpan(
const struct timespec* tm) NLIB_NOEXCEPT {
143 NLIB_FROM_TIMESPEC(tm, value_.tick);
146 NLIB_TO_TIMESPEC(tm, value_.tick);
148 #ifdef NLIB_CXX11_STDLIB_CHRONO 149 template <
class Rep,
class Period>
150 TimeSpan(
const std::chrono::duration<Rep, Period>& rhs) {
151 auto tmp = std::chrono::duration_cast<std::chrono::microseconds>(rhs);
156 auto tmp = std::chrono::microseconds(value_.tick / 10);
157 return std::chrono::duration_cast<T>(tmp);
163 value_ += rhs.value_;
167 value_ -= rhs.value_;
175 value_.tick =
static_cast<int64_t
>(value_.tick * d);
178 void Abs() NLIB_NOEXCEPT { value_.Abs(); }
189 return lhs.value_ == rhs.value_;
193 return lhs.value_ < rhs.value_;
258 WEEK_SUNDAY = kWeekSunday,
259 WEEK_MONDAY = kWeekMonday,
260 WEEK_TUESDAY = kWeekTuesday,
261 WEEK_WEDNESDAY = kWeekWednesday,
262 WEEK_THURSDAY = kWeekThursday,
263 WEEK_FRIDAY = kWeekFriday,
264 WEEK_SATURDAY = kWeekSaturday,
271 errno_t Init(
int year,
int month,
int day,
int hour = 0,
272 int min = 0,
int sec = 0,
int msec = 0,
278 return IsValid() ? 0 : EINVAL;
326 char (&str)[32])
const NLIB_NOEXCEPT {
335 char (&str)[32])
const NLIB_NOEXCEPT {
348 explicit DateTime(
const struct timespec* tm) NLIB_NOEXCEPT {
349 NLIB_FROM_TIMESPEC(tm, value_.tick);
352 NLIB_TO_TIMESPEC(tm, value_.tick);
354 #ifdef NLIB_CXX11_STDLIB_CHRONO 355 template <
class Clock,
class Duration>
356 errno_t Init(
const std::chrono::time_point<Clock, Duration>& rhs) {
357 auto tmp = std::chrono::duration_cast<std::chrono::microseconds>(rhs.time_since_epoch());
363 auto tmp = std::chrono::microseconds(value_.tick / 10);
370 return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
374 static int GetDaysInMonth(
int year,
int month)
NLIB_NOEXCEPT;
376 static std::pair<const char*, errno_t> Parse(
383 std::pair<const char*, errno_t> r = Parse(str, last, dt, delta);
384 return (r.second == 0) ? ((r.first == last) ? 0 : EILSEQ) : r.second;
398 return lhs.value_ == rhs.value_;
402 return lhs.value_ < rhs.value_;
407 return this->Init(rhs.year, rhs.month, rhs.day, rhs.hour, rhs.min, rhs.sec, rhs.msec, rhs.usec);
411 value_ += rhs.ToTimeValue();
412 return IsValid() ? 0 : ERANGE;
474 return TimeSpan(lhs.ToTimeValue() - rhs.ToTimeValue());
479 #endif // INCLUDE_NN_NLIB_DATETIME_H_ int64_t tick
These can be used for nlib_time and nlib_duration.
TimeSpan & operator*=(int i) noexcept
Multiplies the time by i.
TimeValue ToTimeValue() const noexcept
Converts to a TimeValue object.
int64_t ToMilliSeconds() const noexcept
Returns the time in terms of a number of milliseconds. Fractions are truncated.
int min
The minute (0-59).
TimeSpan & operator-=(const TimeSpan &rhs) noexcept
Subtracts time.
static constexpr bool IsLeapYear(int year) noexcept
Checks whether the specified year is a leap year.
int year
The year (1-9999).
errno_t ToW3cDtf(char(&str)[32]) const noexcept
Calls ToW3cDtf(str, TimeSpan()).
#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...
int64_t ToSeconds() const noexcept
Returns the time in terms of a number of seconds. Fractions are truncated.
TimeSpan & operator+=(const TimeSpan &rhs) noexcept
Adds time.
TimeSpan operator*(const TimeSpan &lhs, double d) noexcept
Increases rhs by a factor of d.
int ToHours() const noexcept
Returns the time span as a number of hours. Fractions are truncated.
int msec
The millisecond (0-999).
bool operator==(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are equal.
errno_t ToRfc2822(char(&str)[32]) const noexcept
Calls ToRfc2822(str, TimeSpan()).
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.
static bool IsRfc3339(const nlib_utf8_t *str) noexcept
Executes IsRfc3339(str, str + strlen(str)).
void ToTimeSpec(struct timespec *tm) const noexcept
Converts to a value of the timespec structure.
uint64_t GetTickTime() noexcept
Returns the time in units of milliseconds that has elapsed since the system started.
TimeSpan(const struct timespec *tm) noexcept
Instantiates the object from the timespec structure.
TimeValue operator+() const noexcept
A unary operator.
int sec
The second (0-59).
TimeValue operator-() const noexcept
A unary operator.
constexpr TimeSpan() noexcept
Instantiates the object and initializes the time to 0.
int ToDays() const noexcept
Returns the time in terms of a number of days. Fractions are truncated.
Week
Constants representing the days of the week.
void ToTimeSpec(struct timespec *tm) const noexcept
Converts to a value of the timespec structure.
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...
TimeSpan operator+() const noexcept
A unary operator.
bool operator<(const DateTime &lhs, const DateTime &rhs) noexcept
Returns true if lhs represent a time earlier than the time rhs does.
static errno_t Parse(const char *str, DateTime *dt, TimeSpan *delta)
Parses a string that is terminated with a null character. If a character that is not parsed is encoun...
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
A file that contains the configuration information for each development environment.
int usec
The microsecond (0-999).
TimeSpan operator-() const noexcept
A unary operator.
constexpr TimeSpan(int days) noexcept
Initializes the object with a number of days specified for the period of time.
int month
The month (1-12).
TimeSpan(const TimeValue &rhs) noexcept
Initializes using a TimeValue object.
TimeValue ToTimeValue() const noexcept
Returns a TimeValue object.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
The class for representing the time.
constexpr DateTime() noexcept
Instantiates the object with default parameters (default constructor). Initializes with an invalid ti...
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).
int64_t ToMicroSeconds() const noexcept
Returns the time in terms of a number of microseconds. Fractions are truncated.
constexpr TimeValue(int64_t tick_) noexcept
Initializes with the values of nlib_time and nlib_duration taken as the arguments.
TimeSpan & operator*=(double d) noexcept
Multiplies the time by d.
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.
DateTime(const struct timespec *tm) noexcept
Instantiates the object from the timespec structure.
int64_t ToMinutes() const noexcept
Returns the time as a number of minutes. Fractions are truncated.
TimeValue & operator+=(const TimeValue &rhs) noexcept
Adds.
errno_t Init(const TimeValue &tv) noexcept
Initializes the date and time using the TimeValue structure. Returns EINVAL if given an invalid date...