Python · Inline
Unix Timestamp
Converts between Unix timestamps and dates. Leave it empty for the current time, enter a timestamp (seconds or milliseconds) or a date like 2026-03-01 14:30.
- Language
- Python
- Output
- Inline
- Keyword
epoch- Arguments
- Timestamp or date (optional) (optional)
- Licence
- MIT
- Author
- Docktopus
- File
- unix-timestamp.py (1.7 KB)
- Downloads
- 0
In Docktopus: Settings → Scripts → Browse Community Scripts. Read the source before you run any script.
Source
unix-timestamp.py · sha256 b774b248d462…#!/usr/bin/env python3
# @docktopus.schemaVersion 1
# @docktopus.title Unix Timestamp
# @docktopus.mode inline
# @docktopus.icon clock
# @docktopus.keyword epoch
# @docktopus.description Converts between Unix timestamps and dates. Leave it empty for the current time, enter a timestamp (seconds or milliseconds) or a date like 2026-03-01 14:30.
# @docktopus.argument1 {"type": "text", "placeholder": "Timestamp or date (optional)", "optional": true}
# @docktopus.author Docktopus
import sys
from datetime import datetime, timezone
value = sys.argv[1].strip() if len(sys.argv) > 1 else ""
def describe(moment: datetime) -> None:
local = moment.astimezone()
print(f"Unix: {int(moment.timestamp())}")
print(f"Milliseconds: {int(moment.timestamp() * 1000)}")
print(f"Local: {local:%Y-%m-%d %H:%M:%S %Z}")
print(f"UTC: {moment.astimezone(timezone.utc):%Y-%m-%dT%H:%M:%SZ}")
if not value:
describe(datetime.now(timezone.utc))
elif value.lstrip("-").replace(".", "", 1).isdigit():
number = float(value)
# Thirteen digits or more is almost always milliseconds.
if abs(number) >= 1e12:
number /= 1000
describe(datetime.fromtimestamp(number, timezone.utc))
else:
for pattern in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d", "%d.%m.%Y %H:%M", "%d.%m.%Y"):
try:
parsed = datetime.strptime(value.removesuffix("Z"), pattern)
except ValueError:
continue
zone = timezone.utc if value.endswith("Z") else None
describe(parsed.replace(tzinfo=zone) if zone else parsed.astimezone())
break
else:
sys.exit(f"“{value}” is not a timestamp or a date like 2026-03-01 14:30.")