diff --git a/.nuke/build.schema.json b/.nuke/build.schema.json index 6d511bc81..c5dc88e6c 100644 --- a/.nuke/build.schema.json +++ b/.nuke/build.schema.json @@ -37,6 +37,7 @@ "AppVeyor", "AzurePipelines", "Bamboo", + "Bitbucket", "Bitrise", "GitHubActions", "GitLab", diff --git a/src/Mapping/Profiles/NodaTimeProfile.cs b/src/Mapping/Profiles/NodaTimeProfile.cs index 3589e0a80..05d732e0b 100644 --- a/src/Mapping/Profiles/NodaTimeProfile.cs +++ b/src/Mapping/Profiles/NodaTimeProfile.cs @@ -1,5 +1,6 @@ using AutoMapper; using NodaTime; +using NodaTime.Extensions; using NodaTime.Text; namespace Rocket.Surgery.LaunchPad.Mapping.Profiles; @@ -93,28 +94,33 @@ private void CreateMappingsForInstantConvertor() private void CreateMappingsForLocalDateConverter() { - CreateMap().ConvertUsing(source => LocalDateTime.FromDateTime(source).Date); - CreateMap().ConvertUsing(source => source.HasValue ? LocalDateTime.FromDateTime(source.Value).Date : default(LocalDate?)); + CreateMap().ConvertUsing(source => LocalDate.FromDateTime(source)); + CreateMap().ConvertUsing(source => source.HasValue ? LocalDate.FromDateTime(source.Value) : default(LocalDate?)); CreateMap().ConvertUsing(source => source.AtMidnight().ToDateTimeUnspecified()); CreateMap().ConvertUsing(source => source.HasValue ? source.Value.AtMidnight().ToDateTimeUnspecified() : default(DateTime?)); +#if NET6_0_OR_GREATER + CreateMap().ConvertUsing(source => LocalDate.FromDateOnly(source)); + CreateMap().ConvertUsing(source => source.HasValue ? LocalDate.FromDateOnly(source.Value) : default(LocalDate?)); + CreateMap().ConvertUsing(source => source.ToDateOnly()); + CreateMap().ConvertUsing(source => source.HasValue ? source.Value.ToDateOnly() : default(DateOnly?)); +#endif } private void CreateMappingsForLocalDateTimeConverter() { CreateMap().ConvertUsing(source => LocalDateTime.FromDateTime(source)); - CreateMap().ConvertUsing(source => source.HasValue ? LocalDateTime.FromDateTime(source.Value) : default(LocalDateTime?)); CreateMap().ConvertUsing(source => source.ToDateTimeUnspecified()); - CreateMap().ConvertUsing(source => source.HasValue ? source.Value.ToDateTimeUnspecified() : default(DateTime?)); } private void CreateMappingsForLocalTimeConverter() { - CreateMap().ConvertUsing(source => LocalDateTime.FromDateTime(source).TimeOfDay); - CreateMap().ConvertUsing(source => source.HasValue ? LocalDateTime.FromDateTime(source.Value).TimeOfDay : default(LocalTime?)); - CreateMap().ConvertUsing(source => source.On(new LocalDate(1, 1, 1)).ToDateTimeUnspecified()); +#if NET6_0_OR_GREATER + CreateMap().ConvertUsing(source => LocalTime.FromTimeOnly(source)); + CreateMap().ConvertUsing(source => source.HasValue ? LocalTime.FromTimeOnly(source.Value) : default(LocalTime?)); + CreateMap().ConvertUsing(source => source.ToTimeOnly()); + CreateMap().ConvertUsing(source => source.HasValue ? source.Value.ToTimeOnly() : default(TimeOnly?)); +#endif CreateMap().ConvertUsing(source => new TimeSpan(source.TickOfDay)); - CreateMap() - .ConvertUsing(source => source.HasValue ? source.Value.On(new LocalDate(1, 1, 1)).ToDateTimeUnspecified() : default(DateTime?)); CreateMap().ConvertUsing(source => source.HasValue ? new TimeSpan(source.Value.TickOfDay) : default(TimeSpan?)); CreateMap().ConvertUsing(source => LocalTime.FromTicksSinceMidnight(source.Ticks)); CreateMap().ConvertUsing(source => source.HasValue ? LocalTime.FromTicksSinceMidnight(source.Value.Ticks) : default(LocalTime?)); diff --git a/test/Extensions.Tests/Mapping/LocalDateTests.cs b/test/Extensions.Tests/Mapping/LocalDateTests.cs index 9e3075a03..f8907e220 100644 --- a/test/Extensions.Tests/Mapping/LocalDateTests.cs +++ b/test/Extensions.Tests/Mapping/LocalDateTests.cs @@ -97,6 +97,12 @@ public override IEnumerable GetTypeConverters() yield return typeof(ITypeConverter); yield return typeof(ITypeConverter); yield return typeof(ITypeConverter); +#if NET6_0_OR_GREATER + yield return typeof(ITypeConverter); + yield return typeof(ITypeConverter); + yield return typeof(ITypeConverter); + yield return typeof(ITypeConverter); +#endif } } } diff --git a/test/Extensions.Tests/Mapping/LocalTimeTests.cs b/test/Extensions.Tests/Mapping/LocalTimeTests.cs index eacc16da8..88a762b0e 100644 --- a/test/Extensions.Tests/Mapping/LocalTimeTests.cs +++ b/test/Extensions.Tests/Mapping/LocalTimeTests.cs @@ -2,6 +2,7 @@ using AutoMapper; using FluentAssertions; using NodaTime; +using NodaTime.Extensions; using Xunit; using Xunit.Abstractions; @@ -43,34 +44,6 @@ public void MapsTo_DateTime() result.Should().Be(new LocalTime(502 / 60, 502 % 60)); } - [Fact] - public void MapsFrom_DateTimeOffset() - { - var mapper = Config.CreateMapper(); - - var foo = new Foo1 - { - Bar = LocalTime.FromTicksSinceMidnight(10000) - }; - - var result = mapper.Map(foo).Bar; - result.Should().Be(foo.Bar.On(new LocalDate(1, 1, 1)).ToDateTimeUnspecified()); - } - - [Fact] - public void MapsTo_DateTimeOffset() - { - var mapper = Config.CreateMapper(); - - var foo = new Foo5 - { - Bar = DateTime.Now - }; - - var result = mapper.Map(foo).Bar; - result.Should().Be(LocalDateTime.FromDateTime(foo.Bar).TimeOfDay); - } - public LocalTimeTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } @@ -105,7 +78,9 @@ protected override void Configure(IMapperConfigurationExpression expression) } expression.CreateMap().ReverseMap(); +#if NET6_0_OR_GREATER expression.CreateMap().ReverseMap(); +#endif } private class Foo1 @@ -118,10 +93,12 @@ private class Foo3 public TimeSpan Bar { get; set; } } +#if NET6_0_OR_GREATER private class Foo5 { - public DateTime Bar { get; set; } + public TimeOnly Bar { get; set; } } +#endif public class Converters : TypeConverterFactory { @@ -131,10 +108,42 @@ public override IEnumerable GetTypeConverters() yield return typeof(ITypeConverter); yield return typeof(ITypeConverter); yield return typeof(ITypeConverter); - yield return typeof(ITypeConverter); - yield return typeof(ITypeConverter); - yield return typeof(ITypeConverter); - yield return typeof(ITypeConverter); +#if NET6_0_OR_GREATER + yield return typeof(ITypeConverter); + yield return typeof(ITypeConverter); + yield return typeof(ITypeConverter); + yield return typeof(ITypeConverter); +#endif } } + +#if NET6_0_OR_GREATER + [Fact] + public void MapsFrom_DateTimeOffset() + { + var mapper = Config.CreateMapper(); + + var foo = new Foo1 + { + Bar = LocalTime.FromTicksSinceMidnight(10000) + }; + + var result = mapper.Map(foo).Bar; + result.Should().Be(foo.Bar.ToTimeOnly()); + } + + [Fact] + public void MapsTo_DateTimeOffset() + { + var mapper = Config.CreateMapper(); + + var foo = new Foo5 + { + Bar = TimeOnly.FromDateTime(DateTime.Now) + }; + + var result = mapper.Map(foo).Bar; + result.Should().Be(foo.Bar.ToLocalTime()); + } +#endif } diff --git a/test/Extensions.Tests/Mapping/TypeConverterTest.cs b/test/Extensions.Tests/Mapping/TypeConverterTest.cs index f8db0d167..903aa4422 100644 --- a/test/Extensions.Tests/Mapping/TypeConverterTest.cs +++ b/test/Extensions.Tests/Mapping/TypeConverterTest.cs @@ -124,6 +124,17 @@ private static object GetRandomValue(Type type) { return Faker.Date.RecentOffset(); } +#if NET6_0_OR_GREATER + if (type == typeof(DateOnly)) + { + return Faker.Date.RecentDateOnly(); + } + + if (type == typeof(TimeOnly)) + { + return Faker.Date.RecentTimeOnly(); + } +#endif throw new NotSupportedException($"type {type.FullName} is not supported"); }