Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

DateTimeOffset to DateTime throws Mapster.CompileException

Open TheObliterator opened this issue 4 years ago • 2 comments

Really useful package (thank you!), but I've encountered a small problem when mapping classes containing a DateTimeOffset. Mapster throws a CompileException ("Error while compiling source"). Not sure if I'm supposed to handle this mapping manually somehow? But seems like this could be handled automatically.

Repro example:

    public class ExampleSource
    {
        public DateTimeOffset DateOfBirth { get; set; }
    }

    public class ExampleTarget
    {
        public DateTime DateOfBirth { get; set; }
    }

    public static void Test()
    {
          try
          {
              ExampleSource source = new ExampleSource() { DateOfBirth = DateTimeOffset.UtcNow };
              ExampleTarget target = source.Adapt<ExampleTarget>();
              
              Console.WriteLine(target.DateOfBirth);
          }
          catch(Exception ex)
          {
              Console.WriteLine(ex.Message);      
          }
    }

Expected output: Today's date/time Actual output: "Error while compiling" (Mapster.CompileException)

TheObliterator avatar Jul 14 '21 17:07 TheObliterator

@TheObliterator I will take a look at this, but I also want to mention that you may want to look into the documentation on converting between DateTime and DateTimeOffset. Depending on your use case, you might lose information when performing the mapping you are describing above.

andrerav avatar Feb 19 '22 14:02 andrerav

Thanks for the additional information - interesting read.

TheObliterator avatar Feb 20 '22 00:02 TheObliterator