summaryrefslogtreecommitdiffstats
path: root/src/IOL.Helpers/DateTimeHelpers.cs
blob: 07e951eb1f10bd5f3c933f42b3dd8c25ac0fde21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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");
	}
}