Bug 20172 - System.TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now) returns wrong value.
Summary: System.TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now) returns wrong value.
Status: VERIFIED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.12.4
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 4.16.0
Assignee: Marek Habersack
URL:
Depends on:
Blocks:
 
Reported: 2014-05-29 17:05 UTC by Jon Goldberger [MSFT]
Modified: 2014-08-25 10:45 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test Project. (17.07 KB, application/zip)
2014-05-29 17:05 UTC, Jon Goldberger [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Comment 1 Jon Goldberger [MSFT] 2014-05-29 17:11:50 UTC
System.TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now) in Xamarin.Android returns the negative value of the correct value. This does not occur in a console app built against Mono.

To reporduce:

1. Open the attached test project.
2. Set the Console app, TestTimeZoneOffset, as the startup app and run it.
3. In my case, Hawaiian time zone, the value displayed in the console is -10:00:00 which is correct.
4. Set the Android app, TestTimeZoneOffset.Droid, as the startup project and run it.
5. The value displayed in the console for TimeZone is 10:00:00, IOW the negative value: -(-10:00:00)

My version information:

=== Xamarin Studio ===

Version 5.0 (build 878)
Installation UUID: 2dc9022f-f9a8-424f-8284-bf224cbbfde0
Runtime:
	Mono 3.4.0 ((no/c3fc3ba)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 304000204

=== Xamarin.Android ===

Version: 4.12.4 (Business Edition)
Android SDK: /Users/apple/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		3.2   (API level 13)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Apple Developer Tools ===

Xcode 5.1.1 (5085)
Build 5B1008

=== Xamarin.iOS ===

Version: 7.2.3.39 (Business Edition)
Hash: fc6f56b
Branch: 
Build date: 2014-05-19 19:10:29-0400

=== Xamarin.Mac ===

Xamarin.Mac: 1.8.0.8

=== Build Information ===

Release ID: 500000878
Git revision: bcd66920d963483e7d638a2339c4022fe035b529
Build date: 2014-05-27 17:36:26-04
Xamarin addins: da9064ce55b0fa90930a7c437a4cc1ae0e5c778c

=== Operating System ===

Mac OS X 10.9.3
Darwin Jonathans-MacBook-Pro.local 13.2.0 Darwin Kernel Version 13.2.0
    Thu Apr 17 23:03:13 PDT 2014
    root:xnu-2422.100.13~1/RELEASE_X86_64 x86_64
Comment 3 Udham Singh 2014-05-30 08:13:48 UTC
I have tried to reproduce this issue with Test Project attached in bug description. I have followed the steps mentioned in comment 1 and observed that we are getting same value in both cases Console App as well as Android App.

Screencast: http://screencast.com/t/GVghxZCzv0

Note : If I check this on android device or emulator which have different time zone from the system time zone, then in that case we could have possibility to get different time zone value(negative).

Please let me know if I need to check something else.

Environment Info:

=== Xamarin Studio ===

Version 5.0 (build 878)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
Runtime:
	Mono 3.4.0 ((no/51469e6)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 304000202

=== Apple Developer Tools ===

Xcode 5.1 (5084)
Build 5B130a

=== Xamarin.iOS ===

Version: 7.2.3.39 (Business Edition)
Hash: fc6f56b
Branch: 
Build date: 2014-05-19 19:10:29-0400

=== Xamarin.Android ===

Version: 4.12.4 (Business Edition)
Android SDK: /Users/MM/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		3.2   (API level 13)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Xamarin.Mac ===

Xamarin.Mac: Not Installed

=== Build Information ===

Release ID: 500000878
Git revision: bcd66920d963483e7d638a2339c4022fe035b529
Build date: 2014-05-27 17:36:26-04
Xamarin addins: da9064ce55b0fa90930a7c437a4cc1ae0e5c778c

=== Operating System ===

Mac OS X 10.9.2
Darwin MacMini.local 13.1.0 Darwin Kernel Version 13.1.0
    Thu Jan 16 19:40:37 PST 2014
    root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64
Comment 4 Jon Goldberger [MSFT] 2014-05-30 13:54:01 UTC
this may be an Samsung bug? Originally tested on a Samsung Device, Galaxy Tab 2, running Android v. 4.2.2 (Jelly Bean). 

I just tested on an emulator running Android v 4.1.1 (Jelly Bean) and the results were indeed the same, -10:00 in both the console app and the Android app.

I also just tested on an emulator running v 4.2.2 and again the returned time zone offset was the same in the console app and the Android app.
Comment 5 Jon Goldberger [MSFT] 2014-05-30 14:03:56 UTC
Clarification: testing on an emulator running Android v 4.1.1 or v 4.2.2 did _not_ demonstrate the time zone offset error.
Comment 6 floriang 2014-06-02 00:18:31 UTC
I have this problem on a Freescale SABRE board, running Android 4.0.4
Comment 7 floriang 2014-06-02 00:33:32 UTC
Additionally, here are my repro-steps:

(Repro-steps)
1. Set system timezone to GMT, and date time to 9:00.
2. Set the timezone (GMT+2) using Java API:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE)
Context.am.setTimeZone(TimeZone.getTimeZone("GMT+0200").getDisplayName());
3. After setting the timezone, force refresh cached data via System.TimeZoneInfo.ClearCachedData();                  System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();

(Result)
1. System time is 11:00
2. In Xamarin.Android application, when using "System.TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)", we get -02:00:00
(instead of +02:00:00). Accordingly, Xamarin.Android doesn't provide correct time.

(Expected)
Both system time and application time are the same: 11:00
"System.TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)" should return +02:00:00
Comment 8 Marek Habersack 2014-06-02 05:46:40 UTC
Investigating
Comment 9 Marek Habersack 2014-06-02 07:31:17 UTC
I've been unable to repro this bug on Android 4.4.2 (Nexus 10, stock Android as well as CM11 on Nexus 5). It also worked fine in Genymotion and the following Android versions:

 2.3.7
 4.1.1
 4.4.2
 
I'm currently trying to reimage my old SG2 with the stock Samsung ROM to see if it can be a problem with their software. It may unfortunately take a while...
Comment 10 Marek Habersack 2014-06-03 06:23:23 UTC
I managed to bring the SG2 to life, tried with stock Android v4.1.2 to no avail - no error there.

@Jon - can we get the exact name and model of the device it failed on, together with kernel version, android version (and vendor)? Maybe then we'll be able to repro it somehow

@floriang - is there a way to obtain an emulator image for the board you mentioned?

All of my tests were performed with the current master of Xamarin.Android.
Comment 11 Jon Goldberger [MSFT] 2014-06-03 13:07:45 UTC
Here's the info from /system/build.prop (adb shell cat /system/build.prop) for my Samsung Galaxy Tab 2:

# begin build properties
# autogenerated by buildinfo.sh
ro.build.id=JDQ39
ro.build.display.id=JDQ39.P3113UEUCMK3
ro.build.version.incremental=P3113UEUCMK3
ro.build.version.sdk=17
ro.build.version.codename=REL
ro.build.version.release=4.2.2
ro.build.date=Tue Nov 19 18:40:48 KST 2013
ro.build.date.utc=1384854048
ro.build.type=user
ro.build.user=se.infra
ro.build.host=R0301-09
ro.build.tags=release-keys
ro.product.model=GT-P3113
ro.product.brand=samsung
ro.product.name=espressowifiue
ro.product.device=espressowifi
ro.product.board=piranha
ro.product.cpu.abi=armeabi-v7a
ro.product.cpu.abi2=armeabi
ro.product.manufacturer=samsung
ro.product.locale.language=en
ro.product.locale.region=GB
ro.wifi.channels=
ro.board.platform=omap4
# ro.build.product is obsolete; use ro.product.device
ro.build.product=espressowifi
# Do not try to parse ro.build.description or .fingerprint
ro.build.description=espressowifiue-user 4.2.2 JDQ39 P3113UEUCMK3 release-keys
ro.build.fingerprint=samsung/espressowifiue/espressowifi:4.2.2/JDQ39/P3113UEUCMK3:user/release-keys
ro.build.characteristics=tablet tablet
# Samsung Specific Properties
ro.build.PDA=P3113UEUCMK3
ro.build.hidden_ver=P3113UEUCMK3
ro.build.changelist=1978026
ro.product_ship=true
ro.chipname=piranha
# end build properties
#
# system.prop for maguro
#
# 131072 is decimal for 0x20000 to report version 2
ro.opengles.version=131072
rild.libpath=/system/lib/libsec-ril.so
rild.libargs=-d /dev/ttys0
telephony.lteOnCdmaDevice=0

persist.sys.storage_preload=1

# System property for Default Brightness
ro.lcd_brightness=140
ro.sf.lcd_density=160

#
# System property for qemu
#
ro.kernel.qemu=0

#rotation setting in potrait mode.
ro.sf.hwrotation=270


ro.carrier=wifi-only
media.enable-commonsource=true
net.streaming.rtsp.uaprof=http://wap.samsungmobile.com/uaprof/

com.ti.omap_enhancement=true
com.ti.omap_enhancement_s3d=true

#
# ADDITIONAL_BUILD_PROPERTIES
#
ro.error.receiver.default=com.samsung.receiver.error
ro.url.legal=http://www.google.com/intl/%s/mobile/android/basic/phone-legal.html
ro.url.legal.android_privacy=http://www.google.com/intl/%s/mobile/android/basic/privacy.html
wifi.interface=wlan0
ro.sf.lcd_density=320
ro.opengles.version=131072
dalvik.vm.heapstartsize=5m
dalvik.vm.heapgrowthlimit=48m
dalvik.vm.heapsize=256m
dalvik.vm.heaptargetutilization=0.75
dalvik.vm.heapminfree=512k
dalvik.vm.heapmaxfree=2m
ro.config.ringtone=S_Over_the_horizon.ogg
ro.config.notification_sound=S_Whistle.ogg
ro.config.alarm_alert=Good_Morning.ogg
ro.config.media_sound=Media_preview_Touch_the_light.ogg
ro.sec.fle.encryption=true
ro.hdcp2.rx=tz
keyguard.no_require_sim=true
ro.com.android.dateformat=MM-dd-yyyy
ro.carrier=unknown
ro.crypto.support=recovery_mount|others
ro.com.google.clientidbase=android-samsung
ro.ril.hsxpa=1
ro.ril.gprsclass=10
ro.adb.qemud=1
ro.com.google.gmsversion=4.2_r3
dalvik.vm.dexopt-flags=m=y
net.bt.name=Android
dalvik.vm.stack-trace-file=/data/anr/traces.txt
Comment 13 Marek Habersack 2014-06-04 05:45:51 UTC
I've managed to repro the issue using steps outlined in comment 7 in emulator, running 4.2.2.

I'll get back to this bug as soon as possible.
Comment 14 Marek Habersack 2014-06-09 08:05:37 UTC
@floriang - I created a test case which uses your steps from comment 7 and, I think, the problem is in the Java APIs. Look at the logcat output when setting the time using the code you provided:

I/mono-stdout( 2882): ******** Resetting timezone to GMT+2 using Java APIs
D/AlarmManagerService(  448): Kernel timezone updated to -120 minutes west of GMT
I/mono-stdout( 2882): **** [MFA] Current time: 6/9/2014 1:46:29 PM
I/mono-stdout( 2882): **** [MFA] TimeZone: 00:19:32
I/mono-stdout( 2882): **** [Java] Current time: Jun 9, 2014 1:46:29 PM
I/mono-stdout( 2882): **** [Java] TimeZone: 2 (7200000ms)

Note how confused Mono runtime becomes when reporting the timezone (the offset comes from the Android's libc) and also note that *Java* is setting the timezone to -120 minutes west, while the correct GMT+2 interpretation is +120 minutes *east* for my current tz which is CEST. Then, farther down the log (the exception happens only if the timezone is first set to one of the available values using Settings -> Date Time -> Select time zone):

E/joda-time-android( 2956): Could not recognize timezone id "GMT+02:00"
E/joda-time-android( 2956): java.lang.IllegalArgumentException: The datetime zone id 'GMT+02:00' is not recognised
E/joda-time-android( 2956): 	at org.joda.time.DateTimeZone.forID(DateTimeZone.java:226)
E/joda-time-android( 2956): 	at net.danlew.android.joda.TimeZoneChangedReceiver.onReceive(TimeZoneChangedReceiver.java:20)
E/joda-time-android( 2956): 	at android.app.ActivityThread.handleReceiver(ActivityThread.java:2419)
E/joda-time-android( 2956): 	at android.app.ActivityThread.access$1700(ActivityThread.java:135)
E/joda-time-android( 2956): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
E/joda-time-android( 2956): 	at android.os.Handler.dispatchMessage(Handler.java:102)
E/joda-time-android( 2956): 	at android.os.Looper.loop(Looper.java:136)
E/joda-time-android( 2956): 	at android.app.ActivityThread.main(ActivityThread.java:5017)
E/joda-time-android( 2956): 	at java.lang.reflect.Method.invokeNative(Native Method)
E/joda-time-android( 2956): 	at java.lang.reflect.Method.invoke(Method.java:515)
E/joda-time-android( 2956): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/joda-time-android( 2956): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/joda-time-android( 2956): 	at dalvik.system.NativeStart.main(Native Method)

However, when I re-run the test case I get his output:

I/mono-stdout( 3109): **** [MFA] Current time: 6/9/2014 9:52:01 AM
I/mono-stdout( 3109): **** [MFA] TimeZone: -02:00:00
I/mono-stdout( 3109): **** [Java] Current time: Jun 9, 2014 1:52:01 PM
I/mono-stdout( 3109): **** [Java] TimeZone: 2 (7200000ms)

I/mono-stdout( 3109): ******** Resetting timezone to GMT+2 using Java APIs
D/AlarmManagerService(  448): Kernel timezone updated to -120 minutes west of GMT
I/mono-stdout( 3109): **** [MFA] Current time: 6/9/2014 9:52:27 AM
I/mono-stdout( 3109): **** [MFA] TimeZone: -02:00:00
I/mono-stdout( 3109): **** [Java] Current time: Jun 9, 2014 1:52:27 PM
I/mono-stdout( 3109): **** [Java] TimeZone: 2 (7200000ms)

So the Java APIs do something weird with the tz data and they most probably don't use the bionic (libc) tz APIs to retrieve the information, unlike Mono. At the same time, the time reported by Android in the device's top bar is correct... And, once again, the tz offset Mono reports is obtained from the libc/kernel. At this point I'm yet still unsure what/where is the bug and I'll keep poking around.

For reference, here's the code of the test case:

[Activity(Label = "TestTimeZone.Droid", MainLauncher = true)]
	public class MainActivity : Activity
	{
		int count = 1;

		protected override void OnCreate(Bundle bundle)
		{
			base.OnCreate(bundle);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			PrintTZ ();

			// Get our button from the layout resource,
			// and attach an event to it
			Button button = FindViewById<Button>(Resource.Id.myButton);
			
			button.Click += delegate
			{
				Console.WriteLine ("******** Resetting timezone to GMT+2 using Java APIs");
				var am = GetSystemService (Context.AlarmService) as AlarmManager;
				am.SetTimeZone (Java.Util.TimeZone.GetTimeZone ("GMT+0200").DisplayName);
				ClearTzCaches ();
				PrintTZ ();

				button.Text = string.Format("{0} clicks!", count++);
			};
		}

		void ClearTzCaches ()
		{
			TimeZoneInfo.ClearCachedData ();
			CultureInfo.CurrentCulture.ClearCachedData ();
		}

		void PrintTZ ()
		{
			Console.WriteLine ("**** [MFA] Current time: {0}", DateTime.Now);
			Console.WriteLine ("**** [MFA] TimeZone: {0}", TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now));

			var jd = new Java.Util.Date ();
			Console.WriteLine ("**** [Java] Current time: {0}", jd.ToLocaleString ());
			Console.WriteLine ("**** [Java] TimeZone: {0} ({1}ms)", Java.Util.TimeZone.Default.RawOffset / 1000 / 3600, Java.Util.TimeZone.Default.RawOffset);
		}
	}
Comment 15 Marek Habersack 2014-07-29 15:38:19 UTC
This is now fixed in the master branch.
Comment 16 Saurabh 2014-08-06 09:24:24 UTC
As per comment#3 we were not able to reproduce this issue on both Emulator and device. So We are not sure exact steps to check this Issue. Could you please provide some steps so that we can check this issue?
Comment 17 PJ 2014-08-21 17:00:18 UTC
Saurabh, please review the test case in https://bugzilla.xamarin.com/show_bug.cgi?id=20172#c14.
Comment 18 Udham Singh 2014-08-25 10:45:36 UTC
I have verified this issue by following the instructions provided in comment 7 and comment 14, now this is working fine, Hence closing this issue.

Screencast : http://screencast.com/t/4aQuOV8pf

Environment Info : 

=== Xamarin Studio ===

Version 5.3 (build 434)
Installation UUID: 449f40dd-b3f1-4028-9a6b-cca0d1a2307d
Runtime:
	Mono 3.8.0 ((no/62a857e)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 308000007

=== Apple Developer Tools ===

Xcode 5.1.1 (5085)
Build 5B1008

=== Xamarin.Mac ===

Version: 1.10.0.9 (Starter Edition)

=== Xamarin.Android ===

Version: 4.16.0 (Trial Edition)
Android SDK: /Users/360logicaxamarinmacmini/Desktop/android-sdk-macosx_AndÃsdk
	Supported Android versions:
		1.6    (API level 4)
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		3.2    (API level 13)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		4.5    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)


=== Build Information ===

Release ID: 503000434
Git revision: 9b508e2173c6a211fa020d9743a86b484b5ed0d2
Build date: 2014-08-21 12:41:28-04
Xamarin addins: 69708b283b171ea053d0edfbf09d98457d21fbee

=== Operating System ===

Mac OS X 10.8.4
Darwin 360Logicas-Mac-mini.local 12.4.0 Darwin Kernel Version 12.4.0
    Sun Mar 10 18:01:10 PDT 2013
    root:xnu-2050.24.6~1/RELEASE_X86_64 x86_64