DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
TimeZone.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "System/TimeZone.h"
3
4#if defined(_WIN32)
5#include <windows.h>
6#include "Win32Internal.h"
7using namespace DotNetDupe::System::Internal;
8#endif
9
10namespace DotNetDupe {
11 namespace System {
12 class CurrentTimeZoneImpl : public TimeZone {
13 public:
14 String GetDaylightName() const override {
16#if defined(_WIN32)
17 TIME_ZONE_INFORMATION tzi;
18 if (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_INVALID) {
19 return String(WCharToUtf8(tzi.DaylightName).c_str());
20 }
21 return String("Daylight");
22#else
23 return String("UTC");
24#endif
25 }
26
27 String GetStandardName() const override {
29#if defined(_WIN32)
30 TIME_ZONE_INFORMATION tzi;
31 if (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_INVALID) {
32 return String(WCharToUtf8(tzi.StandardName).c_str());
33 }
34 return String("Standard");
35#else
36 return String("UTC");
37#endif
38 }
39
40 DaylightTime GetDaylightChanges(int year) override {
42#if defined(_WIN32)
43 TIME_ZONE_INFORMATION tzi;
44 if (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_INVALID) {
45 return DaylightTime(DateTimeOffset(0), DateTimeOffset(0), TimeSpan(tzi.DaylightBias * -600000000LL));
46 }
47#endif
49 }
50
51 TimeSpan GetUtcOffset(const DateTimeOffset& time) override {
53#if defined(_WIN32)
54 TIME_ZONE_INFORMATION tzi;
55 DWORD result = GetTimeZoneInformation(&tzi);
56 if (result != TIME_ZONE_ID_INVALID) {
57 long totalBias = tzi.Bias;
58 if (result == TIME_ZONE_ID_DAYLIGHT) {
59 totalBias += tzi.DaylightBias;
60 }
61 else if (result == TIME_ZONE_ID_STANDARD) {
62 totalBias += tzi.StandardBias;
63 }
64 return TimeSpan(totalBias * -600000000LL);
65 }
66#endif
67 return TimeSpan(0);
68 }
69 };
70
73 static CurrentTimeZoneImpl instance;
74 return &instance;
75 }
76
79#if defined(_WIN32)
80 TIME_ZONE_INFORMATION tzi;
81 DWORD result = GetTimeZoneInformation(&tzi);
82 return result == TIME_ZONE_ID_DAYLIGHT;
83#else
84 return false;
85#endif
86 }
87
90 return DateTimeOffset(time.GetTicks() + GetUtcOffset(time).GetTicks());
91 }
92
95 return DateTimeOffset(time.GetTicks() - GetUtcOffset(time).GetTicks());
96 }
97
98 bool TimeZone::IsDaylightSavingTime(const DateTimeOffset& time, const DaylightTime& daylightTimes) {
100 if (daylightTimes.GetDelta().GetTicks() == 0) return false;
101 if (daylightTimes.GetStart() == daylightTimes.GetEnd()) return false;
103 return time >= daylightTimes.GetStart() && time < daylightTimes.GetEnd();
104 }
105 }
106}
Represents a time zone.
Represents a point in time, typically expressed as a date and time of day, relative to Coordinated Un...
int64_t GetTicks() const
Gets the number of ticks that represent the date and time of the current DateTimeOffset object in UTC...
DateTimeOffset()
Initializes a new instance of DateTimeOffset to 0 ticks.
Defines the period of daylight saving time for a region.
TimeSpan GetDelta() const
Gets the time interval that represents the difference between standard time and daylight saving time.
DaylightTime(DateTimeOffset start, DateTimeOffset end, TimeSpan delta)
Initializes a new instance of the DaylightTime class with specified start, end, and time delta.
DateTimeOffset GetEnd() const
Gets the date and time when the daylight saving period ends.
DateTimeOffset GetStart() const
Gets the date and time when the daylight saving period begins.
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
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
Represents a time zone and handles time zone conversions.
Definition TimeZone.h:23
static TimeZone * GetCurrentTimeZone()
Gets the time zone of the current computer.
Definition TimeZone.cpp:71
virtual DateTimeOffset ToLocalTime(const DateTimeOffset &time)
Returns the local time that corresponds to a specified date and time value.
Definition TimeZone.cpp:88
virtual DateTimeOffset ToUniversalTime(const DateTimeOffset &time)
Returns the Coordinated Universal Time (UTC) that corresponds to a specified time.
Definition TimeZone.cpp:93
virtual TimeSpan GetUtcOffset(const DateTimeOffset &time)=0
Returns the Coordinated Universal Time (UTC) offset for the specified time.
virtual bool IsDaylightSavingTime(const DateTimeOffset &time)
Returns a value indicating whether the specified date and time is within a daylight saving time perio...
Definition TimeZone.cpp:77
std::string WCharToUtf8(const wchar_t *pWStr)