diff --git a/sdk/storage/storage-file-datalake/src/utils/utils.common.ts b/sdk/storage/storage-file-datalake/src/utils/utils.common.ts index d04051e8d911..669e1244186e 100644 --- a/sdk/storage/storage-file-datalake/src/utils/utils.common.ts +++ b/sdk/storage/storage-file-datalake/src/utils/utils.common.ts @@ -555,28 +555,32 @@ export function isIpEndpointStyle(parsedUrl: URL): boolean { ); } +const BugTimeBeginningInMS = 13322188800000; + /** * This is to convert a Windows File Time ticks to a Date object. */ export function windowsFileTimeTicksToTime(timeNumber: string | undefined): Date | undefined { if (!timeNumber) return undefined; - // A windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed - // since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC). - // JS Date accepts a value that represents milliseconds from 12:00 A.M. January 1, 1970 - // So, we'll handle the calculations in milliseconds from here - - // Time in milliseconds since "12:00 A.M. January 1, 1601" - const timeElapsed = parseInt(timeNumber) / 10000; - - if (timeElapsed === 0) return undefined; + const timeNumberInternal = parseInt(timeNumber!); - // Reference - https://stackoverflow.com/a/24188106/4137356 + if (timeNumberInternal === 0) return undefined; - // Milliseconds calculated relative to "12:00 A.M. January 1, 1970" (will be negative) - const initialFrameOfReference = Date.UTC(1601, 0, 1); - - // TimeRelativeTo1970 = (TimeAt1601 - TimeAt1970) + (Current - TimeAt1601) = (Current - TimeAt1970) - return new Date(initialFrameOfReference + timeElapsed); + // A windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed + // since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC). + // Date accepts a value that represents miliseconds from 12:00 A.M. January 1, 1970 + // Here should correct the year number after converting. + const timeNumerInMs = timeNumberInternal / 10000; + const date = new Date(timeNumerInMs); + + // When initializing date from a miliseconds number the day after 2023-03-01 is still 2023-03-01. + // For example, 13322188799999 is 2023-03-01T23:59:59.999Z, while 13322188800000 is 2023-03-01T00:00:00.000Z + // Here is to work around the bug. + if (timeNumerInMs >= BugTimeBeginningInMS) { + date.setUTCDate(date.getUTCDate() + 1); + } + date.setUTCFullYear(date.getUTCFullYear() - 369); + return date; } export function ensureCpkIfSpecified(cpk: CpkInfo | undefined, isHttps: boolean): void {