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;
79 NLIB_EQUAL_OPERATOR(TimeValue)
80 NLIB_COMPARE_OPERATOR(TimeValue)
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)) {}
107 : value_(days* tick_1d + seconds * (10LL * 1000 * 1000) + milliseconds * (10LL * 1000) +
108 microseconds * 10LL) {}
110 void Get(
int* days,
int* seconds,
int* milliseconds,
int* microseconds)
NLIB_NOEXCEPT;
114 return static_cast<int>(value_.tick / (10LL * 1000 * 1000 * 60 * 60 * 24));
117 return static_cast<int>(value_.tick / (10LL * 1000 * 1000 * 60 * 60));
123 template<
class TIMEVAL>
125 int64_t usec = ToMicroSeconds();
126 int64_t sec = usec / (1000 * 1000);
127 tv->tv_sec =
static_cast<time_t
>(sec);
128 tv->tv_usec =
static_cast<int32_t
>(usec - sec * (1000 * 1000));
131 NLIB_FROM_TIMESPEC(tm, value_.tick);
134 #ifdef NLIB_CXX11_STDLIB_CHRONO 135 template<
class Rep,
class Period>
136 TimeSpan(
const std::chrono::duration<Rep, Period>& rhs) {
137 auto tmp = std::chrono::duration_cast<std::chrono::microseconds>(rhs);
142 auto tmp = std::chrono::microseconds(value_.tick / 10);
143 return std::chrono::duration_cast<T>(tmp);
149 value_ += rhs.value_;
153 value_ -= rhs.value_;
161 value_.tick =
static_cast<int64_t
>(value_.tick * d);
175 return lhs.value_ == rhs.value_;
177 NLIB_EQUAL_OPERATOR(TimeSpan)
179 return lhs.value_ < rhs.value_;
181 NLIB_COMPARE_OPERATOR(TimeSpan)
248 errno_t Init(
int year,
int month,
int day,
int hour = 0,
int min = 0,
int sec = 0,
int msec = 0,
254 return IsValid() ? 0 : EINVAL;
305 NLIB_FROM_TIMESPEC(tm, value_.tick);
308 #ifdef NLIB_CXX11_STDLIB_CHRONO 309 template<
class Clock,
class Duration>
310 errno_t Init(
const std::chrono::time_point<Clock, Duration>& rhs) {
311 auto tmp = std::chrono::duration_cast<std::chrono::microseconds>(rhs.time_since_epoch());
317 auto tmp = std::chrono::microseconds(value_.tick / 10);
324 return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
328 static int GetDaysInMonth(
int year,
int month)
NLIB_NOEXCEPT;
330 static std::pair<const char*, errno_t>
331 Parse(
const char* first,
const char* last,
DateTime* dt,
335 std::pair<const char*, errno_t> r = Parse(str, last, dt, delta);
336 return (r.second == 0) ? ((r.first == last) ? 0 : EILSEQ) : r.second;
350 return lhs.value_ == rhs.value_;
352 NLIB_EQUAL_OPERATOR(DateTime)
354 return lhs.value_ < rhs.value_;
356 NLIB_COMPARE_OPERATOR(DateTime)
359 return this->Init(rhs.year, rhs.month, rhs.day, rhs.hour, rhs.min, rhs.sec, rhs.msec, rhs.usec);
363 value_ += rhs.ToTimeValue();
364 return IsValid() ? 0 : ERANGE;
426 return TimeSpan(lhs.ToTimeValue() - rhs.ToTimeValue());
431 #endif // INCLUDE_NN_NLIB_DATETIME_H_ int64_t tick
nlib_time, nlib_durationとして利用することができます。
void Abs() noexcept
時間が負である場合には符号を反転します。
TimeSpan & operator*=(int i) noexcept
時間をi で乗算します。
TimeValue ToTimeValue() const noexcept
TimeValueオブジェクトに変換します。
void Abs() noexcept
時間が負である場合には符号を反転します。
int64_t ToMilliSeconds() const noexcept
時間をミリ秒数にして返します。端数は切り捨てられます。
TimeSpan & operator-=(const TimeSpan &rhs) noexcept
時間を減算します。
static constexpr bool IsLeapYear(int year) noexcept
閏年かどうかをチェックします。
errno_t ToW3cDtf(char(&str)[32]) const noexcept
ToW3cDtf(str, TimeSpan()) を呼び出します。
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
int64_t ToSeconds() const noexcept
時間を秒数にして返します。端数は切り捨てられます。
TimeSpan & operator+=(const TimeSpan &rhs) noexcept
時間を加算します。
void ToTimeVal(TIMEVAL *tv) const noexcept
データを秒数とマイクロ秒数に分割して格納します。
TimeSpan operator*(const TimeSpan &lhs, double d) noexcept
rhs を d 倍します。
int ToHours() const noexcept
時間を一時間単位の値として返します。端数は切り捨てられます。
bool operator==(const HeapHash &rhs, const HeapHash &lhs)
2つのサマリを比較して等価ならば、trueを返します。
errno_t ToRfc2822(char(&str)[32]) const noexcept
ToRfc2822(str, TimeSpan()) を呼び出します。
DateTime operator+(const DateTime &lhs, const TimeSpan &rhs) noexcept
lhs から rhs だけ未来の時刻を返します。
static bool IsRfc3339(const nlib_utf8_t *str) noexcept
上記関数の引数省略版で、ヌル終端する文字列を受け取ります。
void ToTimeSpec(struct timespec *tm) const noexcept
timespec構造体の値に変換します。
uint64_t GetTickTime() noexcept
システムを起動した後の経過時間をミリ秒単位で返します。
TimeSpan(const struct timespec *tm) noexcept
timespec構造体からオブジェクトを構築します。
TimeValue operator+() const noexcept
単項演算子です。
TimeValue operator-() const noexcept
単項演算子です。
constexpr TimeSpan() noexcept
デフォルトコンストラクタです。 時間を0で初期化します。
int ToDays() const noexcept
時間を日数にして返します。端数は切り捨てられます。
void ToTimeSpec(struct timespec *tm) const noexcept
timespec構造体の値に変換します。
日時情報をDateTimeに設定したりDateTimeから取得するための構造体です。
TimeSpan operator-(const DateTime &lhs, const DateTime &rhs) noexcept
時刻rhs から時刻lhs までの時間を返します。
errno_t GetNintendoTime(int64_t *t) noexcept
2000/01/01を起点とした経過時間をミリ秒単位で返します。
TimeSpan operator+() const noexcept
単項演算子です。
bool operator<(const DateTime &lhs, const DateTime &rhs) noexcept
lhsがrhsより過去の時刻ならtrueを返します。
static errno_t Parse(const char *str, DateTime *dt, TimeSpan *delta)
ヌル終端する文字列をパースします。ヌル終端する前にパースされない文字に到達した場合へエラーになります...
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
TimeSpan operator-() const noexcept
単項演算子です。
constexpr TimeSpan(int days) noexcept
期間として日数を指定して初期化します。
TimeSpan(const TimeValue &rhs) noexcept
TimeValue オブジェクトを用いて初期化します。
TimeValue ToTimeValue() const noexcept
TimeValueオブジェクトを返します。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
constexpr DateTime() noexcept
デフォルトコンストラクタです。 無効な時刻で初期化します。
constexpr TimeSpan(int days, int seconds) noexcept
期間として日数と秒を指定して初期化します。
constexpr TimeValue() noexcept
デフォルトコンストラクタです。
int64_t ToMicroSeconds() const noexcept
時間をマイクロ秒数にして返します。端数は切り捨てられます。
constexpr TimeValue(int64_t tick_) noexcept
nlib_time, nlib_durationの値を引数にして初期化します。
TimeSpan & operator*=(double d) noexcept
時間をd で乗算します。
constexpr TimeSpan(int days, int seconds, int milliseconds, int microseconds=0) noexcept
期間として日数,秒,ミリ秒,マイクロ秒を指定して初期化します。
TimeValue & operator-=(const TimeValue &rhs) noexcept
減算します。
int64_t ToMinutes() const noexcept
時間を分数にして返します。端数は切り捨てられます。
TimeValue & operator+=(const TimeValue &rhs) noexcept
加算します。