Re: DateTime.Now is WRONG :)
David Logan <[email protected]>
| Newsgroups | gmane.comp.gnu.dotgnu.developer |
|---|---|
| Message-ID | <[email protected]> |
David Logan wrote: > Hey guys, anyone know how come the DateTime class Now property returns > the wrong time? > > David Logan > > [[email protected] temp]$ cscc -o tt.exe tt.cs > [[email protected] temp]$ ilrun tt.exe;date > 6/30/04 10:37:46 PM -5 > Wed Jun 30 21:37:47 MDT 2004 > [[email protected] temp]$ cat tt.cs > class Test > { > static void Main() > { > System.Console.WriteLine(DateTime.Now); > } > } > [[email protected] temp]$ > _______________________________________________ > Developers mailing list > [email protected] > http://dotgnu.org/mailman/listinfo/developers > Looking at the problem, it is in pnet/support/time.c in function ILGetTimeZoneAdjust. As shown below, it calls localtime(), and then modifies the returned timezone offset hours by one hour if the "isdst" variable is set. In my case, I my timezone is GMT-7. The returned timezone from the call is "GMT-6" and isdst=1. So this function modifies it by another hour and returns GMT-5. The documentation for localtime and the tm struct is unclear. It seems that isdst=1 means that the returned timezone has already been modified for daylight savings. Is this correct? Should I inquire on the Unix newsgroup? David Logan ILInt32 ILGetTimeZoneAdjust(void) { #if !defined(__palmos__) static int initialized = 0; static int isdst = 0; #ifdef HAVE_TM_GMTOFF static long timezone = 0; #endif if(!initialized) { /* Call "localtime", which will set the global "timezone" for us */ time_t temp = time(0); struct tm *tms = localtime(&temp); isdst = tms->tm_isdst; #ifdef HAVE_TM_GMTOFF timezone = -(tms->tm_gmtoff); #endif initialized = 1; } return (ILInt32)(timezone - (isdst ? 3600 : 0)); #else /* TODO */ return 0; #endif }