Re: ToFileTime vs ToFileTimeUtc issues



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" <pperusse.nospam@xxxxxxxxxxxxxx> wrote in message
news:1153146532.465673.299400@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.



.



Relevant Pages

  • Re: UTC dates in SQL 2000
    ... Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP) www.SQLDTS.com - The site for all your DTS needs. ... > This db provides dates, which the developer > identifies as UTC, in the format 99999. ... > getutcdatefunction to return the datetime for the> current UTC, or I can CONVERTto convert a> datetime into UTC, but neither will work in an ActiveX> script. ...
    (microsoft.public.sqlserver.dts)
  • Re: Web services and incorrect handling of time zones in DateTime
    ... You are not the first one writing this, I think that it is an error by design. ... when the SOAP message contains datetime like ... exactly the same time as previously, but in UTC), it becomes DateTime ... the UTC time should be converted to local time ...
    (microsoft.public.dotnet.general)
  • How do I set the DateTimeMode property when filling a DataTable
    ... I am investigating how best to manage UTC timestamps in ADO.Net. ... ran into a lot of issues with UTC DateTime serialization in .NET 1.1. ... Here is my test code. ... SqlConnection conn = new ...
    (microsoft.public.dotnet.framework.adonet)
  • How do I set the DateTimeMode property when filling a DataTable
    ... I am investigating how best to manage UTC timestamps in ADO.Net. ... ran into a lot of issues with UTC DateTime serialization in .NET 1.1. ... Here is my test code. ... SqlConnection conn = new ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Time zone
    ... Always get, set, calc, and store only UTC ... Your client side ... can easily convert to/from UTC so they can display local times. ... change their time zone when they travel and still get and use the datetime ...
    (microsoft.public.dotnet.languages.csharp)