All this changes are to internal methods which are later called by other public methods which correctly call `ToLocalTime` for local time "overloads".
Performance hit is most visible for application which doesn't need to load local timezone information which takes ~30ms on 2016 MacBook Pro.
Co-authored-by: Jeff Handley <jeff.handley@microsoft.com>
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Collections.Generic;
+using System.IO.Enumeration;
+using Xunit;
+
+namespace System.IO.Tests.Enumeration
+{
+ public class GetTimesTests : FileSystemTest
+ {
+ private class AllEntries : FileSystemEnumerator<(DateTimeOffset CreationTimeUtc, DateTimeOffset LastAccessTimeUtc, DateTimeOffset LastWriteTimeUtc)>
+ {
+ public AllEntries(string directory, EnumerationOptions options)
+ : base(directory, options)
+ {
+ }
+
+ protected override (DateTimeOffset CreationTimeUtc, DateTimeOffset LastAccessTimeUtc, DateTimeOffset LastWriteTimeUtc) TransformEntry(ref FileSystemEntry entry)
+ {
+ return (entry.CreationTimeUtc, entry.LastAccessTimeUtc, entry.LastWriteTimeUtc);
+ }
+ }
+
+ [Fact]
+ public void FileTimesShouldBeUtc()
+ {
+ DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath());
+ FileInfo fileOne = new FileInfo(Path.Combine(testDirectory.FullName, GetTestFileName()));
+
+ fileOne.Create().Dispose();
+
+ using (var enumerator = new AllEntries(testDirectory.FullName, new EnumerationOptions()))
+ {
+ Assert.True(enumerator.MoveNext());
+ Assert.Equal(TimeSpan.Zero, enumerator.Current.CreationTimeUtc.Offset);
+ Assert.Equal(TimeSpan.Zero, enumerator.Current.LastAccessTimeUtc.Offset);
+ Assert.Equal(TimeSpan.Zero, enumerator.Current.LastWriteTimeUtc.Offset);
+ Assert.False(enumerator.MoveNext());
+ }
+ }
+ }
+}
<Compile Include="FileStream\DisposeAsync.cs" />
<Compile Include="FileStream\ReadWriteSpan.cs" />
<Compile Include="Enumeration\ConstructionTests.cs" />
+ <Compile Include="Enumeration\GetTimesTests.cs" />
<Compile Include="Enumeration\RecursionDepthTests.cs" />
<Compile Include="Enumeration\SpecialDirectoryTests.cs" />
<Compile Include="Enumeration\SkipAttributeTests.cs" />
{
EnsureCachesInitialized(path, continueOnError);
if (!_exists)
- return DateTimeOffset.FromFileTime(0);
+ return new DateTimeOffset(DateTime.FromFileTimeUtc(0));
if ((_fileCache.Flags & Interop.Sys.FileStatusFlags.HasBirthTime) != 0)
return UnixTimeToDateTimeOffset(_fileCache.BirthTime, _fileCache.BirthTimeNsec);
{
EnsureCachesInitialized(path, continueOnError);
if (!_exists)
- return DateTimeOffset.FromFileTime(0);
+ return new DateTimeOffset(DateTime.FromFileTimeUtc(0));
return UnixTimeToDateTimeOffset(_fileCache.ATime, _fileCache.ATimeNsec);
}
{
EnsureCachesInitialized(path, continueOnError);
if (!_exists)
- return DateTimeOffset.FromFileTime(0);
+ return new DateTimeOffset(DateTime.FromFileTimeUtc(0));
return UnixTimeToDateTimeOffset(_fileCache.MTime, _fileCache.MTimeNsec);
}
private DateTimeOffset UnixTimeToDateTimeOffset(long seconds, long nanoseconds)
{
- return DateTimeOffset.FromUnixTimeSeconds(seconds).AddTicks(nanoseconds / NanosecondsPerTick).ToLocalTime();
+ return DateTimeOffset.FromUnixTimeSeconds(seconds).AddTicks(nanoseconds / NanosecondsPerTick);
}
private unsafe void SetAccessOrWriteTime(string path, DateTimeOffset time, bool isAccessTime)
public static DateTimeOffset GetCreationTime(string fullPath)
{
- return new FileInfo(fullPath, null).CreationTime;
+ return new FileInfo(fullPath, null).CreationTimeUtc;
}
public static void SetCreationTime(string fullPath, DateTimeOffset time, bool asDirectory)
public static DateTimeOffset GetLastAccessTime(string fullPath)
{
- return new FileInfo(fullPath, null).LastAccessTime;
+ return new FileInfo(fullPath, null).LastAccessTimeUtc;
}
public static void SetLastAccessTime(string fullPath, DateTimeOffset time, bool asDirectory)
public static DateTimeOffset GetLastWriteTime(string fullPath)
{
- return new FileInfo(fullPath, null).LastWriteTime;
+ return new FileInfo(fullPath, null).LastWriteTimeUtc;
}
public static void SetLastWriteTime(string fullPath, DateTimeOffset time, bool asDirectory)