[mono-android] DateTime.Now broken?

Nikolai Sander nikolai at eodsoft.com
Wed Mar 30 16:55:58 EDT 2011


It's a Nook Color official development device running Android 2.1.

I just updated to Android 2.2 and will let you know what happens. 

This workaround worked for me:

            DateTime nowutc = DateTime.UtcNow;
            DateTime now = TimeZoneInfo.ConvertTime(nowutc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id));

Nikolai

On Mar 30, 2011, at 1:40 PM, Jonathan Pryor wrote:

> On Mar 29, 2011, at 11:48 PM, Nikolai Sander wrote:
>> When I get DateTime.Now Kind is System.DateTimeKind.Local but the hour is -7 hours off, which means it really is in UTC (device is set to PST).
> 
> What device is this?
> 
> This could be: https://bugzilla.novell.com/show_bug.cgi?id=639599
> 
> That said, DateTime.Now and DateTime.UtcNow behave as expected on my N1, while your `nowutc` and `now` values do not:
> 
> 	[Activity (Label = "Scratch", MainLauncher = true)]
> 	public class Activity1 : Activity
> 	{
> 		protected override void OnCreate (Bundle bundle)
> 		{
> 			base.OnCreate (bundle);
> 
> 			DateTime nowutc = new DateTime(DateTime.Now.Ticks, DateTimeKind.Utc);
> 			DateTime now = TimeZoneInfo.ConvertTimeFromUtc(nowutc, TimeZoneInfo.Local);
> 			
> 			TextView tv = new TextView (this) {
> 				Text = string.Format ("Now={0};\nUtcNow={1};\nnowutc={2};\nnow={3}",
> 					DateTime.Now, DateTime.UtcNow, nowutc, now),
> 			};
> 			
> 			SetContentView (tv);
> 		}
> 	}
> 
> results in output:
> 
> 	Now=3/30/2011 4:33:18 PM;
> 	UtcNow=3/30/2011 8:33:18 PM;
> 	nowutc=3/30/2011 4:33:17 PM;
> 	now=3/30/2011 12:33:17 PM
> 
> Meanwhile, on an Android v1.6 emulator, it's all horribly broken:
> 
> 	Now=3/30/2011 8:32:53 PM;
> 	UtcNow=3/30/2011 8:32:53 PM;
> 	nowutc=3/30/2011 8:32:51 PM;
> 	now=3/30/2011 8:32:51 PM
> 
> As per the but report, it appears to work properly in an Android v2.2 emulator.
> 
> Thus, what kind of device are you using? Based on current knowledge, Android v1.6 has a dodgy gettimeofday(2) which results in wrong values, but anything recent (2.2, 2.3.3) has proper behavior.
> 
> - Jon
> 
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com
> 
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid



More information about the Monodroid mailing list