Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[storage] port windows file time ticks bug fix from storage/stable #26535

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions sdk/storage/storage-file-datalake/src/utils/utils.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading