Skip to content

DateTime config not used #796

Closed
Closed

Description

@lucacivale

Hi,
today I faced a weird issue.
Created configs for DateTime/DateTimeOffset/DateOnly mappins aren't used. This example should work or do I miss something?

public class Foo
{
    public DateTimeOffset DateTimeOffset { get; set; }

    public DateOnly Date { get; set; }

    public DateTime DateTime { get; set; }
}
TypeAdapterConfig<DateTimeOffset, DateOnly>.NewConfig()
    .MapWith(s => DateOnly.FromDateTime(s.UtcDateTime.Date))
    .AfterMapping(s =>
    {
        Console.WriteLine("From DateTimeOffset to DateOnly");
    });

TypeAdapterConfig<DateOnly, DateTimeOffset>.NewConfig()
    .MapWith(s => new DateTimeOffset(s.ToDateTime(TimeOnly.FromTimeSpan(TimeSpan.Zero)), TimeSpan.Zero))
    .AfterMapping(s =>
    {
        Console.WriteLine("From DateOnly to DateTimeOffset");
    });

TypeAdapterConfig<DateTimeOffset, DateTime>.NewConfig()
    .MapWith(s => s.UtcDateTime)
    .AfterMapping(s =>
    {
        Console.WriteLine("From DateTimeOffset to DateTime");
    });

TypeAdapterConfig<DateTime, DateTimeOffset>.NewConfig()
    .MapWith(s => new DateTimeOffset(s, TimeSpan.Zero))
    .AfterMapping(s =>
    {
        Console.WriteLine("From DateTime to DateTimeOffset");
    });
var foo = new Foo();
foo.DateTimeOffset = DateTimeOffset.UtcNow;
foo.DateTime = DateTime.UtcNow;

Console.WriteLine(foo.DateTimeOffset.Adapt<DateTime>());
Console.WriteLine(foo.DateTime.Adapt<DateTimeOffset>());

Activity

  1. stagep commented on Apr 29, 2025

    @stagep

    The output with your code:

    4/29/2025 4:31:20 PM
    4/29/2025 4:31:20 PM +00:00
    

    I added the following to every config

    .AddDays(1)
    

    so now we have

    TypeAdapterConfig<DateTimeOffset, DateOnly>.NewConfig()
        .MapWith(s => DateOnly.FromDateTime(s.UtcDateTime.Date.AddDays(1)))
        .AfterMapping(s =>
        {
            Console.WriteLine("From DateTimeOffset to DateOnly");
        });
    
    TypeAdapterConfig<DateOnly, DateTimeOffset>.NewConfig()
        .MapWith(s => new DateTimeOffset(s.ToDateTime(TimeOnly.FromTimeSpan(TimeSpan.Zero)), TimeSpan.Zero).AddDays(1))
        .AfterMapping(s =>
        {
            Console.WriteLine("From DateOnly to DateTimeOffset");
        });
    
    TypeAdapterConfig<DateTimeOffset, DateTime>.NewConfig()
        .MapWith(s => s.UtcDateTime.AddDays(1))
        .AfterMapping(s =>
        {
            Console.WriteLine("From DateTimeOffset to DateTime");
        });
    
    TypeAdapterConfig<DateTime, DateTimeOffset>.NewConfig()
        .MapWith(s => new DateTimeOffset(s, TimeSpan.Zero).AddDays(1))
        .AfterMapping(s =>
        {
            Console.WriteLine("From DateTime to DateTimeOffset");
        });
    

    The output with this modified code is:

    4/30/2025 4:33:16 PM
    4/30/2025 4:33:16 PM +00:00
    

    which shows the Adapt is working.

    What outputs are you expecting for each of your conversions? If you apply your conversion directly without using Mapster, do you get different results?

    Using your 2 output examples and applying the conversion directly:

    Console.WriteLine(DateTimeOffset.UtcNow.UtcDateTime);
    Console.WriteLine(new DateTimeOffset(DateTime.UtcNow, TimeSpan.Zero));
    

    The output is

    4/29/2025 4:45:41 PM
    4/29/2025 4:45:41 PM +00:00
    

    The same output (except the time has changed of course)

  2. lucacivale commented on Apr 30, 2025

    @lucacivale
    Author

    @stagep sorry, seems like it was too early for me yesterday.
    You are totally right the mapping is working properly.

    Initially I wanted to do something in AfterMapping but it's never invoked so I thought the mapping isn't used entirely. Should the AfterMapping work is this working as designed?

  3. DocSvartz commented on Apr 30, 2025

    @DocSvartz
    Contributor

    @stagep @lucacivale

    What version of Mapster are you using?

    In 7.4.0 MapWith should not work with DateTime (Mapster primitive).

    MapWith by default does not merge settings with AfterMapping

    This is activated separately
    .MapWith(s => DateOnly.FromDateTime(s.UtcDateTime.Date), applySettings: true)

    Warning:
    In pre-releases
    mapping to DateTime using MapWith and applySettings: true

    Leads to an infinite loop !!!
    ( Update: it fixed in new PR)

  4. lucacivale commented on Apr 30, 2025

    @lucacivale
    Author

    @DocSvartz I'm using 7.4.0 and it's working without applySettigns:True except the AfterMapping

  5. DocSvartz commented on Apr 30, 2025

    @DocSvartz
    Contributor

    @lucacivale I'm probably already confusing something :). I dealt with this issue almost 2 years ago.

    applySettigns:True activate AfterMapping in 7.4.0 ?

  6. lucacivale commented on May 2, 2025

    @lucacivale
    Author

    Yes it works with applySettigns:True
    @DocSvartz @stagep Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions