From f28128a79e50c32c439a2bbb7d180016dee08cbd Mon Sep 17 00:00:00 2001 From: ShareVB Date: Sun, 30 Mar 2025 23:36:07 +0200 Subject: [PATCH] feat(Date Converter): LDAP and Win32FileTime formats Added LDAP format Added Win32FileTime / Windows timestamp format Fix #1483 --- package.json | 1 + pnpm-lock.yaml | 11 ++- .../date-time-converter.models.test.ts | 78 +++++++++++++++++++ .../date-time-converter.models.ts | 51 ++++++++++++ .../date-time-converter.vue | 18 +++++ src/tools/date-time-converter/index.ts | 2 +- 6 files changed, 156 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 1741c0d4..470d369a 100644 --- a/package.json +++ b/package.json @@ -178,6 +178,7 @@ "lodash.defaultsdeep": "^4.6.1", "lodash.flattendeep": "^4.4.0", "lodash.last": "^3.0.0", + "long": "^5.3.1", "lorem-ipsum-japanese": "^1.0.1", "luhn-js": "^1.1.2", "luxon": "^3.5.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 72059b9e..e2f07e26 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -428,6 +428,9 @@ dependencies: lodash.last: specifier: ^3.0.0 version: 3.0.0 + long: + specifier: ^5.3.1 + version: 5.3.1 lorem-ipsum-japanese: specifier: ^1.0.1 version: 1.0.1 @@ -13045,8 +13048,8 @@ packages: is-unicode-supported: 0.1.0 dev: true - /long@5.2.4: - resolution: {integrity: sha512-qtzLbJE8hq7VabR3mISmVGtoXP8KGc2Z/AT8OuqlYD7JTR3oqrgwdjnk07wpj1twXxYmgDXgoKVWUG/fReSzHg==} + /long@5.3.1: + resolution: {integrity: sha512-ka87Jz3gcx/I7Hal94xaN2tZEOPoUOEVftkQqZx2EeQRN7LGdfLlI3FvZ+7WDplm+vK2Urx9ULrvSowtdCieng==} dev: false /loose-envify@1.4.0: @@ -14051,7 +14054,7 @@ packages: dependencies: flatbuffers: 1.12.0 guid-typescript: 1.0.9 - long: 5.2.4 + long: 5.3.1 onnxruntime-common: 1.20.0-dev.20240827-5d54dc1462 platform: 1.3.6 protobufjs: 7.4.0 @@ -14921,7 +14924,7 @@ packages: '@protobufjs/pool': 1.1.0 '@protobufjs/utf8': 1.1.0 '@types/node': 18.19.75 - long: 5.2.4 + long: 5.3.1 dev: false /prr@1.0.1: diff --git a/src/tools/date-time-converter/date-time-converter.models.test.ts b/src/tools/date-time-converter/date-time-converter.models.test.ts index fca174cc..c30f75cd 100644 --- a/src/tools/date-time-converter/date-time-converter.models.test.ts +++ b/src/tools/date-time-converter/date-time-converter.models.test.ts @@ -1,6 +1,8 @@ import { describe, expect, test } from 'vitest'; import { dateToExcelFormat, + dateToLDAPTimestamp, + dateToWin32FileTime, excelFormatToDate, fromJSDate, fromTimestamp, @@ -8,6 +10,7 @@ import { isISO8601DateTimeString, isISO9075DateString, isJSDate, + isLDAPTimestamp, isMongoObjectId, isRFC3339DateString, isRFC7231DateString, @@ -15,7 +18,10 @@ import { isTimestampMicroSeconds, isUTCDateString, isUnixTimestamp, + isWin32FileTime, + lDAPTimestampToDate, toJSDate, + win32FileTimeToUnix, } from './date-time-converter.models'; describe('date-time-converter models', () => { @@ -107,6 +113,29 @@ describe('date-time-converter models', () => { }); }); + describe('isWin32FileTime', () => { + test('should return true for valid Win32 file time', () => { + expect(isWin32FileTime('131461446367662144')).toBe(true); + }); + + test('should return false for invalid Win32 file time', () => { + expect(isWin32FileTime('92233720368547758071')).toBe(false); // too big + expect(isWin32FileTime('foo')).toBe(false); + expect(isWin32FileTime('')).toBe(false); + }); + }); + + describe('isLDAPTimestamp', () => { + test('should return true for valid LDAP timestamps', () => { + expect(isLDAPTimestamp('20250309122345Z')).toBe(true); + }); + + test('should return false for invalid LDAP timestamps', () => { + expect(isLDAPTimestamp('foo')).toBe(false); + expect(isLDAPTimestamp('')).toBe(false); + }); + }); + describe('isTimestamp', () => { test('should return true for valid Unix timestamps in milliseconds', () => { expect(isTimestamp('1649792026123')).toBe(true); @@ -254,3 +283,52 @@ describe('date-time-converter models', () => { }); }); }); + +describe('win32FileTimeToUnix', () => { + test('should correctly convert Win32 FILETIME to UNIX timestamp', () => { + const fileTime = '132271200000000000'; + const expectedDate = new Date(Date.UTC(2020, 1, 25, 16, 0, 0)); + expect(win32FileTimeToUnix(fileTime)).toEqual(expectedDate); + }); + + test('should handle zero FILETIME', () => { + expect(win32FileTimeToUnix('0')).toEqual(new Date(1601, 0, 1, 0, 0, 0)); + }); +}); + +describe('dateToWin32FileTime', () => { + test('should correctly convert Date to Win32 FILETIME', () => { + const date = new Date(Date.UTC(2020, 1, 25, 16, 0, 0)); + const expectedFileTime = '132271200000000000'; + expect(dateToWin32FileTime(date)).toBe(expectedFileTime); + }); + + test('should handle epoch (1970-01-01)', () => { + const date = new Date(0); + const expectedFileTime = '116444736000000000'; + expect(dateToWin32FileTime(date)).toBe(expectedFileTime); + }); +}); + +describe('lDAPTimestampToDate', () => { + test('should correctly parse LDAP timestamp', () => { + const ldapTimestamp = '20240101120013Z'; // January 1, 2024, 12:00:13 UTC + expect(lDAPTimestampToDate(ldapTimestamp)).toEqual(new Date(Date.UTC(2024, 0, 1, 12, 0, 13))); + }); + + test('should return current date on invalid input', () => { + expect(lDAPTimestampToDate('invalid')).toBeInstanceOf(Date); + }); +}); + +describe('dateToLDAPTimestamp', () => { + test('should correctly convert Date to LDAP timestamp', () => { + const date = new Date(Date.UTC(2024, 0, 1, 12, 0, 13)); + expect(dateToLDAPTimestamp(date)).toBe('20240101120013Z'); + }); + + test('should correctly pad single-digit values', () => { + const date = new Date(Date.UTC(2024, 4, 9, 4, 5, 6)); // May 9, 2024, 04:05:06 UTC + expect(dateToLDAPTimestamp(date)).toBe('20240509040506Z'); + }); +}); diff --git a/src/tools/date-time-converter/date-time-converter.models.ts b/src/tools/date-time-converter/date-time-converter.models.ts index e959a29b..e71fb4be 100644 --- a/src/tools/date-time-converter/date-time-converter.models.ts +++ b/src/tools/date-time-converter/date-time-converter.models.ts @@ -1,5 +1,6 @@ import _ from 'lodash'; import { addMilliseconds } from 'date-fns'; +import Long from 'long'; export { isISO8601DateTimeString, @@ -18,6 +19,12 @@ export { isJSDate, fromJSDate, toJSDate, + isLDAPTimestamp, + isWin32FileTime, + win32FileTimeToUnix, + dateToWin32FileTime, + lDAPTimestampToDate, + dateToLDAPTimestamp, }; const ISO8601_REGEX @@ -34,6 +41,8 @@ const EXCEL_FORMAT_REGEX = /^-?\d+(\.\d+)?$/; const JS_DATE_REGEX = /^new\s+Date\(\s*(?:(\d+)\s*,\s*)(?:(\d|11)\s*,\s*(?:(\d+)\s*,\s*(?:(\d+)\s*,\s*(?:(\d+)\s*,\s*(?:(\d+)\s*,\s*)?)?)?)?)?(\d+)\)\s*;?$/; +const LDAP_TIMESTAMP_REGEX = /^([0-9]{4})(0[0-9]|1[012])([012][0-9]|3[01])([01][0-9]|2[0123])([0-5][0-9])([0-5][0-9])Z$/; + function createRegexMatcher(regex: RegExp) { return (date?: string) => !_.isNil(date) && regex.test(date); } @@ -47,6 +56,8 @@ const isTimestamp = createRegexMatcher(/^([0-9]{1,13}|[0-9]{16})$/); const isTimestampMilliSeconds = createRegexMatcher(/^[0-9]{1,13}$/); const isTimestampMicroSeconds = createRegexMatcher(/^[0-9]{16}$/); const isMongoObjectId = createRegexMatcher(/^[0-9a-fA-F]{24}$/); +const isLDAPTimestamp = createRegexMatcher(LDAP_TIMESTAMP_REGEX); +const isWin32FileTime = createRegexMatcher(/^[0-9]{18}$/); const isJSDate = createRegexMatcher(JS_DATE_REGEX); function fromJSDate(date: string): Date { @@ -89,3 +100,43 @@ function fromTimestamp(timestamp: string, type: 'auto' | 'milliseconds' | 'micro } return addMilliseconds(new Date(0), milliSeconds); } + +function win32FileTimeToUnix(ft: string) { + const ulong = Long.fromString(ft, true, 10).div(10000); + let epochBase = ulong.sub(11644473600000); + + if (epochBase.greaterThan(ulong)) { + epochBase = epochBase.toSigned(); + } + + return new Date(epochBase.toNumber()); +} + +function dateToWin32FileTime(date: Date) { + const timestamp = +date; + const long = Long + .fromNumber(timestamp, timestamp >= 0) + .add(11644473600000) + .mul(10000); + + return long.toString(10); +} + +function lDAPTimestampToDate(ldapTimestamp: string) { + const [, yy, mm, dd, hh, nn, ss] = LDAP_TIMESTAMP_REGEX.exec(ldapTimestamp) || []; + if (!yy || !mm) { + return new Date(); + } + return new Date( + Number.parseInt(yy, 10), + Number.parseInt(mm, 10) - 1, + Number.parseInt(dd, 10), + Number.parseInt(hh, 10), + Number.parseInt(nn, 10), + Number.parseInt(ss, 10)); +} + +function dateToLDAPTimestamp(date: Date) { + const pad2 = (n: number) => n.toString().padStart(2, '0'); + return `${date.getUTCFullYear()}${pad2(date.getUTCMonth() + 1)}${pad2(date.getUTCDate())}${pad2(date.getUTCHours())}${pad2(date.getUTCMinutes())}${pad2(date.getUTCSeconds())}Z`; +} diff --git a/src/tools/date-time-converter/date-time-converter.vue b/src/tools/date-time-converter/date-time-converter.vue index d4919851..a4adb78b 100644 --- a/src/tools/date-time-converter/date-time-converter.vue +++ b/src/tools/date-time-converter/date-time-converter.vue @@ -15,6 +15,8 @@ import { UTCDate } from '@date-fns/utc'; import type { DateFormat, ToDateMapper } from './date-time-converter.types'; import { dateToExcelFormat, + dateToLDAPTimestamp, + dateToWin32FileTime, excelFormatToDate, fromJSDate, fromTimestamp, @@ -22,13 +24,17 @@ import { isISO8601DateTimeString, isISO9075DateString, isJSDate, + isLDAPTimestamp, isMongoObjectId, isRFC3339DateString, isRFC7231DateString, isTimestamp, isUTCDateString, isUnixTimestamp, + isWin32FileTime, + lDAPTimestampToDate, toJSDate, + win32FileTimeToUnix, } from './date-time-converter.models'; import { withDefaultOnError } from '@/utils/defaults'; import { useValidation } from '@/composable/validation'; @@ -110,6 +116,18 @@ const formats: DateFormat[] = [ toDate: date => fromJSDate(date), formatMatcher: isJSDate, }, + { + name: 'LDAP YMD Timestamp', + fromDate: date => dateToLDAPTimestamp(date), + toDate: date => lDAPTimestampToDate(date), + formatMatcher: isLDAPTimestamp, + }, + { + name: 'Win32 FileTime/LDAP 18 digits Timestamp', + fromDate: date => dateToWin32FileTime(date), + toDate: date => win32FileTimeToUnix(date), + formatMatcher: isWin32FileTime, + }, ]; const formatIndex = ref(6); diff --git a/src/tools/date-time-converter/index.ts b/src/tools/date-time-converter/index.ts index ec99a757..ba43c3b2 100644 --- a/src/tools/date-time-converter/index.ts +++ b/src/tools/date-time-converter/index.ts @@ -6,7 +6,7 @@ export const tool = defineTool({ name: translate('tools.date-converter.title'), path: '/date-converter', description: translate('tools.date-converter.description'), - keywords: ['date', 'time', 'converter', 'iso', 'utc', 'unix', 'epoch', 'timezone', 'year', 'month', 'day', 'minute', 'seconde'], + keywords: ['date', 'time', 'converter', 'iso', 'utc', 'unix', 'epoch', 'timezone', 'year', 'month', 'day', 'minute', 'seconde', 'filetime', 'ldap', 'win32'], component: () => import('./date-time-converter.vue'), icon: Calendar, npmPackages: ['date-fns'],