DST / Timezone problem for Brasilia, Brazil

EricKinoshitaEricKinoshita BRMember ✭✭
edited December 2019 in General

I'm facing a weird issue related to Timezone / Daylight Savings Time. When I call DateTime.Now it brings the wrong time (one hour ahead of the current time).

(The Brazil's president determined this year that there will not be Daylight Savings Time anymore, and the law applied right away).

It looks like this happens only on Android. Some phone Models seems to be more affected than others (I have seen two devices with the same exact model, one having this issue and the other working correctly). I tried uninstalling/reinstalling the app, restarting the device, etc and nothing fixed the issue.

I made a simple app to check this problem. The device is configured with GMT-03:00 "Brasilia default time", but when I do DateTime.Now.ToString("o") the time is one hour ahead, and the timezone is -02:00 as if the DST was active (instead of -03:00).

I created a new solution, of "Android App" type. I changed the content_mai.xml layout file adding android:id="@+id/MyTextView" to the TextView. I changed the MainActivity.cs to include the following code:

        protected override void OnStart()
        {
            base.OnStart();
            ShowCurrentTime();
        }

        private void FabOnClick(object sender, EventArgs eventArgs)
        {
            View view = (View)sender;
            ShowCurrentTime();
        }

        void ShowCurrentTime()
        {
            var textView = FindViewById<TextView>(Resource.Id.MyTextView);
            textView.Text = DateTime.Now.ToString("o");
        }

When I run the app the wrong time is returned (see the screenshot above). The solution is attached below.

Does Xamarin/Mono maintain a TZ/DST table? Is this issue already known?

This issue started occurring some weeks ago (when the DST would have started), so I would appreciate this issue was given higher priority.

Posts

  • EricKinoshitaEricKinoshita BRMember ✭✭
    edited January 7

    I improved the test app displaying UTC date times, and also getting the date times using the Java classes. I modified the ShowCurrentTime to the following:

    void ShowCurrentTime()
    {
        var tz = System.TimeZone.CurrentTimeZone;
    
        var textView = FindViewById<TextView>(Resource.Id.MyTextView);
        textView.Text = String.Format("DateTime.Now: {0}\n", DateTime.Now.ToString("yyyy-MM-ddTHH:mmzzz")) +
            String.Format("System.TimeZone.CurrentTimezone.StandardName: {0}\n", tz.StandardName) +
            String.Format("System.TimeZone.CurrentTimezone.IsDaylightSavingsTime: {0}\n", tz.IsDaylightSavingTime(DateTime.Now)) +
            String.Format("DateTime.UtcNow: {0}\n", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mmzzz")) +
            String.Format("DateTimeOffset.Now: {0}\n", DateTimeOffset.Now.ToString("yyyy-MM-ddTHH:mmzzz")) +
            String.Format("DateTimeOffset.UtcNow: {0}\n", DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mmzzz"));
    
        // ===============
    
        Java.Text.DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmX");
        var textView2 = FindViewById<TextView>(Resource.Id.MyTextView2);
        textView2.Text = String.Format("Java.Util.Date: {0}\n", df.Format(new Date())) +
            String.Format("Java.Util.TimeZone.Default: {0}\n", Java.Util.TimeZone.Default.DisplayName) + 
            String.Format("Java.Util.TimeZone.Default.RawOffset: {0}\n", TimeSpan.FromMilliseconds(Java.Util.TimeZone.Default.RawOffset)) +
            String.Format("Java.Util.TimeZone.Default.InDaylightSavingsTime: {0}", Java.Util.TimeZone.Default.InDaylightTime(new Date()));
    }
    

    This is the result of the code above:

    The picture above shows that:

    • DateTime.Now is considering the Daylight savings time incorrectly (the time does not match the system time in the status bar on the top);
    • TimeZone.CurrentTimezone is considering the UTC offset for the Standard Time (which is correct);
    • TimeZone.CurrentTimezone.IsDaylightSavingTime (actual code is IsDaylightSavingTime(DateTime.Now)) is incorrect;
    • DateTime.UtcNow is correct;
    • DateTimeOffset.Now is incorrect (considering DST);
    • DateTimeOffset.UtcNow is correct;
    • Java's Date gives the correct time and correct UTC offset;
    • Java's TimeZone.Default is correct;
    • Java's TimeZone.Default.InDaylightSavingsTime (actual code is InDaylightTime(new Date())) is correct.

    So I conclude that Xamarin/Mono maintains a timezone table and it wasn't updated.

    My system setup details are in the attached file, if it is of any help.

Sign In or Register to comment.