You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DateTimeOffset.Now throws exception on Android with Arabic language & western hemisphere timezone #94709
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:
Set locale on your Android smartphone to Arabic
Set timezone on your Android smartphone to some negative value (somewhere in western hemisphere, e.g. Phoenix, US)
Steps to reproduce:
Write a simple NET7/NET8 Android application where you'll call DateTimeOffset.Now at the very beginning (e.g. in your SplashActivity OnCreate method).
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)
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?
Reacted by mkpould, Evgen Babin and Taras Shevchuk
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
Activity
qwertylolman commentedon Nov 14, 2023
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?
Saratsin commentedon Nov 14, 2023
As for net7.0, I suppose that there should be created another PR for a different branch (not
mainbranch). 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 themainbranch with fix, but I suppose it will go then to net8.0 onlyakoeplinger commentedon Nov 15, 2023
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:This disables the fast path with the problematic code.