-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-epoch.py
More file actions
81 lines (70 loc) · 2.54 KB
/
simple-epoch.py
File metadata and controls
81 lines (70 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Convert between Unix timestamps and human-readable dates."""
import sys
import argparse
from datetime import datetime, timezone, timedelta
def parse_offset(s: str) -> timezone:
"""Parse UTC offset like +03:00, -05:00, or 0."""
s = s.strip()
if s in ("0", "UTC", "utc"):
return timezone.utc
sign = 1 if s.startswith("+") else -1
s = s.lstrip("+-")
parts = s.split(":")
hours = int(parts[0])
minutes = int(parts[1]) if len(parts) > 1 else 0
return timezone(timedelta(hours=sign * hours, minutes=sign * minutes))
def timestamp_to_dt(ts: float, tz: timezone) -> str:
dt = datetime.fromtimestamp(ts, tz=tz)
return dt.strftime("%Y-%m-%d %H:%M:%S %Z")
def dt_to_timestamp(s: str, tz: timezone) -> float:
for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%d"):
try:
dt = datetime.strptime(s, fmt).replace(tzinfo=tz)
return dt.timestamp()
except ValueError:
continue
raise ValueError(f"Unrecognized date format: {s!r}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert Unix timestamps to dates and vice versa."
)
parser.add_argument(
"value",
nargs="?",
help="Unix timestamp or date string (YYYY-MM-DD [HH:MM[:SS]]). Omit for current time.",
)
parser.add_argument(
"-z",
"--timezone",
default="+00:00",
metavar="OFFSET",
help="UTC offset, e.g. +03:00 or -05:00 (default: +00:00)",
)
parser.add_argument(
"--ms", action="store_true", help="Input timestamp is in milliseconds"
)
args = parser.parse_args()
try:
tz = parse_offset(args.timezone)
except Exception:
print(f"Invalid timezone offset: {args.timezone!r}", file=sys.stderr)
sys.exit(1)
if args.value is None:
now = datetime.now(tz=timezone.utc)
local = datetime.now(tz=tz)
print(f"UTC timestamp : {int(now.timestamp())}")
print(f"UTC ms : {int(now.timestamp() * 1000)}")
print(f"UTC date : {now.strftime('%Y-%m-%d %H:%M:%S UTC')}")
print(f"Local date : {local.strftime('%Y-%m-%d %H:%M:%S %Z')}")
elif args.value.lstrip("-+").replace(".", "").isdigit():
ts = float(args.value)
if args.ms:
ts /= 1000
print(timestamp_to_dt(ts, tz))
else:
try:
ts = dt_to_timestamp(args.value, tz)
print(f"{int(ts)}")
except ValueError as e:
print(e, file=sys.stderr)
sys.exit(1)