I posted this about a week ago but it seems to have got lost. Maybe it
didn't measure up to the impeccably high standards prevailing here :-(

New Zealand Daylight Saving started last Sunday (October 7) and revealed the
following problems in Beta 2 date and time zone handling under Win2K with
the Automatically adjust for daylight saving time flag CHECKED:

(A) Timezone.CurrentTimeZone.IsDayLightSavingTime always returns false.
(tested for all dates between 1995 and the current time.)

(B) FileInfo.LastWriteTime and File.GetLastWriteTime return different times
for the same file.

I have included a short VB.NET console app that demonstrates some of the
affects of these problems at the end of this post. If you want to use the
program compile it under visual studio and copy the resulting .EXE to your
SendTo directory. You have to send a file to the program for it to check
file times.

Here is the output from the program run earlier this morning on Win2k with
the Automatically Adjust for Daylight Savings Time box CHECKED in Date/Time
properties as accessed from the toolbar:

1 Microsoft Windows NT 5.0.2195.0 :: 11/10/2001 9:29:10 a.m.
2 New Zealand Daylight Time starts :: 7/10/2001 2:00:00 a.m.
3 New Zealand Daylight Time ends :: 18/03/2001 2:00:00 a.m.
4 TimeZone.IsDayLightSavingTime(Now()) returns False.
5 Universal Time from SystemTime :: 10/10/2001 8:29:10 p.m.
6 Universal Time from Now().ToUniversalTime :: 10/10/2001 9:29:10 p.m.

7 C:\aVB7\FTest.vb
8 File.GetLastWriteTime :: 10/01/2001 5:45:51 a.m.
9 FileInfo.LastWriteTime :: 10/01/2001 5:51:36 a.m.
10 Using FindFirstFile, FileTimeToLocalFileTime, etc :: 10/01/2001 6:51:36
a.m.

Line 4 demonstrates Problem (A) which probably occurs because the programmer
of IsDayLightSavingTime() never learnt about the southern hemisphere at
school :-) I suspect Problem (A) is responsible for the disrepancies
between the Line 5, the Universal Time (UTC or GMT) returned by the Win32
SystemTime API and line 6, the Universal Time returned when
DateTime.ToUniversalTime is passed the current date and time as reported in
line 1.

Lines 8 and 9 demonstrate Problem (B). I have run this test on a number of
files and the times are always a few minutes out, though there is no
consistent error. Line 10 shows the local file time you obtain by using the
Win32 API FindFirst File, FileTimeToLocalFileTime and FileTimeToSystemTime
functions, then contructing a DateTime from the SYSTEMTIME fields.(details
are in function MyFirstFileTime at the end of the listing.). I suspect lines
9 and 10 only differ because of Problem (A).

Now have a look at the output from the program when the Automatically Adjust
for Daylight Savings Time box is UNCHECKED.

1 Microsoft Windows NT 5.0.2195.0 :: 11/10/2001 10:13:38 a.m.
2 New Zealand Standard Time starts :: 1/01/0001 12:00:00 a.m.
3 New Zealand Standard Time ends :: 1/01/0001 12:00:00 a.m.
4 TimeZone.IsDayLightSavingTime(Now()) returns False.
5 Universal Time from SystemTime :: 10/10/2001 10:13:38 p.m.
6 Universal Time from Now().ToUniversalTime :: 10/10/2001 10:13:38 p.m.

7 C:\aVB7\FTest.vb
8 File.GetLastWriteTime :: 10/01/2001 6:50:16 a.m.
9 FileInfo.LastWriteTime :: 10/01/2001 6:51:36 a.m.
10 Using FindFirstFile, FileTimeToLocalFileTime, etc :: 10/01/2001 6:51:36
a.m.

Lines 2 and 3 seemed very strange to me at first. I think they reflect new
behaviour in Win2k because under WinMe they are always the same as in the
first example, whether or not the Automatically Adjust for Daylight Savings
Time box is checked. Now it seems that when the box is unchecked in Win2K we
don't merely not _automatically_ adjust for DST, we don't adjust at all.
Indeed, with the box unchecked under Win2K, Explorer reports the file
modified at 6.51 pm; however if the file is viewed from a networked windows
ME computer in the same timezone, the modified time reported is 7.51pm!
This one hour time difference remains however you set the adjust box on the
Windows ME system.

