DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
DotNetDupe::System::DateTime Class Reference

Represents an instant in time, typically expressed as a date and time of day. More...

#include <DateTime.h>

Inheritance diagram for DotNetDupe::System::DateTime:

Public Member Functions

 DateTime ()
 Initializes a new instance of DateTime to 0 ticks (0001-01-01 00:00:00) with Unspecified kind.
 DateTime (int year, int month, int day)
 Initializes a new instance of DateTime to specified year, month, and day.
 DateTime (int year, int month, int day, int hour, int minute, int second)
 Initializes a new instance of DateTime to specified date and time components.
 DateTime (int year, int month, int day, int hour, int minute, int second, DateTimeKind kind)
 Initializes a new instance of DateTime to specified date, time components, and kind.
 DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond)
 Initializes a new instance of DateTime with millisecond precision.
 DateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind)
 Initializes a new instance of DateTime with millisecond precision and kind.
 DateTime (int64_t nTicks)
 Initializes a new instance of DateTime with a specified number of ticks.
 DateTime (int64_t nTicks, DateTimeKind kind)
 Initializes a new instance of DateTime with specified ticks and DateTimeKind.
DateTime Add (TimeSpan value) const
 Returns a new DateTime that adds the value of the specified TimeSpan.
DateTime AddDays (double value) const
 Returns a new DateTime that adds the specified number of days.
DateTime AddHours (double value) const
 Returns a new DateTime that adds the specified number of hours.
DateTime AddMilliseconds (double value) const
 Returns a new DateTime that adds the specified number of milliseconds.
DateTime AddMinutes (double value) const
 Returns a new DateTime that adds the specified number of minutes.
DateTime AddMonths (int months) const
 Returns a new DateTime that adds the specified number of months.
DateTime AddSeconds (double value) const
 Returns a new DateTime that adds the specified number of seconds.
DateTime AddTicks (int64_t value) const
 Returns a new DateTime that adds the specified number of ticks.
DateTime AddYears (int value) const
 Returns a new DateTime that adds the specified number of years.
DateTime GetDate () const
 Gets the date component of this instance with the time set to 00:00:00.
int GetDay () const
 Gets the day of the month represented by this instance.
int GetDayOfWeek () const
 Gets the day of the week represented by this instance.
int GetDayOfYear () const
 Gets the day of the year represented by this instance.
int GetHour () const
 Gets the hour component of the date represented by this instance.
DateTimeKind GetKind () const
 Gets a value that indicates whether the time is local, UTC, or neither.
int GetMillisecond () const
 Gets the milliseconds component of the date represented by this instance.
int GetMinute () const
 Gets the minute component of the date represented by this instance.
int GetMonth () const
 Gets the month component of the date represented by this instance.
int GetSecond () const
 Gets the seconds component of the date represented by this instance.
int64_t GetTicks () const
 Gets the number of ticks that represent the date and time of this instance.
TimeSpan GetTimeOfDay () const
 Gets the time of day for this instance.
int GetYear () const
 Gets the year component of the date represented by this instance.
bool operator!= (const DateTime &other) const
DateTime operator+ (const TimeSpan &t) const
 Adds a TimeSpan to this instance.
TimeSpan operator- (const DateTime &other) const
 Subtracts a DateTime from this instance to calculate the elapsed TimeSpan.
DateTime operator- (const TimeSpan &t) const
 Subtracts a TimeSpan from this instance.
bool operator< (const DateTime &other) const
bool operator<= (const DateTime &other) const
bool operator== (const DateTime &other) const
bool operator> (const DateTime &other) const
bool operator>= (const DateTime &other) const
DateTime ToLocalTime () const
 Converts the value of the current DateTime object to local time.
String ToString () const
 Converts the value of the current DateTime object to its equivalent string representation.
String ToString (const String &sFormat) const
 Converts the value of the current DateTime object to its equivalent string representation using format.
DateTime ToUniversalTime () const
 Converts the value of the current DateTime object to Coordinated Universal Time (UTC).
Public Member Functions inherited from DotNetDupe::System::Object
virtual ~Object ()=default
 Virtual destructor ensuring polymorphic cleanup.
virtual bool Equals (const Object &obj) const
 Determines whether the specified Object is equal to the current Object.
