Bug 3520 - DateTime.Now returns UTC time instead of local time
Summary: DateTime.Now returns UTC time instead of local time
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.0
Hardware: PC Windows
: --- normal
Target Milestone: 4.2
Assignee: Bugzilla
: 4227 ()
Depends on:
Reported: 2012-02-17 12:13 UTC by Craig
Modified: 2012-04-18 05:10 UTC (History)
5 users (show)

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

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:

Description Craig 2012-02-17 12:13:19 UTC
Whenever I use DateTime.Now it always returns UTC time, which is not the timezone I live in.  This also does NOT match the timezone on the emulator I'm using.

I live in CST timezone and the emulator accurately reflects that with the correct time in the upper-right corner.  For example, the current time in CST timezone is 11:02 AM.  But calling DateTime.Now returns 5:02 PM.

To make things more interesting new Java.Util.Date() returns the correct time.  So I don't think this can be blamed on the device or emulator since the correct time is being returned by the Java version - it seems to be a bug in mono.

Here is actual output and results:

Log.Info("TEST", TimeZone.CurrentTimeZone.StandardName); // outputs CST
Log.Info("TEST", DateTime.Now.Hour.ToString());          // outputs 17
Log.Info("TEST", new Java.Util.Date().Hours.ToString()); // outputs 11

My emulator version and target platform is Android 2.1.  I have no clue if this bug exists in other versions, but it should work in 2.1 regardless.
Comment 1 Atsushi Eno 2012-03-29 05:16:45 UTC
I could reproduce this only on an API Level 7 emulator. I tried the same logging on my ASUS Transformer (which runs Android 3.2) but I couldn't get that wrong result. DateTime.Now also showed local time. I also tried API Level 10 emulator, and it also worked fine.

From these results, it seems this is rather a bug in Android's underlying bionic libc on which our System.DateTime implementation depends. If that is the case, unfortunately there is no practical way to determine the actual local date in System.DateTime.
Comment 2 Atsushi Eno 2012-04-18 05:10:56 UTC
*** Bug 4227 has been marked as a duplicate of this bug. ***