Last active
December 6, 2018 17:51
-
-
Save ilatypov/cd561ee17e2b9b457f00dfedc92c21e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# vim: et:ts=4:sts=4:sw=4:fileencoding=utf-8 | |
ur""" | |
Parse a date/time string. | |
Usage: | |
python {script} FORMAT DATE [LOCALE] | |
Example: | |
$ python {script} "%d %B %Y" "15 mai 2017" fr_FR | |
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=135, tm_isdst=-1) | |
1494820800.0 | |
2017-05-15 00:00:00-0400 | |
$ /cygdrive/c/Python27/python {script} "%d %B %Y" "15 mai 2017" French | |
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=135, tm_isdst=-1) | |
1494820800.0 | |
2017-05-15 00:00:00-0400 | |
C:\DIR>\Python27\python {script} "%d %B %Y" "15 mai 2017" French | |
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=135, tm_isdst=-1) | |
1494820800.0 | |
2017-05-15 00:00:00-0400 | |
""" | |
import os | |
if os.name == "nt": | |
# Check if executing the Windows build of Python from a Cygwin shell. | |
if "TZ" in os.environ: | |
# The Windows build of Python (as opposed to the Cygwin one) appears | |
# confused with the TZ variable set by the Cygwin shell. The former | |
# sets time.timezone to 0, time.altzone to -3600 (-1 hr) in the | |
# presence of TZ="America/New_York", which turns the local time zone to | |
# UTC. | |
del os.environ["TZ"] | |
import time | |
import locale | |
import threading | |
from contextlib import contextmanager | |
class Usage(SystemExit): | |
def __init__(self): | |
super(Usage, self).__init__(__doc__.format(script=os.path.basename(__file__))) | |
# "How do I strftime a date object in a different locale?" | |
# https://stackoverflow.com/a/24070673/80772 by Daniel, June 5, 2014 | |
LOCALE_LOCK = threading.Lock() | |
@contextmanager | |
def setlocale(name): | |
with LOCALE_LOCK: | |
saved = locale.setlocale(locale.LC_ALL) | |
try: | |
yield locale.setlocale(locale.LC_ALL, name) | |
finally: | |
locale.setlocale(locale.LC_ALL, saved) | |
def local_timestamp(s_since_epoch): | |
t = time.localtime(s_since_epoch) | |
is_dst = time.daylight and t.tm_isdst | |
zone = time.altzone if is_dst else time.timezone | |
strtime = time.strftime("%Y-%m-%d %H:%M:%S", t) | |
utcoff = -zone | |
if utcoff > 0: | |
utcsign = "+" | |
else: | |
utcsign = "-" | |
utcoff = -utcoff | |
strtime += ("%s%02d%02d" % (utcsign, utcoff // 3600, (utcoff % 3600) // 60)) | |
return strtime | |
def parse_date_time(fmt, dt): | |
t = time.strptime(dt, fmt) | |
print t | |
return time.mktime(t) | |
if __name__ == "__main__": | |
import sys | |
args = sys.argv[1:] | |
if len(args) not in (2, 3): | |
raise Usage() | |
fmt = args[0] | |
dt = args[1] | |
if len(args) == 3: | |
loc = args[2] | |
else: | |
loc = "" | |
with setlocale(loc): | |
s_since_epoch = parse_date_time(fmt, dt) | |
print s_since_epoch | |
print local_timestamp(s_since_epoch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment