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"). Language: C (C11, stdlib only). Port of src/tools/TimestampConverter.tsx — same logic as this dir's typescript.ts; ISO milliseconds and fromISO parsing omitted for the 80-line budget (see typescript.ts / python.py). */
#include <math.h>
#include <stdio.h>
#include <time.h>
/* 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). */
static void relative_time(long long ms, long long now_ms, char out[40]) {
long long diff = ms - now_ms;
double a = fabs((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";
}
if (diff < 0)
snprintf(out, 40, "%lld %s ago", n, unit);
else
snprintf(out, 40, "in %lld %s", n, unit);
}
/* Render every form the tool exposes from epoch seconds — the island's row
table: epoch (s), epoch (ms), ISO 8601, UTC string, local string, and
relative time. %a/%b are weekday/month names (English in the C locale,
matching toUTCString()). */
int main(void) {
time_t now = time(NULL);
long long secs = (long long)now, ms = secs * 1000;
char iso[32], utc[40], local[40], rel[40];
struct tm tm_utc = *gmtime(&now), tm_loc = *localtime(&now);
strftime(iso, sizeof iso, "%Y-%m-%dT%H:%M:%SZ", &tm_utc);
strftime(utc, sizeof utc, "%a, %d %b %Y %H:%M:%S GMT", &tm_utc);
strftime(local, sizeof local, "%Y-%m-%d %H:%M:%S", &tm_loc);
relative_time(ms, ms, rel);
printf("Epoch (s): %lld\n", secs);
printf("Epoch (ms): %lld\n", ms);
printf("ISO 8601: %s\n", iso);
printf("UTC: %s\n", utc);
printf("Local: %s\n", local);
printf("Relative: %s\n", rel);
relative_time(ms + 3LL * 3600 * 1000, ms, rel); /* future demo */
printf("In 3 hr: %s\n", rel);
return 0;
}
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 →