I noticed that no matter what I do, tofiletimeutc() always returns the same as toFileTime(). I need to produce a UTC timestamp. So I tried this experiment in Powershell:
(get-date).tofiletime();
(get-date).touniversalTime().tofiletimeutc();
(get-date).tofiletimeutc();
and the output is:
129757574870723241
129757574870723241
129757574870723241
(All 3 are identical, local-time stamps. Time zone is set to Pacific).
(get-date).touniversalTime() works as expected, but no matter which “to-utc” function I use, it is for some reason converted back into local time. Does anyone see what is going on?
Yes, it does work all three values you see are filetimes “in UTC”.
Background:
The Windows FILETIME is defined as: “a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).”
Both
DateTime.ToFileTime()andDateTime.ToFileTimeUtc()return a UTC-based value.In fact,
DateTime.ToFileTime()is implemented asDateTime.ToUniversalTime().ToFileTimeUtc().Update Concerning the comments:
The value is always the same, as said above, what you “see” when converting it back to a
DateTimeinstance and dumping that, on which method you use:Those two return the “same” DateTime value with
DateTimeKind.Utcand theDateTimevalue “adjusted” to UTC.Those two return the “same” DateTime value with
DateTimeKind.Localand theDateTimevalue adjusted to local.