add t2t script
This commit is contained in:
parent
2950d78b51
commit
5bff961095
1 changed files with 65 additions and 0 deletions
65
bin/t2t
Executable file
65
bin/t2t
Executable file
|
@ -0,0 +1,65 @@
|
|||
#! /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()))
|
Loading…
Add table
Add a link
Reference in a new issue