DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
DateTime.h
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "Common.h"
9#include "System/Object.h"
10#include "System/String.h"
11#include "System/TimeSpan.h"
12#include <cstdint>
13
14namespace DotNetDupe {
15 namespace System {
16
19 enum class DateTimeKind {
21 Utc = 1,
22 Local = 2
23 };
24
31 class DateTime : public Object {
32 public:
34 DOTNETDUPE_API DateTime() : m_nTicks(0), m_kind(DateTimeKind::Unspecified) { }
35
38 DOTNETDUPE_API DateTime(int64_t nTicks) : m_nTicks(nTicks), m_kind(DateTimeKind::Unspecified) { }
39
43 DOTNETDUPE_API DateTime(int64_t nTicks, DateTimeKind kind) : m_nTicks(nTicks), m_kind(kind) { }
44
49 DOTNETDUPE_API DateTime(int year, int month, int day);
50
58 DOTNETDUPE_API DateTime(int year, int month, int day, int hour, int minute, int second);
59
61 DOTNETDUPE_API DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);
62
64 DOTNETDUPE_API DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);
65
67 DOTNETDUPE_API DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind);
68
71 int64_t GetTicks() const { return m_nTicks; }
72
75 DateTimeKind GetKind() const { return m_kind; }
76
79 DOTNETDUPE_API int GetYear() const;
80
83 DOTNETDUPE_API int GetMonth() const;
84
87 DOTNETDUPE_API int GetDay() const;
88
91 DOTNETDUPE_API int GetHour() const;
92
95 DOTNETDUPE_API int GetMinute() const;
96
99 DOTNETDUPE_API int GetSecond() const;
100
103 DOTNETDUPE_API int GetMillisecond() const;
104
107 DOTNETDUPE_API int GetDayOfYear() const;
108
111 DOTNETDUPE_API int GetDayOfWeek() const;
112
116
120
122 DOTNETDUPE_API DateTime Add(TimeSpan value) const;
123
125 DOTNETDUPE_API DateTime AddDays(double value) const;
126
128 DOTNETDUPE_API DateTime AddHours(double value) const;
129
131 DOTNETDUPE_API DateTime AddMilliseconds(double value) const;
132
134 DOTNETDUPE_API DateTime AddMinutes(double value) const;
135
137 DOTNETDUPE_API DateTime AddMonths(int months) const;
138
140 DOTNETDUPE_API DateTime AddSeconds(double value) const;
141
143 DOTNETDUPE_API DateTime AddTicks(int64_t value) const;
144
146 DOTNETDUPE_API DateTime AddYears(int value) const;
147
149 DOTNETDUPE_API static DateTime Now();
150
153
156
161 DOTNETDUPE_API static DateTime Parse(const String& s);
162
165 DOTNETDUPE_API static DateTime ParseExact(const String& s, const String& format);
166
168 DOTNETDUPE_API static bool TryParse(const String& s, DateTime& result);
169
171 DOTNETDUPE_API static bool TryParseExact(const String& s, const String& format, DateTime& result);
172
175
178
181
183 DOTNETDUPE_API String ToString(const String& sFormat) const;
184
186 DOTNETDUPE_API static int DaysInMonth(int year, int month);
187
189 DOTNETDUPE_API static bool IsLeapYear(int year);
190
192 TimeSpan operator-(const DateTime& other) const {
193 return TimeSpan(m_nTicks - other.m_nTicks);
194 }
195
197 DateTime operator+(const TimeSpan& t) const {
198 return DateTime(m_nTicks + t.GetTicks(), m_kind);
199 }
200
202 DateTime operator-(const TimeSpan& t) const {
203 return DateTime(m_nTicks - t.GetTicks(), m_kind);
204 }
205
206 bool operator==(const DateTime& other) const { return m_nTicks == other.m_nTicks; }
207 bool operator!=(const DateTime& other) const { return m_nTicks != other.m_nTicks; }
208 bool operator<(const DateTime& other) const { return m_nTicks < other.m_nTicks; }
209 bool operator<=(const DateTime& other) const { return m_nTicks <= other.m_nTicks; }
210 bool operator>(const DateTime& other) const { return m_nTicks > other.m_nTicks; }
211 bool operator>=(const DateTime& other) const { return m_nTicks >= other.m_nTicks; }
212
213 private:
214 int64_t m_nTicks;
215 DateTimeKind m_kind;
216 };
217 }
218}
Defines common cross-platform macros, export decorators, and fundamental types.
#define DOTNETDUPE_API
Platform-specific linkage decoration for exporting or importing library symbols.
Definition Common.h:20
Base object class for DotNetDupe mirroring .NET System.Object.
High-performance UTF-8 / UTF-16 string manipulation class mirroring .NET System.String.
Represents a time interval.
Represents an instant in time, typically expressed as a date and time of day.
Definition DateTime.h:31
int GetSecond() const
Gets the seconds component of the date represented by this instance.
Definition DateTime.cpp:138
static bool IsLeapYear(int year)
Returns an indication whether the specified year is a leap year.
Definition DateTime.cpp:344
DateTime Add(TimeSpan value) const
Returns a new DateTime that adds the value of the specified TimeSpan.
Definition DateTime.cpp:172
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...
Definition DateTime.cpp:400
int64_t GetTicks() const
Gets the number of ticks that represent the date and time of this instance.
Definition DateTime.h:71
bool operator<(const DateTime &other) const
Definition DateTime.h:208
DateTime operator+(const TimeSpan &t) const
Adds a TimeSpan to this instance.
Definition DateTime.h:197
DateTime AddSeconds(double value) const
Returns a new DateTime that adds the specified number of seconds.
Definition DateTime.cpp:217
DateTime AddMonths(int months) const
Returns a new DateTime that adds the specified number of months.
Definition DateTime.cpp:198
DateTime operator-(const TimeSpan &t) const
Subtracts a TimeSpan from this instance.
Definition DateTime.h:202
DateTime AddHours(double value) const
Returns a new DateTime that adds the specified number of hours.
Definition DateTime.cpp:182
static DateTime Now()
Gets a DateTime object set to the current date and time on this computer, expressed as local time.
Definition DateTime.cpp:242
DateTime ToUniversalTime() const
Converts the value of the current DateTime object to Coordinated Universal Time (UTC).
Definition DateTime.cpp:307
int GetMonth() const
Gets the month component of the date represented by this instance.
Definition DateTime.cpp:114
static bool TryParse(const String &s, DateTime &result)
Converts the string representation of a date and time to its DateTime equivalent and returns success ...
Definition DateTime.cpp:395
int GetDayOfWeek() const
Gets the day of the week represented by this instance.
Definition DateTime.cpp:157
DateTime AddMilliseconds(double value) const
Returns a new DateTime that adds the specified number of milliseconds.
Definition DateTime.cpp:187
DateTime AddYears(int value) const
Returns a new DateTime that adds the specified number of years.
Definition DateTime.cpp:232
int GetHour() const
Gets the hour component of the date represented by this instance.
Definition DateTime.cpp:128
DateTimeKind GetKind() const
Gets a value that indicates whether the time is local, UTC, or neither.
Definition DateTime.h:75
DateTime()
Initializes a new instance of DateTime to 0 ticks (0001-01-01 00:00:00) with Unspecified kind.
Definition DateTime.h:34
bool operator<=(const DateTime &other) const
Definition DateTime.h:209
TimeSpan operator-(const DateTime &other) const
Subtracts a DateTime from this instance to calculate the elapsed TimeSpan.
Definition DateTime.h:192
bool operator!=(const DateTime &other) const
Definition DateTime.h:207
int GetYear() const
Gets the year component of the date represented by this instance.
Definition DateTime.cpp:107
DateTime(int64_t nTicks)
Initializes a new instance of DateTime with a specified number of ticks.
Definition DateTime.h:38
static DateTime Parse(const String &s)
Converts the string representation of a date and time to its DateTime equivalent.
Definition DateTime.cpp:410
String ToString() const
Converts the value of the current DateTime object to its equivalent string representation.
Definition DateTime.cpp:317
int GetMinute() const
Gets the minute component of the date represented by this instance.
Definition DateTime.cpp:133
DateTime(int64_t nTicks, DateTimeKind kind)
Initializes a new instance of DateTime with specified ticks and DateTimeKind.
Definition DateTime.h:43
DateTime AddTicks(int64_t value) const
Returns a new DateTime that adds the specified number of ticks.
Definition DateTime.cpp:222
DateTime ToLocalTime() const
Converts the value of the current DateTime object to local time.
Definition DateTime.cpp:297
int GetDayOfYear() const
Gets the day of the year represented by this instance.
Definition DateTime.cpp:148
bool operator>(const DateTime &other) const
Definition DateTime.h:210
TimeSpan GetTimeOfDay() const
Gets the time of day for this instance.
Definition DateTime.cpp:167
bool operator>=(const DateTime &other) const
Definition DateTime.h:211
static DateTime UtcNow()
Gets a DateTime object set to the current date and time on this computer, expressed as UTC.
Definition DateTime.cpp:248
int GetDay() const
Gets the day of the month represented by this instance.
Definition DateTime.cpp:121
int GetMillisecond() const
Gets the milliseconds component of the date represented by this instance.
Definition DateTime.cpp:143
DateTime GetDate() const
Gets the date component of this instance with the time set to 00:00:00.
Definition DateTime.cpp:162
static DateTime Today()
Gets the current date with the time component set to 00:00:00.
Definition DateTime.cpp:259
static int DaysInMonth(int year, int month)
Returns the number of days in the specified month and year.
Definition DateTime.cpp:333
bool operator==(const DateTime &other) const
Definition DateTime.h:206
DateTime AddMinutes(double value) const
Returns a new DateTime that adds the specified number of minutes.
Definition DateTime.cpp:193
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...
Definition DateTime.cpp:390
DateTime AddDays(double value) const
Returns a new DateTime that adds the specified number of days.
Definition DateTime.cpp:177
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
Represents a time interval (duration of time or elapsed time) measured as a positive or negative numb...
Definition TimeSpan.h:20
TimeSpan()
Initializes a new instance of TimeSpan to zero duration.
Definition TimeSpan.h:38
int64_t GetTicks() const
Gets the number of ticks that represent the value of the current TimeSpan structure.
Definition TimeSpan.h:46
DateTimeKind
Specifies whether a DateTime object represents local time, UTC, or is unspecified.
Definition DateTime.h:19
@ Utc
The time represented is UTC.
Definition DateTime.h:21
@ Local
The time represented is local time.
Definition DateTime.h:22
@ Unspecified
The time represented is not specified as either local time or UTC.
Definition DateTime.h:20