1

Why does the following display 12/31/1600 ???

Imports System.IO

Module Module1

Sub Main()
    Dim fi As New FileInfo("DocFiles\phillips_phone_number.txt")
    Console.WriteLine(fi.FullName)
    Console.WriteLine(fi.LastAccessTime.ToShortDateString)
    Console.ReadKey()
End Sub

End Module

dannyrosalex
  • 1,794
  • 4
  • 16
  • 25

2 Answers2

2

It seems that code is not able to find the file, see the documentation for the GetLastAccessTime function in the SDK http://msdn.microsoft.com/en-us/library/system.io.file.getlastaccesstime.aspx

It states,

If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

Sijin
  • 4,530
  • 21
  • 22
  • OK, this one is a candidate for most bizarre .NET library behavior. Why would they return *anything* instead of throwing an exception here, and why return that date in particular? And why return an adjusted local time instead of UTC (making this condition much more difficult to check for)? This is weirder than .NET throwing an OutOfMemoryException when trying to load an invalid Image file. – MusiGenesis May 19 '10 at 15:57
  • This is a candidate for my .NET gotchas question: http://stackoverflow.com/questions/241134/what-is-the-worst-gotcha-in-c-or-net – MusiGenesis May 19 '10 at 15:59
0

You did not specify the time you are seeing, only the date, but I suspect it is 23:00:00. If you use Console.WriteLine(fi.LastAccessTime), you will likely see it print -36000000000.

The last-access-timestamp of the file is being returned as 0 either because, as Sijin pointed out, the path is invalid, or because you have disabled the last-access update on an NTFS volume, or because you are using Vista or higher in which it is disabled by default.

In any case, because Windows uses January 1, 1601 as its epoch base, a date of 0 would return 00:00:00 1601-01-01.

Now why are you seeing it as December 31, 1600 instead? Simple: timezones. Your profile has no biographical information, but your name and the Latin quote in your biography section make it not unreasonable to guess that you may be living somewhere around Spain, which is in the timezone UTC+01:00. In this case, when the timestamp is adjusted to local time, it would take the time (0) as the base, and subtract one hour to convert it to your timezone. Thus, you get the value -36000000000 which is 23:00:00 1600-12-31.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Synetech
  • 9,643
  • 9
  • 64
  • 96