virtual int GetHashCode () const
 Serves as the default hash function.
bool operator== (const Object &obj) const
 Determines reference equality between two objects.
char * ToStringA () const
 Returns narrow-character string representation.
wchar_t * ToStringW () const
 Returns wide-character string representation.

Static Public Member Functions

static int DaysInMonth (int year, int month)
 Returns the number of days in the specified month and year.
static bool IsLeapYear (int year)
 Returns an indication whether the specified year is a leap year.
static DateTime Now ()
 Gets a DateTime object set to the current date and time on this computer, expressed as local time.
static DateTime Parse (const String &s)
 Converts the string representation of a date and time to its DateTime equivalent.
static DateTime ParseExact (const String &s, const String &format)
 Converts the string representation of a date and time to its DateTime equivalent using specified format.
static DateTime Today ()
 Gets the current date with the time component set to 00:00:00.
static bool TryParse (const String &s, DateTime &result)
 Converts the string representation of a date and time to its DateTime equivalent and returns success flag.
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 returns success flag.
static DateTime UtcNow ()
 Gets a DateTime object set to the current date and time on this computer, expressed as UTC.
Static Public Member Functions inherited from DotNetDupe::System::Object
static bool Equals (const Object &obj1, const Object &obj2)
 Static helper determining whether two object instances are equal.

Detailed Description

Represents an instant in time, typically expressed as a date and time of day.

Note
Conforms to ECMA-335 Partition IV Section 5.16 (System.DateTime), ISO 8601, and the proleptic Gregorian calendar system. Time values are measured in 100-nanosecond units called ticks, starting from 00:00:00.0000000 UTC on January 1, 0001 (Common Era).

Definition at line 31 of file DateTime.h.

Constructor & Destructor Documentation

◆ DateTime() [1/8]

◆ DateTime() [2/8]

DotNetDupe::System::DateTime::DateTime ( int64_t nTicks)
inline

Initializes a new instance of DateTime with a specified number of ticks.

Parameters
nTicksA date and time expressed in the number of 100-nanosecond units.

Definition at line 38 of file DateTime.h.

References DOTNETDUPE_API, and DotNetDupe::System::Unspecified.

◆ DateTime() [3/8]

DotNetDupe::System::DateTime::DateTime ( int64_t nTicks,
DateTimeKind kind )
inline

Initializes a new instance of DateTime with specified ticks and DateTimeKind.

Parameters
nTicksA date and time expressed in ticks.
kindOne of the enumeration values that indicates whether ticks specifies local time, UTC, or neither.

Definition at line 43 of file DateTime.h.

References DOTNETDUPE_API.

◆ DateTime() [4/8]

DotNetDupe::System::DateTime::DateTime ( int year,
int month,
int day )

Initializes a new instance of DateTime to specified year, month, and day.

Parameters
yearThe year (1 through 9999).
monthThe month (1 through 12).
dayThe day (1 through the number of days in month).

Definition at line 82 of file DateTime.cpp.

References DotNetDupe::System::DateToTicks(), and DotNetDupe::System::Unspecified.

◆ DateTime() [5/8]

DotNetDupe::System::DateTime::DateTime ( int year,
int month,
int day,
int hour,
int minute,
int second )

Initializes a new instance of DateTime to specified date and time components.

Parameters
yearThe year (1 through 9999).
monthThe month (1 through 12).
dayThe day (1 through the number of days in month).
hourThe hours (0 through 23).
minuteThe minutes (0 through 59).
secondThe seconds (0 through 59).

Definition at line 85 of file DateTime.cpp.

References DotNetDupe::System::DateToTicks(), DotNetDupe::System::TimeToTicks(), and DotNetDupe::System::Unspecified.

◆ DateTime() [6/8]

DotNetDupe::System::DateTime::DateTime ( int year,
int month,
int day,
int hour,
int minute,
int second,
DateTimeKind kind )

Initializes a new instance of DateTime to specified date, time components, and kind.

Definition at line 88 of file DateTime.cpp.

References DotNetDupe::System::DateToTicks(), and DotNetDupe::System::TimeToTicks().

◆ DateTime() [7/8]

