/
WuLocationExtensions.cs
40 lines (26 loc) · 1008 Bytes
/
WuLocationExtensions.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
using System;
namespace XWeather.Unified
{
public static class WuLocationExtensions
{
public static DateTime LocalTime (this WuLocation location)
{
var tzId = location?.Location?.tz ?? location?.Conditions?.local_tz_long ?? location?.TodayForecast?.date?.tz_long;
var now = DateTime.Now;
if (string.IsNullOrWhiteSpace (tzId)) return now;
try {
//var tzInfo = TimeZoneInfo.FindSystemTimeZoneById (location.Location.tz);
return TimeZoneInfo.ConvertTimeBySystemTimeZoneId (now, tzId);
} catch (TimeZoneNotFoundException tzEx) {
System.Diagnostics.Debug.WriteLine (tzEx.Message);
System.Diagnostics.Debug.WriteLine (tzId);
return now;
} catch (Exception ex) {
System.Diagnostics.Debug.WriteLine (ex.Message);
return now;
}
}
public static string LocalTimeString (this WuLocation location, bool lowercase = true)
=> lowercase ? location?.LocalTime ().ToShortTimeString ().ToLower () : location?.LocalTime ().ToShortTimeString ();
}
}