[Mono-devel-list] patch for DateTime.Now

Atsushi Eno atsushi at ximian.com
Tue May 25 13:13:37 EDT 2004


Hello,

I noticed that DateTime.Now returns incorrect time. For example am
getting +9h future time. I live in JST timezone (+09:00)

using System;
using System.Threading;
using System.Globalization;

public class Test
{
  public static void Main ()
  {
    DateTime dt = DateTime.Now;
    Console.WriteLine ("Day is {0}, hour is {1}. Tick is {2}",
      dt.Day, dt.Hour, dt.Ticks);
    Thread.CurrentThread.CurrentCulture =
      CultureInfo.InvariantCulture;
    dt = DateTime.Now;
    Console.WriteLine ("Day is {0}, hour is {1}. Tick is {2}",
      dt.Day, dt.Hour, dt.Ticks);
  }
}

$ mono now.exe;./now.exe
Day is 26, hour is 11. Tick is 632211664737140000
Day is 26, hour is 11. Tick is 632211664738450000
Day is 26, hour is 2. Tick is 632211340739552400
Day is 26, hour is 2. Tick is 632211340739552400

I created a patch that changes GetNow() behavior from GetLocalTime()
to GetSystemTime()

$ mono now.exe;./now.exe
Day is 26, hour is 2. Tick is 632211343513640000
Day is 26, hour is 2. Tick is 632211343515340000
Day is 26, hour is 2. Tick is 632211343516545520
Day is 26, hour is 2. Tick is 632211343516545520

...but not sure if it is the right way to fix.
At least current implementation is incorrect (I could understand if it
is -9h from my Japanese time ;-).

Atsushi Eno
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: win-getnow.diff
Url: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20040526/d244ccbd/attachment.pl 


More information about the Mono-devel-list mailing list