How do I correcly handle ZoneLocalMapping.ResultType.Ambiguous?
Question
In my code I try to handle ZoneLocalMapping.ResultType.Ambiguous. The line
unambiguousLocalDateTime = localDateTimeMapping.EarlierMapping;
throws an InvalidOperationException with message "EarlierMapping property should not be called on a result of type Ambiguous".
I have no clue how I should handle it. Can you give me an example?
This is what my code looks like:
public Instant getInstant(int year, int month, int day, int hour, int minute)
{
var localDateTime = new LocalDateTime(year, month, day, hour, minute); //invalidated, might be not existing
var timezone = DateTimeZone.ForId(TimeZoneId); //TimeZone is set elsewhere, example "Brazil/East"
var localDateTimeMapping = timezone.MapLocalDateTime(localDateTime);
ZonedDateTime unambiguousLocalDateTime;
switch (localDateTimeMapping.Type)
{
case ZoneLocalMapping.ResultType.Unambiguous:
unambiguousLocalDateTime = localDateTimeMapping.UnambiguousMapping;
break;
case ZoneLocalMapping.ResultType.Ambiguous:
unambiguousLocalDateTime = localDateTimeMapping.EarlierMapping;
break;
case ZoneLocalMapping.ResultType.Skipped:
unambiguousLocalDateTime = new ZonedDateTime(localDateTimeMapping.ZoneIntervalAfterTransition.Start, timezone);
break;
default:
throw new InvalidOperationException(string.Format("Unexpected mapping result type: {0}", localDateTimeMapping.Type));
}
return unambiguousLocalDateTime.ToInstant();
}
If I look at class ZoneLocalMapping I see the following code:
/// <summary>
/// In an ambiguous mapping, returns the earlier of the two ZonedDateTimes which map to the original LocalDateTime.
/// </summary>
/// <exception cref="InvalidOperationException">The mapping isn't ambiguous.</exception>
public virtual ZonedDateTime EarlierMapping { get { throw new InvalidOperationException("EarlierMapping property should not be called on a result of type " + type); } }
That's why I am receiving the exception, but what should I do to get the EarlierMapping?
Solution
It looks like you're using a relatively old build. ZoneLocalMapping
has changed a bit since then, and I suspect I've fixed the bug you've found. Here's an example which works:
using System;
using NodaTime;
class Program
{
public static void Main()
{
var local = new LocalDateTime(2012, 10, 28, 1, 30, 0);
var zone = DateTimeZone.ForId("Europe/London");
var mapping = zone.MapLocal(local);
Console.WriteLine(mapping.Count); // 2
Console.WriteLine(mapping.First()); // 1.30 with offset +1
Console.WriteLine(mapping.Last()); // 1.30 with offest +0
}
}
Your actual method can go away entirely now, due to the improved "resolver" API. You can now use:
private static readonly ZoneLocalResolver CustomResolver =
Resolvers.CreateMappingResolver(Resolvers.ReturnEarlier,
Resolvers.ReturnStartOfIntervalAfter);
...
Instant instant = zone.ResolveLocal(localDateTime, CustomResolver).ToInstant();