DotNetDupe::System::DateTime::DateTime ( int year,
int month,
int day,
int hour,
int minute,
int second,
int millisecond )

◆ DateTime() [8/8]

DotNetDupe::System::DateTime::DateTime ( int year,
int month,
int day,
int hour,
int minute,
int second,
int millisecond,
DateTimeKind kind )

Initializes a new instance of DateTime with millisecond precision and kind.

Guard: Validate millisecond range.

Definition at line 99 of file DateTime.cpp.

References DotNetDupe::System::ArgumentOutOfRangeException::ArgumentOutOfRangeException(), DotNetDupe::System::DateToTicks(), DotNetDupe::System::TicksPerMillisecond, and DotNetDupe::System::TimeToTicks().

Member Function Documentation

◆ Add()

DateTime DotNetDupe::System::DateTime::Add ( TimeSpan value) const

Returns a new DateTime that adds the value of the specified TimeSpan.

Add duration ticks to date.

Definition at line 172 of file DateTime.cpp.

References DateTime(), AddTicks(), and DotNetDupe::System::TimeSpan::GetTicks().

◆ AddDays()

DateTime DotNetDupe::System::DateTime::AddDays ( double value) const

Returns a new DateTime that adds the specified number of days.

Scale days to milliseconds and add.

Definition at line 177 of file DateTime.cpp.

References DateTime(), and AddMilliseconds().

◆ AddHours()

DateTime DotNetDupe::System::DateTime::AddHours ( double value) const

Returns a new DateTime that adds the specified number of hours.

Scale hours to milliseconds and add.

Definition at line 182 of file DateTime.cpp.

References DateTime(), and AddMilliseconds().

◆ AddMilliseconds()

DateTime DotNetDupe::System::DateTime::AddMilliseconds ( double value) const

Returns a new DateTime that adds the specified number of milliseconds.

Convert milliseconds to nearest integer ticks with rounding.

Definition at line 187 of file DateTime.cpp.

References DateTime(), AddTicks(), and DotNetDupe::System::TicksPerMillisecond.

Referenced by AddDays(), AddHours(), AddMinutes(), and AddSeconds().

◆ AddMinutes()

DateTime DotNetDupe::System::DateTime::AddMinutes ( double value) const

Returns a new DateTime that adds the specified number of minutes.

Scale minutes to milliseconds and add.

Definition at line 193 of file DateTime.cpp.

References DateTime(), and AddMilliseconds().

◆ AddMonths()

DateTime DotNetDupe::System::DateTime::AddMonths ( int months) const

Returns a new DateTime that adds the specified number of months.

Guard: Validate month offset limit.

Decompose date and adjust month and year.

Validate year range and clamp day of month.

Reconstruct DateTime preserving time of day.

Definition at line 198 of file DateTime.cpp.

References DotNetDupe::System::ArgumentOutOfRangeException::ArgumentOutOfRangeException(), DateTime(), DotNetDupe::System::DateToTicks(), DaysInMonth(), DotNetDupe::System::GetDatePart(), and DotNetDupe::System::TicksPerDay.

Referenced by AddYears().

◆ AddSeconds()

DateTime DotNetDupe::System::DateTime::AddSeconds ( double value) const

Returns a new DateTime that adds the specified number of seconds.

Scale seconds to milliseconds and add.

Definition at line 217 of file DateTime.cpp.

References DateTime(), and AddMilliseconds().

◆ AddTicks()

DateTime DotNetDupe::System::DateTime::AddTicks ( int64_t value) const

Returns a new DateTime that adds the specified number of ticks.

Calculate new ticks and enforce minimum/maximum boundaries.

Definition at line 222 of file DateTime.cpp.

References DotNetDupe::System::ArgumentOutOfRangeException::ArgumentOutOfRangeException(), DateTime(), and DotNetDupe::System::MaxTicks.

Referenced by Add(), and AddMilliseconds().

◆ AddYears()

DateTime DotNetDupe::System::DateTime::AddYears ( int value) const

Returns a new DateTime that adds the specified number of years.

Guard: Validate year boundary.

Add months corresponding to full years.

Definition at line 232 of file DateTime.cpp.

References DotNetDupe::System::ArgumentOutOfRangeException::ArgumentOutOfRangeException(), DateTime(), and AddMonths().

