22 constexpr int64_t
MaxTicks = 3155378975999999999LL;
25 constexpr int DaysToMonth365 [] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
26 constexpr int DaysToMonth366 [] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
31 if (year < 1 || year > 9999 || month < 1 || month > 12) {
37 if (day < 1 || day > days [month] - days [month - 1]) {
43 int n = y * 365 + y / 4 - y / 100 + y / 400 + days [month - 1] + day - 1;
50 static int64_t
TimeToTicks(
int hour,
int minute,
int second) {
52 if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 || second < 0 || second >= 60) {
57 int64_t totalSeconds = (int64_t)hour * 3600 + (int64_t)minute * 60 + (int64_t)second;
62 static void GetDatePart(int64_t ticks,
int& year,
int& month,
int& day) {
67 int y400 = n / 146097; n -= y400 * 146097;
68 int y100 = n / 36524;
if (y100 == 4) y100 = 3; n -= y100 * 36524;
69 int y4 = n / 1461; n -= y4 * 1461;
70 int y1 = n / 365;
if (y1 == 4) y1 = 3; n -= y1 * 365;
73 year = y400 * 400 + y100 * 100 + y4 * 4 + y1 + 1;
78 while (n >= days [month]) month++;
79 day = n - days [month - 1] + 1;
91 DateTime::DateTime(
int year,
int month,
int day,
int hour,
int minute,
int second,
int millisecond)
94 if (millisecond < 0 || millisecond >= 1000) {
102 if (millisecond < 0 || millisecond >= 1000) {
154 return days [m - 1] + d;
204 int i = m - 1 + months;
205 if (i >= 0) { m = i % 12 + 1; y = y + i / 12; }
206 else { m = 12 + (i + 1) % 12; y = y + (i - 11) / 12; }
211 if (d > days) d = days;
224 int64_t ticks = m_nTicks + value;
225 if (ticks < MinTicks || ticks >
MaxTicks) {
234 if (value < -10000 || value > 10000) {
250 auto now = std::chrono::system_clock::now();
251 auto duration = now.time_since_epoch();
254 int64_t micro = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
264 static int64_t
TmToTicks(
const struct tm& t, int64_t remainderTicks) {
266 int year = t.tm_year + 1900, month = t.tm_mon + 1, day = t.tm_mday;
267 int hour = t.tm_hour, minute = t.tm_min, second = t.tm_sec;
277 _localtime64_s(&t, &time);
279 time_t time_t_val =
static_cast<time_t
>(time);
280 localtime_r(&time_t_val, &t);
289 _gmtime64_s(&t, &time);
291 time_t time_t_val =
static_cast<time_t
>(time);
292 gmtime_r(&time_t_val, &t);
319 return ToString(
"yyyy-MM-dd HH:mm:ss");
324 std::stringstream ss;
327 ss << std::setfill(
'0') << std::setw(4) << y <<
"-"
328 << std::setw(2) << m <<
"-" << std::setw(2) << d <<
" "
330 return String(ss.str().c_str());
335 if (month < 1 || month > 12) {
341 return days [month] - days [month - 1];
346 if (year < 1 || year > 9999) {
351 return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
358 while ((pos = f.find(
"yyyy")) != std::string::npos) f.replace(pos, 4,
"%Y");
359 while ((pos = f.find(
"MM")) != std::string::npos) f.replace(pos, 2,
"%m");
360 while ((pos = f.find(
"dd")) != std::string::npos) f.replace(pos, 2,
"%d");
361 while ((pos = f.find(
"HH")) != std::string::npos) f.replace(pos, 2,
"%H");
362 while ((pos = f.find(
"mm")) != std::string::npos) f.replace(pos, 2,
"%M");
363 while ((pos = f.find(
"ss")) != std::string::npos) f.replace(pos, 2,
"%S");
372 ss >> std::get_time(&t, f.c_str());
374 if (ss.fail())
return false;
377 result =
DateTime(t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
384 catch (
const std::exception&) {
404 throw FormatException(
"String was not recognized as a valid DateTime.");
414 throw FormatException(
"String was not recognized as a valid DateTime.");
Defines the exception thrown when an invalid argument is provided to a method.
Defines the exception thrown when an argument value is outside the acceptable range of values.
Represents an instant in time, typically expressed as a date and time of day.
High-performance UTF-8 / UTF-16 string manipulation class mirroring .NET System.String.
ArgumentOutOfRangeException(const String &sMessage)
Initializes a new instance of the ArgumentOutOfRangeException class with a specified error message.
Represents an instant in time, typically expressed as a date and time of day.
int GetSecond() const
Gets the seconds component of the date represented by this instance.
static bool IsLeapYear(int year)
Returns an indication whether the specified year is a leap year.
DateTime Add(TimeSpan value) const
Returns a new DateTime that adds the value of the specified TimeSpan.
static DateTime ParseExact(const String &s, const String &format)
Converts the string representation of a date and time to its DateTime equivalent using specified form...
DateTime AddSeconds(double value) const
Returns a new DateTime that adds the specified number of seconds.
DateTime AddMonths(int months) const
Returns a new DateTime that adds the specified number of months.
DateTime AddHours(double value) const
Returns a new DateTime that adds the specified number of hours.
static DateTime Now()
Gets a DateTime object set to the current date and time on this computer, expressed as local time.
DateTime ToUniversalTime() const
Converts the value of the current DateTime object to Coordinated Universal Time (UTC).
int GetMonth() const
Gets the month component of the date represented by this instance.
static bool TryParse(const String &s, DateTime &result)
Converts the string representation of a date and time to its DateTime equivalent and returns success ...
int GetDayOfWeek() const
Gets the day of the week represented by this instance.
DateTime AddMilliseconds(double value) const
Returns a new DateTime that adds the specified number of milliseconds.
DateTime AddYears(int value) const
Returns a new DateTime that adds the specified number of years.
int GetHour() const
Gets the hour component of the date represented by this instance.
DateTime()
Initializes a new instance of DateTime to 0 ticks (0001-01-01 00:00:00) with Unspecified kind.
int GetYear() const
Gets the year component of the date represented by this instance.
static DateTime Parse(const String &s)
Converts the string representation of a date and time to its DateTime equivalent.
String ToString() const
Converts the value of the current DateTime object to its equivalent string representation.
int GetMinute() const
Gets the minute component of the date represented by this instance.
DateTime AddTicks(int64_t value) const
Returns a new DateTime that adds the specified number of ticks.
DateTime ToLocalTime() const
Converts the value of the current DateTime object to local time.
int GetDayOfYear() const
Gets the day of the year represented by this instance.
TimeSpan GetTimeOfDay() const
Gets the time of day for this instance.
static DateTime UtcNow()
Gets a DateTime object set to the current date and time on this computer, expressed as UTC.
int GetDay() const
Gets the day of the month represented by this instance.
int GetMillisecond() const
Gets the milliseconds component of the date represented by this instance.
DateTime GetDate() const
Gets the date component of this instance with the time set to 00:00:00.
static DateTime Today()
Gets the current date with the time component set to 00:00:00.
static int DaysInMonth(int year, int month)
Returns the number of days in the specified month and year.
DateTime AddMinutes(double value) const
Returns a new DateTime that adds the specified number of minutes.
static bool TryParseExact(const String &s, const String &format, DateTime &result)
Converts the string representation of a date and time to its DateTime equivalent using format and ret...
DateTime AddDays(double value) const
Returns a new DateTime that adds the specified number of days.
Represents errors that occur during application execution.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
String()
Initializes a new instance of the String class to an empty string.
const char * GetRawString() const
Represents a time interval (duration of time or elapsed time) measured as a positive or negative numb...
TimeSpan()
Initializes a new instance of TimeSpan to zero duration.
int64_t GetTicks() const
Gets the number of ticks that represent the value of the current TimeSpan structure.
constexpr int64_t TicksPerMinute
constexpr int64_t TicksPerDay
static struct tm ConvertToUtcTm(int64_t time)
static int64_t DateToTicks(int year, int month, int day)
Converts a Gregorian calendar date into total 100-nanosecond ticks since CE 0001-01-01.
constexpr int64_t TicksPerHour
static std::string ConvertFormat(const String &format)
static void GetDatePart(int64_t ticks, int &year, int &month, int &day)
Decomposes total ticks into Gregorian calendar date components (year, month, day).
static int64_t TmToTicks(const struct tm &t, int64_t remainderTicks)
constexpr int64_t MaxTicks
constexpr int64_t TicksPerMillisecond
DateTimeKind
Specifies whether a DateTime object represents local time, UTC, or is unspecified.
@ Utc
The time represented is UTC.
@ Local
The time represented is local time.
@ Unspecified
The time represented is not specified as either local time or UTC.
constexpr int64_t TicksPerSecond
static struct tm ConvertToLocalTm(int64_t time)
constexpr int64_t MinTicks
static bool ParseDateTimeStr(const String &s, const String &format, DateTime &result)
constexpr int DaysToMonth365[]
constexpr int64_t UnixEpochTicks
constexpr int DaysToMonth366[]
static int64_t TimeToTicks(int hour, int minute, int second)
Converts hour, minute, and second into total 100-nanosecond ticks.