30#ifndef NUMCPP_NO_USE_BOOST
41#include "boost/date_time/posix_time/posix_time.hpp"
73 auto tpSubSeconds = std::chrono::duration_cast<Duration>(tp.time_since_epoch());
74 auto fractionalSecond =
static_cast<double>(tpSubSeconds.count() % Duration::period::den) /
75 static_cast<double>(Duration::period::den);
76 auto time = Clock::to_time_t(std::chrono::time_point_cast<Clock::duration>(tp));
84 setYear(tm.tm_year + TM_EPOCH_YEAR);
99 explicit DateTime(
const std::string& timestamp) :
134 [[nodiscard]]
int year() const noexcept
149 throw std::invalid_argument(
"input year must be greater than zero");
160 [[nodiscard]]
int month() const noexcept
175 throw std::invalid_argument(
"input month must be greater than one");
179 throw std::invalid_argument(
"input month must be less than DateTime::MAX_MONTH");
190 [[nodiscard]]
int day() const noexcept
205 throw std::invalid_argument(
"input day must be greater than one");
209 throw std::invalid_argument(
"input day must be less than DateTime::MAX_DAY");
220 [[nodiscard]]
int hour() const noexcept
235 throw std::invalid_argument(
"input hour must be greater than zero");
239 throw std::invalid_argument(
"input hour must be less than DateTime::MAX_HOUR");
250 [[nodiscard]]
int minute() const noexcept
265 throw std::invalid_argument(
"input minute must be greater than zero");
269 throw std::invalid_argument(
"input minute must be less than DateTime::MAX_MINUTE");
280 [[nodiscard]]
int second() const noexcept
295 throw std::invalid_argument(
"input second must be greater than zero");
299 throw std::invalid_argument(
"input second must be less than DateTime::MAX_SECOND");
312 return fractionalSecond_;
323 if (fractionalSecond < 0. || fractionalSecond >= 1.)
325 throw std::invalid_argument(
"input fractionalSecond must be in the range [0, 1)");
339 t.tm_year = year_ - TM_EPOCH_YEAR;
340 t.tm_mon = month_ - 1;
345 auto timePoint = Clock::from_time_t(
352 return std::chrono::time_point_cast<TimePoint::duration>(timePoint) +
353 std::chrono::nanoseconds(
static_cast<int64_t
>(fractionalSecond_ * SECONDS_TO_NANOSECONDS));
362 [[nodiscard]] std::string
toStr()
const
365 const auto timeSinceEpoch = timePoint.time_since_epoch().count();
366 time_t secondsFromEpoch = timeSinceEpoch / Duration::period::den;
367 const auto fractionalSeconds =
static_cast<double>(timeSinceEpoch % Duration::period::den) /
368 static_cast<double>(Duration::period::den);
372 gmtime_s(&tm, &secondsFromEpoch);
374 gmtime_r(&secondsFromEpoch, &tm);
377 std::stringstream ss;
378 if (fractionalSeconds > 0)
380 const auto format =
"%Y-%m-%dT%H:%M:%S.%msZ";
381 std::stringstream ssFractionalSecond;
382 ssFractionalSecond.precision(NANO_SECOND_PRECESION);
383 ssFractionalSecond << std::fixed << fractionalSeconds;
384 auto fractionalSecondStr = ssFractionalSecond.str();
386 fractionalSecondStr = fractionalSecondStr.substr(2, fractionalSecondStr.size());
387 fractionalSecondStr = fractionalSecondStr.substr(0, fractionalSecondStr.find_last_not_of(
'0') + 1);
388 const auto fractionalSecondsFormat = std::regex_replace(format, std::regex(
"%ms"), fractionalSecondStr);
389 ss << std::put_time(&tm, fractionalSecondsFormat.c_str());
393 const auto format =
"%Y-%m-%dT%H:%M:%SZ";
394 ss << std::put_time(&tm, format);
420 const std::regex regexIsoTime{ R
"(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(.\d+)?Z)" };
421 if (!std::regex_match(timestamp, regexIsoTime))
423 throw std::invalid_argument(
"Invalid iso timestamp format");
426 auto convertedTime = boost::posix_time::ptime{};
429 convertedTime = boost::posix_time::from_iso_extended_string(timestamp.substr(0, timestamp.size() - 1));
433 throw std::invalid_argument(
"Invalid iso timestamp format");
436 const auto fromEpoch = convertedTime - POSIX_EPOCH;
441 static constexpr int TM_EPOCH_YEAR = 1900;
442 static constexpr int POSIX_EPOCH_YEAR = 1970;
443 static inline const std::string POSIX_EPOCH_STR{
"1970-01-01T00:00:00" };
444 static inline const boost::posix_time::ptime POSIX_EPOCH{ boost::posix_time::from_iso_extended_string(
446 static constexpr double SECONDS_TO_NANOSECONDS = 1e9;
447 static constexpr int NANO_SECOND_PRECESION = 9;
450 int year_{ POSIX_EPOCH_YEAR };
462 double fractionalSecond_{ 0.0 };
475 return lhs.toTimePoint() == rhs.toTimePoint();
488 return !(lhs == rhs);
501 return lhs.toTimePoint() < rhs.toTimePoint();
514 return lhs.toTimePoint() <= rhs.toTimePoint();
527 return lhs.toTimePoint() > rhs.toTimePoint();
540 return lhs.toTimePoint() >= rhs.toTimePoint();
553 return lhs.toTimePoint() - rhs.toTimePoint();
567 os <<
"\tyear: " << datetime.year() <<
'\n';
568 os <<
"\tmonth: " << datetime.month() <<
'\n';
569 os <<
"\tday: " << datetime.day() <<
'\n';
570 os <<
"\thour: " << datetime.hour() <<
'\n';
571 os <<
"\tminute: " << datetime.minute() <<
'\n';
572 os <<
"\tsecond: " << datetime.second() <<
'\n';
573 os <<
"\tfractionalSecond: " << datetime.fractionalSecond() <<
'\n';
Date Time class for working with iso formatted date times.
Definition: DateTime/DateTime.hpp:51
void setFractionalSecond(double fractionalSecond)
fractionalSecond setter
Definition: DateTime/DateTime.hpp:321
void setHour(int hour)
hour setter
Definition: DateTime/DateTime.hpp:231
static constexpr int MAX_HOUR
Definition: DateTime/DateTime.hpp:55
int hour() const noexcept
hour getter
Definition: DateTime/DateTime.hpp:220
int second() const noexcept
second getter
Definition: DateTime/DateTime.hpp:280
TimePoint toTimePoint() const
Converts the struct to a TimePoint.
Definition: DateTime/DateTime.hpp:336
static constexpr int MAX_MINUTE
Definition: DateTime/DateTime.hpp:56
DateTime(int year, int month, int day, int hour, int minute, int second, double fractionalSecond=0.0) noexcept
Definition: DateTime/DateTime.hpp:116
int year() const noexcept
year getter
Definition: DateTime/DateTime.hpp:134
void setSecond(int second)
second setter
Definition: DateTime/DateTime.hpp:291
static constexpr int MAX_DAY
Definition: DateTime/DateTime.hpp:54
int minute() const noexcept
minute getter
Definition: DateTime/DateTime.hpp:250
DateTime(const std::string ×tamp)
Definition: DateTime/DateTime.hpp:99
void setDay(int day)
day setter
Definition: DateTime/DateTime.hpp:201
double fractionalSecond() const noexcept
fractionalSecond getter
Definition: DateTime/DateTime.hpp:310
void setMonth(int month)
month setter
Definition: DateTime/DateTime.hpp:171
static TimePoint strToTimepoint(const std::string ×tamp)
Converts the struct to an iso string.
Definition: DateTime/DateTime.hpp:418
std::string toStr() const
Converts the struct to an iso string.
Definition: DateTime/DateTime.hpp:362
static constexpr int MAX_SECOND
Definition: DateTime/DateTime.hpp:57
static constexpr int MAX_MONTH
Definition: DateTime/DateTime.hpp:53
void setMinute(int minute)
minute setter
Definition: DateTime/DateTime.hpp:261
int month() const noexcept
month getter
Definition: DateTime/DateTime.hpp:160
int day() const noexcept
day getter
Definition: DateTime/DateTime.hpp:190
static DateTime now() noexcept
Factory static method for returning a DateTime object cooresponding to the system clock now.
Definition: DateTime/DateTime.hpp:407
DateTime(const TimePoint &tp)
Definition: DateTime/DateTime.hpp:71
void setYear(int year)
year setter
Definition: DateTime/DateTime.hpp:145
Definition: Cartesian.hpp:40
bool operator==(const DateTime &lhs, const DateTime &rhs) noexcept
Equality operator for DateTime.
Definition: DateTime/DateTime.hpp:473
bool operator>=(const std::complex< T > &lhs, const std::complex< T > &rhs) noexcept
Definition: StdComplexOperators.hpp:98
Duration operator-(const DateTime &lhs, const DateTime &rhs) noexcept
Subtraction operator.
Definition: DateTime/DateTime.hpp:551
bool operator>(const std::complex< T > &lhs, const std::complex< T > &rhs) noexcept
Definition: StdComplexOperators.hpp:84
std::chrono::nanoseconds Duration
Duration Type.
Definition: Clock.hpp:16
bool operator!=(const DateTime &lhs, const DateTime &rhs) noexcept
Non Equality operator for DateTime.
Definition: DateTime/DateTime.hpp:486
bool operator<(const std::complex< T > &lhs, const std::complex< T > &rhs) noexcept
Definition: StdComplexOperators.hpp:46
std::ostream & operator<<(std::ostream &os, Duration duration)
Output stream operator for the Duration type.
Definition: Clock.hpp:30
bool operator<=(const std::complex< T > &lhs, const std::complex< T > &rhs) noexcept
Definition: StdComplexOperators.hpp:65
std::chrono::time_point< Clock, Duration > TimePoint
TimePoint Type.
Definition: Clock.hpp:21