Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion packages/test-serializer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/uhk-agent/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions packages/uhk-common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/uhk-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"buffer": "6.0.3",
"lodash.isequal": "4.5.0",
"md5": "2.3.0",
"moment": "2.30.1",
"tslib": "2.8.1"
},
"license": "GPL-3.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, it } from 'node:test';

import {
convertHistoryFilenameToDisplayText,
convertDateToDisplayText,
getUserConfigHistoryFilename,
} from './user-configuration-history-helpers.js';

it('getUserConfigHistoryFilename should return with proper name', ({ assert, mock }) => {
const md5Hash = '1234567890abcdef1234567890abcdef';
const fakeNow = new Date(2026, 4, 28, 15, 23, 45)
mock.timers.enable({ apis: ['Date'], now: fakeNow })
const filename = getUserConfigHistoryFilename(md5Hash);

assert.strictEqual(filename, '20260528-152345-1234567890abcdef1234567890abcdef.bin');
});

it('convertDateToDisplayText should properly format date', ({ assert }) => {
const testDate = new Date(2026, 4, 28, 15, 23, 45);
const displayText = convertDateToDisplayText(testDate);

assert.strictEqual(displayText, '2026-05-28 15:23:45');
});

describe('convertHistoryFilenameToDisplayText', () => {
it('should properly parses filename', ({ assert }) => {
const testFilename = '20260528-152345-1234567890abcdef1234567890abcdef.bin';
const displayText = convertHistoryFilenameToDisplayText(testFilename);

assert.strictEqual(displayText, '2026-05-28 15:23:45');
});

it('should return with "Invalid date" when filename length shorter than 15 character', ({ assert }) => {
const testFilename = '20260528-15234';
const displayText = convertHistoryFilenameToDisplayText(testFilename);

assert.strictEqual(displayText, 'Invalid date');
});
})
60 changes: 51 additions & 9 deletions packages/uhk-common/src/util/user-configuration-history-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,62 @@
import moment from 'moment';

export const FILENAME_DATE_FORMAT = 'YYYYMMDD-HHmmss';
export const DISPLAY_DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss';

export function getUserConfigHistoryFilename(md5Hash: string): string {
const timestamp = moment().format(FILENAME_DATE_FORMAT);
const timestamp = formatFilenameTimestamp(new Date());

return `${timestamp}-${md5Hash}.bin`;
}

export function convertHistoryFilenameToDisplayText(filename: string): string {
const timestamp = filename.substring(0, 16);
// simulates moment.js behavior
if (filename.length < 15) {
return 'Invalid date';
}

const timestamp = filename.substring(0, 15);

return moment(timestamp, FILENAME_DATE_FORMAT).format(DISPLAY_DATE_FORMAT);
const year = timestamp.substring(0, 4);
const month = timestamp.substring(4, 6);
const day = timestamp.substring(6, 8);
// index 8 is the '-' separator
const hours = timestamp.substring(9, 11);
const minutes = timestamp.substring(11, 13);
const seconds = timestamp.substring(13, 15);

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

/**
* Formats a Date as `YYYY-MM-DD HH:mm:ss` using local time.
*/
export function convertDateToDisplayText(date: Date): string {
return moment(date).format(DISPLAY_DATE_FORMAT);
const segments = getDateSegments(date);

return `${segments.year}-${segments.month}-${segments.day} ${segments.hours}:${segments.minutes}:${segments.seconds}`;
}

/**
* Formats a Date as `YYYYMMDD-HHmmss` using local time.
*/
function formatFilenameTimestamp(date: Date): string {
const segments = getDateSegments(date);

return `${segments.year}${segments.month}${segments.day}-${segments.hours}${segments.minutes}${segments.seconds}`;
}

interface DateSegments {
year: string;
month: string;
day: string;
hours: string;
minutes: string;
seconds: string;
}

function getDateSegments(date: Date): DateSegments {
return {
year: date.getFullYear().toString().padStart(4, '0'),
month: (date.getMonth() + 1).toString().padStart(2, '0'),
day: date.getDate().toString().padStart(2, '0'),
hours: date.getHours().toString().padStart(2, '0'),
minutes: date.getMinutes().toString().padStart(2, '0'),
seconds: date.getSeconds().toString().padStart(2, '0'),
};
}
1 change: 0 additions & 1 deletion packages/uhk-usb/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/uhk-web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/usb/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading