65 lines
1.1 KiB
Python
Executable file
65 lines
1.1 KiB
Python
Executable file
#! /usr/bin/env python3
|
|
# encoding: utf-8
|
|
|
|
usage = """
|
|
Converts between various subsets of ISO 8601 datetimes and unix timestamps.
|
|
|
|
Usage:
|
|
|
|
→ current unix time
|
|
$ t2t
|
|
1609849499
|
|
|
|
→ convert unix time to ISO 8601
|
|
$ t2t 590055912
|
|
1988-09-12 10:25:12
|
|
|
|
→ convert ISO 8601 subsets into unix time
|
|
$ t2t 1988-09-12
|
|
590018400
|
|
|
|
$ t2t 1988-09-12 10:25
|
|
590055900
|
|
|
|
$ t2t 1988-09-12 10:25:12
|
|
590055912
|
|
""".strip()
|
|
|
|
import sys
|
|
import datetime
|
|
|
|
targets = sys.argv[1:]
|
|
l = len(targets)
|
|
|
|
if l == 0:
|
|
# no args
|
|
D = datetime.datetime.now()
|
|
print(int(D.timestamp()))
|
|
|
|
elif l == 1:
|
|
if targets[0].isdigit():
|
|
# time_t to dt
|
|
D = datetime.datetime.fromtimestamp(int(targets[0]))
|
|
print(D.strftime("%Y-%m-%d %H:%M:%S"))
|
|
else:
|
|
try:
|
|
D = datetime.datetime.strptime(targets[0], "%Y-%m-%d")
|
|
except ValueError:
|
|
print(usage)
|
|
sys.exit(1)
|
|
|
|
print(int(D.timestamp()))
|
|
|
|
elif l == 2:
|
|
if targets[1].count(":") == 2:
|
|
fmt = "%Y-%m-%d %H:%M:%S"
|
|
else:
|
|
fmt = "%Y-%m-%d %H:%M"
|
|
|
|
try:
|
|
D = datetime.datetime.strptime(" ".join(targets), fmt)
|
|
except ValueError:
|
|
print(usage)
|
|
sys.exit(1)
|
|
|
|
print(int(D.timestamp()))
|