With effectively no daylight saving in force problem (A) is no longer
demonstrated, supporting the conjecture that it was responsible for the
discrepancy between lines 9 and 10 in the previous example. Lines 8 and 9
still demonstrate problem (B).

Although I spend plenty of time RTFMSDN, to coin a phrase, I'd really
welcome comments on this from a Guru - ideally one from the .NET development
team :-).

Here's the listing of the test console app.

Imports System.Console
Imports System.IO
Imports System.Globalization
Imports System.Runtime.InteropServices

Module Module1

Sub Main()
Dim dt As Date
Dim dtUTC As Date = MySystemTime()
Dim dtNow As Date = Now
MyLine("1 " & Environment.OSVersion.ToString, dtNow) 'OS and current
time

Dim nYear As Integer = dtNow.Year
Dim tz As TimeZone = TimeZone.CurrentTimeZone
Dim dlt As DaylightTime = tz.GetDaylightChanges(nYear)
Dim sName As String = tz.DaylightName
MyLine("2 " & sName & " starts", dlt.Start)
MyLine("3 " & sName & " ends", dlt.End)
WriteLine("4 TimeZone.IsDayLightSavingTime(Now()) returns {0}.", _
tz.IsDaylightSavingTime(dtNow))

MyLine("5 Universal Time from SystemTime", dtUTC)
dt = dtNow.ToUniversalTime
MyLine("6 Universal Time from Now().ToUniversalTime", dt)

WriteLine()
Dim sPath As String = Command().Trim(""""c)
WriteLine("7 " & sPath)
dt = File.GetLastWriteTime(sPath)
MyLine("8 File.GetLastWriteTime", dt)

dt = MyFileInfoWriteTime(sPath)
MyLine("9 FileInfo.LastWriteTime", dt)

dt = MyFindFirstFileTime(sPath)
MyLine("10 Using FindFirstFile, FileTimeToLocalFileTime, etc", dt)
ReadLine()
End Sub

Private Function MyFileInfoWriteTime(ByVal sPath As String) As Date
Dim sDir As String = Path.GetDirectoryName(sPath)
Dim sFileName As String = Path.GetFileName(sPath)
Dim dir As New DirectoryInfo(sDir)
Dim fil As FileInfo = dir.GetFiles(sFileName)(0)
Return fil.LastWriteTime
End Function

Private Sub MyLine(ByVal sOp As String, ByVal dt As Date)
Dim sBoth As String = sOp & " :: " & dt.ToString
WriteLine(sBoth)
End Sub

Private Declare Auto Function FindFirstFile Lib "Kernel32" _
(ByVal sPath As String, ByVal p As IntPtr) As
Integer

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Class SystemTime
Public Year As Short
Public Month As Short
Public DayOfWeek As Short
Public Day As Short
Public Hour As Short
Public Minute As Short
Public Second As Short
Public Millisecond As Short
End Class

Declare Auto Sub GetSystemTime Lib "Kernel32" (ByVal st As SystemTime)

Private Function MySystemTime() As Date
'
' Returns current time as Universal Time
'
Dim st As New SystemTime()
GetSystemTime(st)
Dim dt As Date = New Date(st.Year, st.Month, st.Day, _
st.Hour, st.Minute, st.Second, st.Millisecond)
Return dt
End Function


Declare Auto Function FileTimeToSystemTime Lib "Kernel32" _
(ByRef nnFileTime As Long, ByVal st As SystemTime)
As Integer

Declare Auto Function FileTimeToLocalFileTime Lib "Kernel32" _
(ByRef nnFileTime As Long, ByRef nnLocalFileTime As
Long) _
As Integer



Private Function MyFindFirstFileTime(ByVal sPath As String) As Date
'
' How I found the local file time under Visual C++ in 1995
'
Dim p As IntPtr = Marshal.AllocHGlobal(1024)
Dim nResult0 As Integer = FindFirstFile(sPath, p)
Dim nnLastWriteTime As Long = Marshal.ReadInt64(p, 20)
Marshal.FreeHGlobal(p)
Dim nnLocalFileTime As Long
Dim nResult1 As Integer = FileTimeToLocalFileTime(nnLastWriteTime,
nnLocalFileTime)
Dim st As New SystemTime()
Dim nResult2 As Integer = FileTimeToSystemTime(nnLocalFileTime, st)
Dim dt As Date = New Date(st.Year, st.Month, st.Day, _
st.Hour, st.Minute, st.Second, st.Millisecond)
Return dt
End Function

End Module