-
Beta2 Southern Hemisphere Blues
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
-
Re: Beta2 Southern Hemisphere Blues
"Michael (michka) Kaplan" <former_mvp@spamfree.trigeminal.nospam.com> wrote
in message news:3bce0458@news.devx.com...
I tried it with values from 1 Jan 1995 to present day and it always returned
false. I suspect it is to do with the something about the packaging of
Windows for NZ itself, since I cannot believe it wouldn't have been found by
lots of other Southern hemisphere users. I have had email from someone in
Microsoft who says it is being investigated.
Thanks for your help. Sorry not to have replied earlier but I tend to do
email about once a week so I can focus on .net. Very impressiver once you
get into it.
-
Re: Beta2 Southern Hemisphere Blues
I know it is a bit late, but I've confirmed that Beta2 does have the same
problem for me with SA/NT time zones.
[Note that the meaning of the checkbox in the Time Zone tab of the Date/Time
control panel is "Does daylight savings apply in your location?", irrespective
of what they thought "Automatically" means. That is, the only difference
between SA and NT is that NT does not have daylight savings, so the only
difference between the settings is that the checkbox is marked or not.]
Can someone in the southern hemisphere with a RC version check that the code
in the root message of this thread produces correct results? The command line
to compile his code (after correcting word wrapped lines) is
vbc -r:Microsoft.VisualBasic.dll -imports:System,Microsoft.VisualBasic
VBdotNetDateTest.vb
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)
[Daylight savings does not apply => Northern Territory]
1 Microsoft Windows NT 5.0.2195.0 :: 14/01/2002 9:28:28 PM
2 Cen. Australia Standard Time starts :: 1/01/0001 12:00:00 AM
3 Cen. Australia Standard Time ends :: 1/01/0001 12:00:00 AM
4 TimeZone.IsDayLightSavingTime(Now()) returns False.
5 Universal Time from SystemTime :: 14/01/2002 11:58:28 AM
6 Universal Time from Now().ToUniversalTime :: 14/01/2002 11:58:28 AM
7 C:\...\VBdotNetDateTest.exe
8 File.GetLastWriteTime :: 14/01/2002 9:15:47 PM
9 FileInfo.LastWriteTime :: 14/01/2002 9:19:34 PM
10 Using FindFirstFile, FileTimeToLocalFileTime, etc :: 14/01/2002 9:19:34 PM
[Daylight savings applies => South Australia]
1 Microsoft Windows NT 5.0.2195.0 :: 14/01/2002 10:29:30 PM
2 Cen. Australia Daylight Time starts :: 27/10/2002 2:00:00 AM
3 Cen. Australia Daylight Time ends :: 31/03/2002 2:00:00 AM
4 TimeZone.IsDayLightSavingTime(Now()) returns False.
5 Universal Time from SystemTime :: 14/01/2002 11:59:30 AM
6 Universal Time from Now().ToUniversalTime :: 14/01/2002 12:59:30 PM
7 C:\...\VBdotNetDateTest.exe
8 File.GetLastWriteTime :: 14/01/2002 9:15:47 PM
9 FileInfo.LastWriteTime :: 14/01/2002 9:19:34 PM
10 Using FindFirstFile, FileTimeToLocalFileTime, etc :: 14/01/2002 10:19:34 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks