Skip to content

Commit 66e595c

Browse files
[storage-file-datalake] [Bug Fix] Fix the date property(createdOn, expiresOn) in some of the returned responses (Azure#25072)
### Packages impacted by this PR `@azure/storage-file-datalake` ### Issues associated with this PR Fixes Azure#25068 ### Describe the problem that is addressed by this PR `windowsFileTimeTicksToTime` calculation somehow went wrong since 2023 with a 1-day difference, perhaps due to miscalculation w.r.t leap days. This PR attempts to fix the bug and fix the tests. The new implementation makes use of the JS Date object to do most of the calculation instead of relying on the numbers/constants to account for the leap days. ```js // Existing Code - Straight from the storage-file-datalake code function windowsFileTimeTicksToTime(timeNumber: string | undefined): Date | undefined { if (!timeNumber) return undefined; const timeNumberInternal = parseInt(timeNumber!); if (timeNumberInternal === 0) return undefined; const date = new Date(timeNumberInternal / 10000); date.setUTCFullYear(date.getUTCFullYear() - 369); return date; } // New Implementation - Alternative algo from stackoverflow that works as expected // Reference https://stackoverflow.com/a/24188106 function windowsFileTimeTicksToTime2(timeNumber: string | undefined): Date | undefined { if (!timeNumber) return undefined; const timeElapsed = parseInt(timeNumber)/10000; if (timeElapsed === 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); } // Value from a saved recording const olderTicks = "132934495529749313"; // April 3rd, 2022 console.log(windowsFileTimeTicksToTime(olderTicks)) // "2022-04-03T08:52:32.974Z" console.log(windowsFileTimeTicksToTime2(olderTicks)) // "2022-04-03T08:52:32.974Z" // Current time (At about the time of this PR) const newerTicks = "133222649014464202"; // March 2nd, 2023 console.log(windowsFileTimeTicksToTime(newerTicks)) // "2023-03-01T21:08:21.446Z" console.log(windowsFileTimeTicksToTime2(newerTicks)) // "2023-03-02T21:08:21.446Z" ``` ## Playground if you want to play with [windows filetime to JS date - TS playground](https://www.typescriptlang.org/play?#code/PTAEGUBcCcEME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AuRep2B4CtWBkz5FDW+9NpJHbYyIYpPOHNAcgAUSZFlBhGMYJimLT1mgYJgFXXFqgu0BzTJf6NojbbSE5YgACJeWq2rkwapqM1a1UyeA867Oh2H4h2IYDhhY4KjOKorlKiGyWBeB2DUOA9AAeXQGj0Dorl4enQV93MBnFT2BUsUWMrQGFdbAcjT4eA+r7uphDgoMheCKfjGrEzqlNGrTZqs1zMmMgUR7HBbQhEG++lgk8n8TgnSaenMRRRfFyXCBluWFeIABqQKYaKlkdZucxzVuSB1dAF8c7z07ChT8x1TKYpugL3GWlz9XCiGcvK5WtBC7r4vcyDoDKNAAA1eaYzwb5+vpUla06-tQEEYWsErJwVPwoYKdfHlM1fP4-kzABWbe1+zLfXyxV8yex0C4QYN9oCYJUeT2HkeRnmQSDn7dMMQcw5KeRTGGUgwjBMOacwc8BDQEXiQYIhQcZkzvg-agewAR7FfFoPYwlnB72cKvQUOY-gAC0PZP0aK-QU79P6PAUhoX+LRwGAI0sA+eYD-4QMKDcGB98eTwMQcg1B6C+RYJwfg6evcK41lboFDIucvj2HLJDIg4jMQ+mCgABSooUQhTQJwLyYfDFeq8H7-EzKqTe-xYGnxxsKMYPBkCgB5EoW+99XzqOIaQr+FClLUKYbQiwmjGGqUgbrNhPJXzwOCaqLQPIsT8jQREwUxj8FOMYCQrAH9XGqEofoPQND1J6E0j48B-jWGwNCaEnk4TIk8JiXEpmgggA)
1 parent 9a1c1a9 commit 66e595c

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

sdk/storage/storage-file-datalake/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
### Bugs Fixed
2525

26+
- Fixes the date properties(`createdOn` and `expiresOn`) in the returned responses of `DataLakeFileSystemClient` by calculating the date more accurately. [#25072](https://github.com/Azure/azure-sdk-for-js/pull/25072)
27+
2628
### Other Changes
2729

2830
- Update dependency `@azure/core-http` version to `^3.0.0`.

sdk/storage/storage-file-datalake/src/utils/utils.common.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -565,17 +565,23 @@ export function isIpEndpointStyle(parsedUrl: URL): boolean {
565565
*/
566566
export function windowsFileTimeTicksToTime(timeNumber: string | undefined): Date | undefined {
567567
if (!timeNumber) return undefined;
568-
const timeNumberInternal = parseInt(timeNumber!);
569-
570-
if (timeNumberInternal === 0) return undefined;
571-
572568
// A windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed
573569
// since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).
574-
// Date accepts a value that represents miliseconds from 12:00 A.M. January 1, 1970
575-
// Here should correct the year number after converting.
576-
const date = new Date(timeNumberInternal / 10000);
577-
date.setUTCFullYear(date.getUTCFullYear() - 369);
578-
return date;
570+
// JS Date accepts a value that represents milliseconds from 12:00 A.M. January 1, 1970
571+
// So, we'll handle the calculations in milliseconds from here
572+
573+
// Time in milliseconds since "12:00 A.M. January 1, 1601"
574+
const timeElapsed = parseInt(timeNumber) / 10000;
575+
576+
if (timeElapsed === 0) return undefined;
577+
578+
// Reference - https://stackoverflow.com/a/24188106/4137356
579+
580+
// Milliseconds calculated relative to "12:00 A.M. January 1, 1970" (will be negative)
581+
const initialFrameOfReference = Date.UTC(1601, 0, 1);
582+
583+
// TimeRelativeTo1970 = (TimeAt1601 - TimeAt1970) + (Current - TimeAt1601) = (Current - TimeAt1970)
584+
return new Date(initialFrameOfReference + timeElapsed);
579585
}
580586

581587
export function ensureCpkIfSpecified(cpk: CpkInfo | undefined, isHttps: boolean): void {

0 commit comments

Comments
 (0)