Vibe coded "timestamps"

This commit is contained in:
Leon Mika 2025-11-19 21:20:06 +11:00
parent 35270f72ca
commit 7bf586757a
6 changed files with 441 additions and 0 deletions

View file

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Timestamp Converter</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<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>
</select>
<div class="timezone-selector">
<label>
<input type="radio" name="timezone" value="utc" checked> UTC
</label>
<label>
<input type="radio" name="timezone" value="local"> Local
</label>
</div>
</div>
<div class="editor-pane">
<textarea id="input" placeholder="Enter timestamps here (one per line)..." spellcheck="false"></textarea>
<textarea id="output" readonly placeholder="Results will appear here..." spellcheck="false"></textarea>
</div>
</div>
<script src="main.js" type="module"></script>
</body>
</html>

8
site/timestamps/main.js Normal file
View file

@ -0,0 +1,8 @@
import "/wasm/wasm_exec.js";
const go = new Go();
WebAssembly.instantiateStreaming(fetch("/wasm/timestamps.wasm"), go.importObject)
.then((result) => {
go.run(result.instance);
});

91
site/timestamps/style.css Normal file
View file

@ -0,0 +1,91 @@
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
height: 100vh;
box-sizing: border-box;
display: flex;
flex-direction: column;
}
.container {
display: flex;
flex-direction: column;
height: 100%;
max-width: 1200px;
margin: 0 auto;
width: 100%;
}
h1 {
margin-top: 0;
margin-bottom: 20px;
font-size: 24px;
color: #333;
}
.controls {
margin-bottom: 15px;
display: flex;
align-items: center;
gap: 20px;
background: white;
padding: 15px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
select {
padding: 8px 12px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 4px;
background-color: white;
}
.timezone-selector {
display: flex;
gap: 15px;
}
.timezone-selector label {
display: flex;
align-items: center;
gap: 6px;
cursor: pointer;
font-size: 14px;
color: #555;
}
.editor-pane {
display: flex;
gap: 20px;
flex: 1;
min-height: 0; /* Important for nested flex scrolling */
}
textarea {
flex: 1;
padding: 15px;
border: 1px solid #ddd;
border-radius: 8px;
resize: none;
font-family: "SF Mono", "Monaco", "Inconsolata", "Fira Mono", "Droid Sans Mono", "Source Code Pro", monospace;
font-size: 13px;
line-height: 1.5;
white-space: pre;
overflow-y: auto;
background-color: white;
}
textarea:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 0 2px rgba(0,123,255,0.1);
}
#output {
background-color: #f8f9fa;
color: #333;
}

View file

@ -0,0 +1,103 @@
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.");