xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.46k stars 511 forks source link

DateTime to NSDate conversion issue with new DateTime instance in iOS platform #6993

Closed KarthikRajaAKR closed 1 year ago

KarthikRajaAKR commented 5 years ago

Description

I'm working in custom Date and TimePicker using UIDatePicker in native iOS renderer file. I tried to convert DateTime value to NSDate using methods attached below. Everything working fine if I set any DateTime value but the conversion not working as expected while DateTime field just initialized or initialized with the date(1/1/0001). In this case, converted date shows (1/3/0001) and add some time also.

NSDate converter

     public NSDate ToNsDate(DateTime dateTime)
     {
        if (dateTime.Kind == DateTimeKind.Unspecified)
        {
            dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Local);
        }
        return (NSDate)dateTime;
      }

DateTime converter

       public DateTime ToDateTime(NSDate date)
        {
               NSCalendar calendar = new NSCalendar(NSCalendarType.Gregorian) { TimeZone = NSTimeZone.FromGMT(NSTimeZone.LocalTimeZone.GetSecondsFromGMT) };
               NSDateComponents components = calendar.Components(NSCalendarUnit.Year | NSCalendarUnit.Month | NSCalendarUnit.Day | NSCalendarUnit.Hour | NSCalendarUnit.Minute | NSCalendarUnit.Second | NSCalendarUnit.Calendar, date);

                return new DateTime((int)components.Year, (int)components.Month, (int)components.Day, (int)components.Hour, (int)components.Minute, (int)components.Second);
        }

Steps to Reproduce

  1. Place breakpoint in iOS renderer file OnElementChanged
  2. Run the sample
  3. Note DateTime initialized with date(1/1/1). After converted to NSDate it has value(1/3/1).

Expected Behavior

Converted value should be same

Actual Behavior

Converted values are not same while DateTime field just initialized

Basic Information

Screenshots

image

Note: Initial DateTime value, Converted NsDate value and Converted DateTime value

Reproduction Link

PickerRendererNSDate.zip

Which is working fine for all other dates than 01/01/0001.

whitneyschmidt commented 5 years ago

1@KarthikRajaAKR Thank you for surfacing this issue! We were able to run your solution and reproduce it :)

We are planning to finish work on this issue, but our team is prioritizing Xcode 11 binding work.

The workaround we came up with is changing the way that we convert from DateTime to NSDate, here's the code snippet:

            var dt2 = dt.ToUniversalTime ();

            var calendar = new NSCalendar(NSCalendarType.Gregorian);
            calendar.TimeZone = NSTimeZone.FromName("UTC");
            var components = new NSDateComponents { Day = dt2.Day, Month = dt2.Month, Year = dt2.Year, Hour = dt2.Hour,
                Minute = dt2.Minute, Second = dt2.Second, Nanosecond = dt2.Millisecond * 1000000 };
            return calendar.DateFromComponents(components);
onurhazar commented 3 years ago

Any progress? do we still need to use "workaround"?

rolfbjarne commented 3 years ago

@onurhazar yes, you still need to use the workaround, this hasn't been fixed yet.