Switched to expression-based timestamps
This commit is contained in:
parent
7bf586757a
commit
5c89c44ff7
7 changed files with 215 additions and 151 deletions
|
|
@ -13,10 +13,9 @@
|
|||
<h1>Timestamp Converter</h1>
|
||||
<div class="controls">
|
||||
<select id="operation">
|
||||
<option value="unix">Convert from Unix</option>
|
||||
<option value="unix_micro">Convert from Unix Micro</option>
|
||||
<option value="to_utc">To UTC</option>
|
||||
<option value="from_utc">From UTC</option>
|
||||
<option value="iso_8601">ISO-8601</option>
|
||||
<option value="unix">Unix</option>
|
||||
<option value="unix_micro">Unix Micro</option>
|
||||
</select>
|
||||
<div class="timezone-selector">
|
||||
<label>
|
||||
|
|
|
|||
|
|
@ -1,103 +0,0 @@
|
|||
|
||||
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.");
|
||||
Loading…
Add table
Add a link
Reference in a new issue