◆ DaysInMonth()

int DotNetDupe::System::DateTime::DaysInMonth ( int year,
int month )
static

Returns the number of days in the specified month and year.

Guard: Validate month parameter.

Look up day count using leap-year aware month table.

Definition at line 333 of file DateTime.cpp.

References DotNetDupe::System::ArgumentOutOfRangeException::ArgumentOutOfRangeException(), DotNetDupe::System::DaysToMonth365, DotNetDupe::System::DaysToMonth366, and IsLeapYear().

Referenced by AddMonths().

◆ GetDate()

DateTime DotNetDupe::System::DateTime::GetDate ( ) const

Gets the date component of this instance with the time set to 00:00:00.

Returns
A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).

Strip time of day portion by rounding down to day ticks.

Definition at line 162 of file DateTime.cpp.

References DateTime(), and DotNetDupe::System::TicksPerDay.

Referenced by Today().

◆ GetDay()

int DotNetDupe::System::DateTime::GetDay ( ) const

Gets the day of the month represented by this instance.

Returns
The day component, expressed as a value between 1 and 31.

Decompose ticks to calendar date.

Definition at line 121 of file DateTime.cpp.

References DotNetDupe::System::GetDatePart().

◆ GetDayOfWeek()

int DotNetDupe::System::DateTime::GetDayOfWeek ( ) const

Gets the day of the week represented by this instance.

Returns
An integer representing Sunday (0) through Saturday (6).

Compute 0-indexed day of week where Sunday is 0.

Definition at line 157 of file DateTime.cpp.

References DotNetDupe::System::TicksPerDay.

◆ GetDayOfYear()

int DotNetDupe::System::DateTime::GetDayOfYear ( ) const

Gets the day of the year represented by this instance.

Returns
The day of the year, expressed as a value between 1 and 366.

Decompose ticks to calendar date and add day offset.

Definition at line 148 of file DateTime.cpp.

References DotNetDupe::System::DaysToMonth365, DotNetDupe::System::DaysToMonth366, DotNetDupe::System::GetDatePart(), and IsLeapYear().

◆ GetHour()

int DotNetDupe::System::DateTime::GetHour ( ) const

Gets the hour component of the date represented by this instance.

Returns
The hour component, expressed as a value between 0 and 23.

Calculate hour from ticks quotient modulo 24.

Definition at line 128 of file DateTime.cpp.

References DotNetDupe::System::TicksPerHour.

Referenced by ToString().

◆ GetKind()

DateTimeKind DotNetDupe::System::DateTime::GetKind ( ) const
inline

Gets a value that indicates whether the time is local, UTC, or neither.

Returns
One of the DateTimeKind enumeration values.

Definition at line 75 of file DateTime.h.

◆ GetMillisecond()

int DotNetDupe::System::DateTime::GetMillisecond ( ) const

Gets the milliseconds component of the date represented by this instance.

Returns
The milliseconds component, expressed as a value between 0 and 999.

Calculate millisecond from ticks quotient modulo 1000.

Definition at line 143 of file DateTime.cpp.

References DotNetDupe::System::TicksPerMillisecond.

◆ GetMinute()

int DotNetDupe::System::DateTime::GetMinute ( ) const

Gets the minute component of the date represented by this instance.

Returns
The minute component, expressed as a value between 0 and 59.

Calculate minute from ticks quotient modulo 60.

Definition at line 133 of file DateTime.cpp.

References DotNetDupe::System::TicksPerMinute.

Referenced by ToString().

◆ GetMonth()

int DotNetDupe::System::DateTime::GetMonth ( ) const

Gets the month component of the date represented by this instance.

Returns
The month component, expressed as a value between 1 and 12.

Decompose ticks to calendar date.

Definition at line 114 of file DateTime.cpp.

References DotNetDupe::System::GetDatePart().

◆ GetSecond()

int DotNetDupe::System::DateTime::GetSecond ( ) const

Gets the seconds component of the date represented by this instance.

Returns
The seconds component, expressed as a value between 0 and 59.

Calculate second from ticks quotient modulo 60.

Definition at line 138 of file DateTime.cpp.

