Timestamp Converter — C++ source
Convert Unix epoch timestamps to human-readable dates and back. See seconds, milliseconds, ISO 8601, UTC, local and relative time at once.
This is the C++ implementation — the same logic the interactive tool runs, in a shareable, citable form.
// timestamp — Unix epoch (seconds & milliseconds) <-> ISO 8601 / UTC / local strings and relative time ("3 hr ago" / "in 2 day"). C++ (20) port of src/tools/TimestampConverter.tsx — same logic as this dir's typescript.ts; fromISO parsing omitted for the 80-line budget (see typescript.ts / python.py).
#include <chrono>
#include <cmath>
#include <format>
#include <iostream>
using namespace std::chrono;
// Human relative time: bucket |now - t| into sec/min/hr/day/month/yr with
// the island's 60s / 60m / 24h / 30d / 365d thresholds, round to the
// nearest whole unit, then sign it ("ago" past / "in" future).
std::string relativeTime(long long ms, long long nowMs) {
long long diff = ms - nowMs;
double a = std::abs(static_cast<double>(diff)) / 1000.0;
long long n;
const char *unit;
if (a < 60) {
n = llround(a), unit = "sec";
} else if (a < 3600) {
n = llround(a / 60), unit = "min";
} else if (a < 86400) {
n = llround(a / 3600), unit = "hr";
} else if (a < 86400 * 30) {
n = llround(a / 86400), unit = "day";
} else if (a < 86400 * 365) {
n = llround(a / (86400 * 30)), unit = "month";
} else {
n = llround(a / (86400 * 365)), unit = "yr";
}
return diff < 0 ? std::format("{} {} ago", n, unit)
: std::format("in {} {}", n, unit);
}
// Render every form the tool exposes from "now" — the island's row table:
// epoch (s), epoch (ms), ISO 8601, UTC string, local string, relative time.
// std::format chrono specifiers do the rendering: floor<milliseconds> + %S
// prints "HH:MM:SS.fff" (ISO), floor<seconds> prints whole seconds (UTC).
int main() {
auto now = system_clock::now();
long long ms = duration_cast<milliseconds>(now.time_since_epoch()).count();
zoned_time local{current_zone(), now};
std::cout << std::format("Epoch (s): {}\n", ms / 1000)
<< std::format("Epoch (ms): {}\n", ms)
<< std::format("ISO 8601: {:%Y-%m-%dT%H:%M:%S}Z\n", floor<milliseconds>(now))
<< std::format("UTC: {:%a, %d %b %Y %H:%M:%S} GMT\n", floor<seconds>(now))
<< std::format("Local: {:%Y-%m-%d %H:%M:%S}\n", local)
<< std::format("Relative: {}\n", relativeTime(ms, ms))
<< std::format("In 3 hr: {}\n", relativeTime(ms + 3LL * 3600 * 1000, ms));
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →