From 9e0fbac6d3cb4e11efdb33c339ccf0adbca0bc6c Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Thu, 1 Apr 2021 22:36:59 +0200 Subject: Initial commit --- src/IOL.Helpers/DateTimeHelpers.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/IOL.Helpers/DateTimeHelpers.cs (limited to 'src/IOL.Helpers/DateTimeHelpers.cs') 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 -- cgit v1.3