I use Noda Time, and have the following code:
var pattern = ZonedDateTimePattern.CreateWithInvariantCulture(
"yyyy-MM-dd HH:mm:ss z",
DateTimeZoneProviders.Tzdb);
var parsed = pattern.Parse("2017-11-05 01:00:00 America/Los_Angeles");
Console.WriteLine(parsed.Value);
This results in an UnparsableValueException
with the message:
The local date/time is ambiguous in the target time zone
The problem, as I understand it, is that this specific time can occur twice because of daylight saving. At 02:00, the clock is turned back one hour to 01:00. NodaTime doesn't know which "version" of 01:00 that the string refers to, and because of that the exception is thrown.
To me, it doesn't really matter which version of the time that the parse results in, I just want to avoid the exception, and get a date that is as close to reality as possible. One hour less or more is OK. What would be the best way to do that?
The only way I can think of is splitting the string and parsing the parts individually, and then add one hour, but that feels completely wrong. Is there a better solution?