[mono-android] DateTime.Now broken?

Jonathan Pryor jpryor at novell.com
Wed Mar 30 16:40:04 EDT 2011


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



More information about the Monodroid mailing list