aboutsummaryrefslogtreecommitdiffstats
path: root/src/IOL.Helpers/DateTimeHelpers.cs
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2021-04-01 22:36:59 +0200
committerivarlovlie <git@ivarlovlie.no>2021-04-01 22:36:59 +0200
commit9e0fbac6d3cb4e11efdb33c339ccf0adbca0bc6c (patch)
tree8fd1e8710abc3b17f0bc7f960c89931efd83c944 /src/IOL.Helpers/DateTimeHelpers.cs
downloaddotnet-helpers-9e0fbac6d3cb4e11efdb33c339ccf0adbca0bc6c.tar.xz
dotnet-helpers-9e0fbac6d3cb4e11efdb33c339ccf0adbca0bc6c.zip
Initial commit
Diffstat (limited to 'src/IOL.Helpers/DateTimeHelpers.cs')
-rw-r--r--src/IOL.Helpers/DateTimeHelpers.cs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/IOL.Helpers/DateTimeHelpers.cs b/src/IOL.Helpers/DateTimeHelpers.cs
new file mode 100644
index 0000000..07e951e
--- /dev/null
+++ b/src/IOL.Helpers/DateTimeHelpers.cs
@@ -0,0 +1,22 @@
+using System;
+
+namespace IOL.Helpers
+{
+ public static class DateTimeHelpers
+ {
+ public static DateTime ToTimeZoneId(this DateTime value, string timeZoneId) {
+ try {
+ var cstZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
+ return TimeZoneInfo.ConvertTimeFromUtc(value, cstZone);
+ } catch (TimeZoneNotFoundException) {
+ Console.WriteLine("The registry does not define the " + timeZoneId + " zone.");
+ return default;
+ } catch (InvalidTimeZoneException) {
+ Console.WriteLine("Registry data on the " + timeZoneId + " zone has been corrupted.");
+ return default;
+ }
+ }
+
+ public static DateTime ToOsloTimeZone(this DateTime value) => ToTimeZoneId(value, "Europe/Oslo");
+ }
+} \ No newline at end of file