webtools/site/timestamps/test_logic.js

104 lines
3.5 KiB
JavaScript
Raw Normal View History

2025-11-19 10:20:06 +00:00
function processLine(line, operation, timezone) {
if (!line.trim() || line.trim().startsWith('#')) {
return line;
}
try {
let date;
const trimmedLine = line.trim();
switch (operation) {
case 'unix':
// Input is seconds
date = new Date(Number(trimmedLine) * 1000);
break;
case 'unix_micro':
// Input is milliseconds
date = new Date(Number(trimmedLine));
break;
case 'to_utc':
case 'from_utc':
// Input is ISO 8601
date = new Date(trimmedLine);
break;
}
if (isNaN(date.getTime())) {
return 'Invalid Date';
}
if (operation === 'to_utc') {
let dateToConvert = date;
if (timezone === 'utc' && !trimmedLine.toUpperCase().endsWith('Z') && !trimmedLine.match(/[+-]\d{2}:?\d{2}$/)) {
dateToConvert = new Date(trimmedLine + 'Z');
}
return dateToConvert.toISOString();
}
if (operation === 'from_utc') {
let dateFromUtc;
if (!trimmedLine.toUpperCase().endsWith('Z') && !trimmedLine.match(/[+-]\d{2}:?\d{2}$/)) {
dateFromUtc = new Date(trimmedLine + 'Z');
} else {
dateFromUtc = new Date(trimmedLine);
}
if (timezone === 'utc') {
return dateFromUtc.toISOString();
} else {
// Local
const offset = dateFromUtc.getTimezoneOffset();
const localDate = new Date(dateFromUtc.getTime() - (offset * 60 * 1000));
return localDate.toISOString().slice(0, -1);
}
}
// For Unix conversions:
if (timezone === 'utc') {
return date.toISOString();
} else {
// Local
const offset = date.getTimezoneOffset();
const localDate = new Date(date.getTime() - (offset * 60 * 1000));
return localDate.toISOString().slice(0, -1);
}
} catch (e) {
return 'Error';
}
}
// Tests
console.log("Running tests...");
// 1. Unix to UTC
const t1 = processLine('1672531200', 'unix', 'utc');
console.log(`1. Unix to UTC: ${t1} (Expected: 2023-01-01T00:00:00.000Z)`);
if (t1 !== '2023-01-01T00:00:00.000Z') console.error("FAIL");
// 2. Unix Micro to UTC
const t2 = processLine('1672531200000', 'unix_micro', 'utc');
console.log(`2. Unix Micro to UTC: ${t2} (Expected: 2023-01-01T00:00:00.000Z)`);
if (t2 !== '2023-01-01T00:00:00.000Z') console.error("FAIL");
// 3. To UTC (from Local input)
// Note: Node.js server might be in UTC or Local.
// If I run this on a machine, "Local" depends on system time.
// However, the logic for 'to_utc' with 'utc' timezone forces Z.
const t3 = processLine('2023-01-01T00:00:00', 'to_utc', 'utc');
console.log(`3. To UTC (input no offset, selected UTC): ${t3} (Expected: 2023-01-01T00:00:00.000Z)`);
if (t3 !== '2023-01-01T00:00:00.000Z') console.error("FAIL");
// 4. From UTC (to UTC)
const t4 = processLine('2023-01-01T00:00:00', 'from_utc', 'utc');
console.log(`4. From UTC (input no offset, selected UTC): ${t4} (Expected: 2023-01-01T00:00:00.000Z)`);
if (t4 !== '2023-01-01T00:00:00.000Z') console.error("FAIL");
// 5. Comment
const t5 = processLine('# comment', 'unix', 'utc');
console.log(`5. Comment: ${t5} (Expected: # comment)`);
if (t5 !== '# comment') console.error("FAIL");
console.log("Tests finished.");