References DotNetDupe::System::TicksPerSecond.

Referenced by ToString().

◆ GetTicks()

int64_t DotNetDupe::System::DateTime::GetTicks ( ) const
inline

Gets the number of ticks that represent the date and time of this instance.

Returns
The number of 100-nanosecond ticks.

Definition at line 71 of file DateTime.h.

◆ GetTimeOfDay()

TimeSpan DotNetDupe::System::DateTime::GetTimeOfDay ( ) const

Gets the time of day for this instance.

Returns
A time interval that represents the fraction of the day that has elapsed since midnight.

Extract time portion as TimeSpan modulo one day.

Definition at line 167 of file DateTime.cpp.

References DotNetDupe::System::TimeSpan::TimeSpan(), and DotNetDupe::System::TicksPerDay.

◆ GetYear()

int DotNetDupe::System::DateTime::GetYear ( ) const

Gets the year component of the date represented by this instance.

Returns
The year, between 1 and 9999.

Decompose ticks to calendar date.

Definition at line 107 of file DateTime.cpp.

References DotNetDupe::System::GetDatePart().

◆ IsLeapYear()

bool DotNetDupe::System::DateTime::IsLeapYear ( int year)
static

Returns an indication whether the specified year is a leap year.

Guard: Validate Gregorian year boundaries.

Gregorian leap year rule: divisible by 4, not divisible by 100 unless divisible by 400.

Definition at line 344 of file DateTime.cpp.

References DotNetDupe::System::ArgumentOutOfRangeException::ArgumentOutOfRangeException().

Referenced by DotNetDupe::System::DateToTicks(), DaysInMonth(), DotNetDupe::System::GetDatePart(), and GetDayOfYear().

◆ Now()

DateTime DotNetDupe::System::DateTime::Now ( )
static

Gets a DateTime object set to the current date and time on this computer, expressed as local time.

Obtain UTC time and convert to local time zone.

Definition at line 242 of file DateTime.cpp.

References DateTime(), ToLocalTime(), and UtcNow().

Referenced by Today().

◆ operator!=()

bool DotNetDupe::System::DateTime::operator!= ( const DateTime & other) const
inline

Definition at line 207 of file DateTime.h.

References DateTime().

◆ operator+()

DateTime DotNetDupe::System::DateTime::operator+ ( const TimeSpan & t) const
inline

Adds a TimeSpan to this instance.

Definition at line 197 of file DateTime.h.

References DateTime(), and DotNetDupe::System::TimeSpan::GetTicks().

◆ operator-() [1/2]

TimeSpan DotNetDupe::System::DateTime::operator- ( const DateTime & other) const
inline

Subtracts a DateTime from this instance to calculate the elapsed TimeSpan.

Definition at line 192 of file DateTime.h.

References DateTime(), and DotNetDupe::System::TimeSpan::TimeSpan().

◆ operator-() [2/2]

DateTime DotNetDupe::System::DateTime::operator- ( const TimeSpan & t) const
inline

Subtracts a TimeSpan from this instance.

Definition at line 202 of file DateTime.h.

References DateTime(), and DotNetDupe::System::TimeSpan::GetTicks().

◆ operator<()

bool DotNetDupe::System::DateTime::operator< ( const DateTime & other) const
inline

Definition at line 208 of file DateTime.h.

References DateTime().

◆ operator<=()

bool DotNetDupe::System::DateTime::operator<= ( const DateTime & other) const
inline

Definition at line 209 of file DateTime.h.

References DateTime().

◆ operator==()

bool DotNetDupe::System::DateTime::operator== ( const DateTime & other) const
inline

Definition at line 206 of file DateTime.h.

References DateTime().

◆ operator>()

bool DotNetDupe::System::DateTime::operator> ( const DateTime & other) const
inline

Definition at line 210 of file DateTime.h.

References DateTime().

◆ operator>=()

bool DotNetDupe::System::DateTime::operator>= ( const DateTime & other) const
inline

Definition at line 211 of file DateTime.h.

References DateTime().

◆ Parse()

DateTime DotNetDupe::System::DateTime::Parse ( const String & s)
static

Converts the string representation of a date and time to its DateTime equivalent.

Parameters
sA string that contains a date and time to convert.
Returns
An object that is equivalent to the date and time contained in s.
Exceptions
FormatExceptions does not contain a valid string representation of a date and time.

Attempt parse and throw FormatException on failure.

Definition at line 410 of file DateTime.cpp.

References DateTime(), DotNetDupe::System::FormatException::FormatException(), and TryParse().

◆ ParseExact()

DateTime DotNetDupe::System::DateTime::ParseExact ( const String & s,
const String & format )
static

Converts the string representation of a date and time to its DateTime equivalent using specified format.

Exceptions
FormatExceptions does not match format.

Attempt exact parse and throw FormatException on failure.

Definition at line 400 of file DateTime.cpp.

References DateTime(), DotNetDupe::System::FormatException::FormatException(), and TryParseExact().

◆ Today()

DateTime DotNetDupe::System::DateTime::Today ( )
static

Gets the current date with the time component set to 00:00:00.

Obtain current local date with midnight time component.

Definition at line 259 of file DateTime.cpp.

References DateTime(), GetDate(), and Now().

◆ ToLocalTime()

DateTime DotNetDupe::System::DateTime::ToLocalTime ( ) const

Converts the value of the current DateTime object to local time.

Guard: Return directly if already local time.

Convert ticks relative to Unix epoch to local calendar.

Definition at line 297 of file DateTime.cpp.

References DateTime(), DotNetDupe::System::ConvertToLocalTm(), DotNetDupe::System::Local, DotNetDupe::System::TicksPerSecond, DotNetDupe::System::TmToTicks(), and DotNetDupe::System::UnixEpochTicks.

Referenced by Now().

◆ ToString() [1/2]

String DotNetDupe::System::DateTime::ToString ( ) const

Converts the value of the current DateTime object to its equivalent string representation.

Format using default ISO 8601 extended pattern.

Definition at line 317 of file DateTime.cpp.

References ToString().

Referenced by ToString().

◆ ToString() [2/2]

String DotNetDupe::System::DateTime::ToString ( const String & sFormat) const

Converts the value of the current DateTime object to its equivalent string representation using format.

Format date components using standard ISO format.

Definition at line 322 of file DateTime.cpp.

References DotNetDupe::System::String::String(), DotNetDupe::System::GetDatePart(), GetHour(), GetMinute(), and GetSecond().

◆ ToUniversalTime()

DateTime DotNetDupe::System::DateTime::ToUniversalTime ( ) const

Converts the value of the current DateTime object to Coordinated Universal Time (UTC).

Guard: Return directly if already UTC.

Convert ticks relative to Unix epoch to UTC calendar.

Definition at line 307 of file DateTime.cpp.

References DateTime(), DotNetDupe::System::ConvertToUtcTm(), DotNetDupe::System::TicksPerSecond, DotNetDupe::System::TmToTicks(), DotNetDupe::System::UnixEpochTicks, and DotNetDupe::System::Utc.

◆ TryParse()

bool DotNetDupe::System::DateTime::TryParse ( const String & s,
DateTime & result )
static

Converts the string representation of a date and time to its DateTime equivalent and returns success flag.

Attempt parse against default ISO format.

Definition at line 395 of file DateTime.cpp.

References DateTime(), and DotNetDupe::System::ParseDateTimeStr().

Referenced by Parse().

◆ TryParseExact()

bool DotNetDupe::System::DateTime::TryParseExact ( const String & s,
const String & format,
DateTime & result )
static

Converts the string representation of a date and time to its DateTime equivalent using format and returns success flag.

Attempt parse against exact format specification.

Definition at line 390 of file DateTime.cpp.

References DateTime(), and DotNetDupe::System::ParseDateTimeStr().

Referenced by ParseExact().

◆ UtcNow()

DateTime DotNetDupe::System::DateTime::UtcNow ( )
static

Gets a DateTime object set to the current date and time on this computer, expressed as UTC.

Query high-resolution system clock.

Convert microseconds to 100-nanosecond ticks and offset by Unix epoch.

Definition at line 248 of file DateTime.cpp.

References DateTime(), DotNetDupe::System::UnixEpochTicks, and DotNetDupe::System::Utc.

Referenced by Now().


The documentation for this class was generated from the following files: