Skip to content

DateTimeOffset.Now throws exception on Android with Arabic language & western hemisphere timezone #94709

Closed

Description

@Saratsin

Description

There's an issue with Android implementation of DateTimeOffset.Now, that requires specific preconditions.
Issue happens in this file on line 63, where we call Convert.ToInt32(localDateTimeOffset) method without providing a specific IFormatProvider value.

Thus, it uses default culture provider, which is arabic. In arabic culture, negative numbers pattern has a minus sign at the end, i.e. number -2000 which is negative two thousands should be written as 2000- for arabic culture in order to be converted/parsed.

However, in monodroid.c file on line 256 it uses "%d" C syntax in order to store a negative integer, which is not culture-aware and always places negative sign at the beginning.

As a result, we're getting ThrowOverflowOrFormatException, if we will call DateTimeOffset.Now property at the beginning of life of our Android application.

Reproduction Steps

Preconditions:

  1. Set locale on your Android smartphone to Arabic
  2. Set timezone on your Android smartphone to some negative value (somewhere in western hemisphere, e.g. Phoenix, US)

Steps to reproduce:

  1. Write a simple NET7/NET8 Android application where you'll call DateTimeOffset.Now at the very beginning (e.g. in your SplashActivity OnCreate method).
  2. Run your application

Expected behavior

App will execute without crashes, DateTimeOffset.Now will return a proper result

Actual behavior

App crashes with ThrowOverflowOrFormatException

Regression?

As far as I know, Xamarin.Android didn't have such issue

Known Workarounds

Call _ = TimeZoneInfo.Local; in order to preload AndroidTZData
However, it will cause app slowdown in start which was the very point of making android-specific implementation of DateTimeOffset.Now property.

Fix actually should be very easy, on line 63 here, we have to change the code from
int localDateTimeOffsetSeconds = Convert.ToInt32(localDateTimeOffset);
to
int localDateTimeOffsetSeconds = Convert.ToInt32(localDateTimeOffset, CultureInfo.InvariantCulture);

Configuration

Versions of .NET: 7.0, 8.0
OS: Android
OS language: Arabic
OS Timezone: Phoenix, US (GMT-07:00)

Other information

No response

Activity

  1. ghost added
    untriaged
    on Nov 14, 2023
  2. ghost added
    in-pr
    on Nov 14, 2023
  3. qwertylolman commented on Nov 14, 2023

    @qwertylolman

    We got the same issue reported from Google, when we sent our app to store scans. Previous version was fine, this issue occured only after migration to net7 and Android api34.
    Could you please merge this fix asap and release the fix for net7?

  4. Saratsin commented on Nov 14, 2023

    @Saratsin
    ContributorAuthor

    As for net7.0, I suppose that there should be created another PR for a different branch (not main branch). Can somebody from Microsoft give a hint how what should I do in order to add these chagnes to net7.0 as well? I've already created a PR to the main branch with fix, but I suppose it will go then to net8.0 only

  5. removed
    untriaged
    on Nov 14, 2023
  6. added this to the 8.0.x milestone on Nov 14, 2023
  7. akoeplinger commented on Nov 15, 2023

    @akoeplinger
    Member

    Just FYI, the code in monodroid.c is mostly used for testing inside of this repo, apps built using .NET Android will use this code in xamarin-android which sets this key here: https://github.com/xamarin/xamarin-android/blob/1331aa215308b6c80e9348f80ccbcf57e639a27f/src/monodroid/jni/monovm-properties.hh#L35

    As a workaround you can add this code into your app, basically as one of the first things that runs very early on e.g. in CreateMauiApp:

    AppContext.SetData("System.TimeZoneInfo.LocalDateTimeOffset", null);

    This disables the fast path with the problematic code.

  8. ghost removed
    in-pr
    on Nov 15, 2023
  9. locked and limited conversation to collaborators on Dec 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions