ToFileTime vs ToFileTimeUtc issues

P

philibertperusse

I have stumble on problems with ToFileTime and ToFileTimeUtc.... No
matter what I try, I always get the same value out of both of them! You
can see that below:

Anyone has an idea why the following code:
class Program
{
static void CompareDateTime(DateTime dt, DateTime dtUtc)
{
long ftLastWriteTime = dt.ToFileTime();
long ftLastWriteTimeUtc = dt.ToFileTimeUtc();


System.Console.WriteLine("{0} in file time is
{1}.", dt, ftLastWriteTime);

System.Console.WriteLine("{0} in file time UTC is
{1}.", dt, ftLastWriteTimeUtc);

ftLastWriteTime = dtUtc.ToFileTime();
ftLastWriteTimeUtc = dtUtc.ToFileTimeUtc();

System.Console.WriteLine("{0} UTC in file time is
{1}.", dtUtc, ftLastWriteTime);

System.Console.WriteLine("{0} UTC in file time UTC is
{1}.", dtUtc, ftLastWriteTimeUtc);
}

static void Main(string[] args)
{
string filePath;

filePath = "D:\\fsum.exe";

System.DateTime dtLastWriteTime;
System.DateTime dtLastWriteTimeUtc;

System.Console.WriteLine("== INITIALIZING DATETIME WITH
File.GetLastWriteTime ==");
dtLastWriteTime =
System.IO.File.GetLastWriteTime(filePath);
dtLastWriteTimeUtc =
System.IO.File.GetLastWriteTimeUtc(filePath);
CompareDateTime(dtLastWriteTime, dtLastWriteTimeUtc);

System.Console.WriteLine("== INITIALIZING DATETIME WITH
DateTime.Now ==");
dtLastWriteTime = DateTime.Now;
dtLastWriteTimeUtc = DateTime.UtcNow;
CompareDateTime(dtLastWriteTime, dtLastWriteTimeUtc);


}
}

Gives the following output:
== INITIALIZING DATETIME WITH File.GetLastWriteTime ==
2004-07-06 21:47:32 in file time is 127336384520000000.
2004-07-06 21:47:32 in file time UTC is 127336384520000000.
2004-07-07 01:47:32 UTC in file time is 127336384520000000.
2004-07-07 01:47:32 UTC in file time UTC is 127336384520000000.
== INITIALIZING DATETIME WITH DateTime.Now ==
2006-07-17 10:25:21 in file time is 127976199219998752.
2006-07-17 10:25:21 in file time UTC is 127976199219998752.
2006-07-17 14:25:21 UTC in file time is 127976199219998752.
2006-07-17 14:25:21 UTC in file time UTC is 127976199219998752.
 
K

Kevin Spencer

Hi Philbert,

The DateTime.ToFileTime method does return a UTC time in the .Net platform
2.0. From the SDK documentation:

"Previous versions of the ToFileTime method assume the current DateTime
object is a local time. Starting with the.NET Framework version 2.0, the
ToFileTime method uses the Kind property to determine whether the current
DateTime object is a local time, a UTC time, or an unspecified kind of time
which is treated as a local time. "

And according to a Word document on GotDotNet, the LocalFileTime assumes
UTC:
http://www.gotdotnet.com/team/clr/bcl/TechArticles/techarticles/datetimefaq.doc

The Word document is quite good. It explains how each of the DateTime
methods is calculated. It also explains that DateTime data is not stored
with reference to any particular culture or time zone, which makes sense,
because regardless of where you are, and how you measure it, it is always
the same time everywhere in the world. In other words, Time is in a sense a
mesure of length, which is relative to something else, just as distance is a
measure of length relative to something else. 1 mile is 1.609344 KM, but
they are the same distance. It's 1:40 PM here, and early afternoon, but on
the opposite side of the world, it's 1:40 AM and very late at night. But
it's still now in both places, and it's still the same number of years,
months, days, minutes, hours, and seconds from the Date and Time that Mt.
Krakatoa exploded. DateTime data must be referenced within some context in
order to be locally meaningful.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.


philibertperusse said:
I have stumble on problems with ToFileTime and ToFileTimeUtc.... No
matter what I try, I always get the same value out of both of them! You
can see that below:

Anyone has an idea why the following code:
class Program
{
static void CompareDateTime(DateTime dt, DateTime dtUtc)
{
long ftLastWriteTime = dt.ToFileTime();
long ftLastWriteTimeUtc = dt.ToFileTimeUtc();


System.Console.WriteLine("{0} in file time is
{1}.", dt, ftLastWriteTime);

System.Console.WriteLine("{0} in file time UTC is
{1}.", dt, ftLastWriteTimeUtc);

ftLastWriteTime = dtUtc.ToFileTime();
ftLastWriteTimeUtc = dtUtc.ToFileTimeUtc();

System.Console.WriteLine("{0} UTC in file time is
{1}.", dtUtc, ftLastWriteTime);

System.Console.WriteLine("{0} UTC in file time UTC is
{1}.", dtUtc, ftLastWriteTimeUtc);
}

static void Main(string[] args)
{
string filePath;

filePath = "D:\\fsum.exe";

System.DateTime dtLastWriteTime;
System.DateTime dtLastWriteTimeUtc;

System.Console.WriteLine("== INITIALIZING DATETIME WITH
File.GetLastWriteTime ==");
dtLastWriteTime =
System.IO.File.GetLastWriteTime(filePath);
dtLastWriteTimeUtc =
System.IO.File.GetLastWriteTimeUtc(filePath);
CompareDateTime(dtLastWriteTime, dtLastWriteTimeUtc);

System.Console.WriteLine("== INITIALIZING DATETIME WITH
DateTime.Now ==");
dtLastWriteTime = DateTime.Now;
dtLastWriteTimeUtc = DateTime.UtcNow;
CompareDateTime(dtLastWriteTime, dtLastWriteTimeUtc);


}
}

Gives the following output:
== INITIALIZING DATETIME WITH File.GetLastWriteTime ==
2004-07-06 21:47:32 in file time is 127336384520000000.
2004-07-06 21:47:32 in file time UTC is 127336384520000000.
2004-07-07 01:47:32 UTC in file time is 127336384520000000.
2004-07-07 01:47:32 UTC in file time UTC is 127336384520000000.
== INITIALIZING DATETIME WITH DateTime.Now ==
2006-07-17 10:25:21 in file time is 127976199219998752.
2006-07-17 10:25:21 in file time UTC is 127976199219998752.
2006-07-17 14:25:21 UTC in file time is 127976199219998752.
2006-07-17 14:25:21 UTC in file time UTC is 127976199219998752.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top