-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeZones.cs
211 lines (189 loc) · 8.19 KB
/
TimeZones.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using GeoTimeZone;
using NodaTime;
using NodaTime.Extensions;
using TimeZoneConverter;
using TimeZoneNames;
namespace Arex388.TimeZones;
/// <summary>
/// TimeZones object.
/// </summary>
public static class TimeZones {
/// <summary>
/// Returns all time zones for the current instant in time.
/// </summary>
/// <param name="languageCode">The language code to use. "en-US" by default.</param>
/// <returns>The time zones.</returns>
public static IEnumerable<TimeZone> GetTimeZones(
string languageCode = "en-US") {
var instant = SystemClock.Instance.GetCurrentInstant();
return GetTimeZones(instant, languageCode);
}
/// <summary>
/// Returns all time zones for an instant in time.
/// </summary>
/// <param name="dateTime">The DateTimeOffset value to use for an instant in time.</param>
/// <param name="languageCode">The language code to use. "en-US" by default.</param>
/// <returns>The time zones.</returns>
public static IEnumerable<TimeZone> GetTimeZones(
DateTimeOffset? dateTime,
string languageCode = "en-US") {
if (dateTime is null) {
throw new ArgumentNullException(nameof(dateTime));
}
var instant = dateTime.Value.ToInstant();
return GetTimeZones(instant, languageCode);
}
/// <summary>
/// Returns all time zones for an instant in time.
/// </summary>
/// <param name="instant">The instant in time.</param>
/// <param name="languageCode">The language code to use. "en-US" by default.</param>
/// <returns>The time zones.</returns>
public static IEnumerable<TimeZone> GetTimeZones(
Instant instant,
string languageCode = "en-US") => DateTimeZoneProviders.Tzdb.Ids.Select(
id => DateTimeZoneProviders.Tzdb[id]).Select(
dtz => {
var gotWindowsTimeZoneId = TZConvert.TryIanaToWindows(dtz.Id, out var windowsTimeZoneId);
if (!gotWindowsTimeZoneId
|| windowsTimeZoneId is null) {
return null;
}
var isDaylightSavings = TimeZoneInfo.FindSystemTimeZoneById(windowsTimeZoneId).IsDaylightSavingTime(instant.ToDateTimeOffset());
var abbreviation = TZNames.GetAbbreviationsForTimeZone(dtz.Id, languageCode);
var offset = dtz.GetUtcOffset(instant);
return new TimeZone {
Abbreviation = (isDaylightSavings
? abbreviation.Daylight
: abbreviation.Standard) ?? dtz.Id,
IanaId = dtz.Id,
IsDaylightSavings = isDaylightSavings,
UtcOffset = offset.ToFormattedString(),
UtcOffsetTs = offset.ToTimeSpan(),
WindowsId = windowsTimeZoneId
};
}).Where(
tz => tz is not null);
/// <summary>
/// Returns the time zone by the specified latitude and longitude coordinate for the current instant in time.
/// </summary>
/// <param name="latitude">The coordinate's latitude.</param>
/// <param name="longitude">The coordinate's longitude.</param>
/// <returns>The time zone.</returns>
public static TimeZone? GetTimeZoneByCoordinate(
decimal latitude,
decimal longitude) => GetTimeZoneByCoordinate((double)latitude, (double)longitude);
/// <summary>
/// Returns the time zone by the specified latitude and longitude coordinate for the current instant in time.
/// </summary>
/// <param name="latitude">The coordinate's latitude.</param>
/// <param name="longitude">The coordinate's longitude.</param>
/// <returns>The time zone.</returns>
public static TimeZone? GetTimeZoneByCoordinate(
double latitude,
double longitude) => GetTimeZoneByCoordinate(new Coordinate {
Latitude = latitude,
Longitude = longitude
});
/// <summary>
/// Returns the time zone by the specified latitude and longitude coordinate for the current instant in time.
/// </summary>
/// <param name="coordinate">The coordinate.</param>
/// <param name="languageCode">The language code to use. "en-US" by default.</param>
/// <returns>The time zone.</returns>
public static TimeZone? GetTimeZoneByCoordinate(
ICoordinate coordinate,
string languageCode = "en-US") {
var ianaId = TimeZoneLookup.GetTimeZone(coordinate.Latitude, coordinate.Longitude);
return GetTimeZoneByIanaId(ianaId.Result, languageCode);
}
/// <summary>
/// Returns the time zone by the specified latitude and longitude coordinate for the current instant in time.
/// </summary>
/// <param name="latitude">The coordinate's latitude.</param>
/// <param name="longitude">The coordinate's longitude.</param>
/// <param name="dateTime">The DateTimeOffset value to use for an instant in time.</param>
/// <param name="languageCode">The language code to use. "en-US" by default.</param>
/// <returns>The time zone.</returns>
public static TimeZone? GetTimeZoneByCoordinate(
decimal latitude,
decimal longitude,
DateTimeOffset? dateTime,
string languageCode = "en-US") => GetTimeZoneByCoordinate((double)latitude, (double)longitude, dateTime, languageCode);
/// <summary>
/// Returns the time zone by the specified latitude and longitude coordinate for an instant in time.
/// </summary>
/// <param name="latitude">The coordinate's latitude.</param>
/// <param name="longitude">The coordinate's longitude.</param>
/// <param name="dateTime">The DateTimeOffset value to use for an instant in time.</param>
/// <param name="languageCode">The language code to use. "en-US" by default.</param>
/// <returns>The time zone.</returns>
public static TimeZone? GetTimeZoneByCoordinate(
double latitude,
double longitude,
DateTimeOffset? dateTime,
string languageCode = "en-US") {
return GetTimeZoneByCoordinate(new Coordinate {
Latitude = latitude,
Longitude = longitude
}, dateTime, languageCode);
}
/// <summary>
/// Returns the time zone by the specified point coordinate for an instant in time.
/// </summary>
/// <param name="coordinate">The coordinate.</param>
/// <param name="dateTime">The DateTimeOffset value to use for an instant in time.</param>
/// <param name="languageCode">The language code to use. "en-US" by default.</param>
/// <returns>The time zone.</returns>
public static TimeZone? GetTimeZoneByCoordinate(
ICoordinate coordinate,
DateTimeOffset? dateTime,
string languageCode = "en-US") {
var ianaId = TimeZoneLookup.GetTimeZone(coordinate.Latitude, coordinate.Longitude);
return GetTimeZoneByIanaId(ianaId.Result, dateTime, languageCode);
}
/// <summary>
/// Returns the time zone by the IANA id for the current instant in time.
/// </summary>
/// <param name="ianaId">The time zone's IANA id.</param>
/// <param name="languageCode">The language code to use. "en-US" by default.</param>
/// <returns>The time zone.</returns>
public static TimeZone? GetTimeZoneByIanaId(
string ianaId,
string languageCode = "en-US") => GetTimeZones(languageCode).FirstOrDefault(
tz => tz?.IanaId == ianaId);
/// <summary>
/// Returns the time zone by the IANA id for an instant in time.
/// </summary>
/// <param name="ianaId">The time zone's IANA id.</param>
/// <param name="dateTime">The DateTimeOffset value to use for an instant in time.</param>
/// <param name="languageCode">The language code to use. "en-US" by default.</param>
/// <returns>The time zone.</returns>
public static TimeZone? GetTimeZoneByIanaId(
string ianaId,
DateTimeOffset? dateTime,
string languageCode = "en-US") => GetTimeZones(dateTime, languageCode).FirstOrDefault(
tz => tz?.IanaId == ianaId);
/// <summary>
/// Returns the time zones by the Windows id for the current instant in time.
/// </summary>
/// <param name="windowsId">The time zones' Windows id.</param>
/// <param name="languageCode">The language code to use. "en-US" by default.</param>
/// <returns>The time zones.</returns>
public static IEnumerable<TimeZone> GetTimeZonesByWindowsId(
string windowsId,
string languageCode = "en-US") => GetTimeZones(languageCode).Where(
tz => tz?.WindowsId == windowsId);
/// <summary>
/// Returns the time zones by the Windows id for the current instant in time.
/// </summary>
/// <param name="windowsId">The time zones' Windows id.</param>
/// <param name="dateTime">The DateTimeOffset value to use for an instant in time.</param>
/// <param name="languageCode">The language code to use. "en-US" by default.</param>
/// <returns>The time zones.</returns>
public static IEnumerable<TimeZone> GetTimeZonesByWindowsId(
string windowsId,
DateTimeOffset? dateTime,
string languageCode = "en-US") => GetTimeZones(dateTime, languageCode).Where(
tz => tz?.WindowsId == windowsId);
}