Mercurial > hg > config
annotate python/epoch2date.py @ 673:1b8ea91f00a0
STUB: python/epoch2date.py
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Tue, 06 May 2014 14:26:05 -0700 |
| parents | bcf5781b615e |
| children | 1e1bed921c08 |
| rev | line source |
|---|---|
| 636 | 1 #!/usr/bin/env python |
| 2 # -*- coding: utf-8 -*- | |
| 3 | |
| 667 | 4 """ |
| 5 conversion between epoch and dates | |
| 6 | |
| 7 https://docs.python.org/2/library/time.html | |
|
668
ef2762b1f7e8
note to self about fun tzinfo to fix in a year
Jeff Hammel <k0scist@gmail.com>
parents:
667
diff
changeset
|
8 https://docs.python.org/2/library/datetime.html |
| 667 | 9 """ |
| 10 | |
| 669 | 11 # TODO: tz info |
| 12 #http://bugs.python.org/issue7229 | |
| 13 # http://stackoverflow.com/questions/13218506/how-to-get-system-timezone-setting-and-pass-it-to-pytz-timezone | |
| 14 | |
| 15 | |
| 636 | 16 import argparse |
| 17 import datetime | |
| 18 import os | |
| 19 import subprocess | |
| 20 import sys | |
| 21 import time | |
| 22 | |
| 673 | 23 try: |
| 24 # use dateutil parser if available | |
| 25 from dateutil.parser import parse as parse_date | |
| 26 except ImportError: | |
| 27 parse_date = None | |
| 28 | |
| 29 | |
| 636 | 30 def main(args=sys.argv[1:]): |
| 665 | 31 """CLI""" |
| 636 | 32 |
| 665 | 33 # parse command line |
| 636 | 34 parser = argparse.ArgumentParser(description=__doc__) |
| 665 | 35 parser.add_argument('seconds_since_epoch', |
| 36 type=float, nargs='?', default=time.time(), | |
| 37 help="seconds since epoch input [DEFAULT: %(default)s]") | |
| 636 | 38 options = parser.parse_args(args) |
| 39 | |
| 665 | 40 # produce a datetime |
| 41 dt = datetime.datetime.fromtimestamp(options.seconds_since_epoch) | |
| 42 dt2 = datetime.datetime.utcfromtimestamp(options.seconds_since_epoch) | |
| 43 | |
| 44 # output | |
| 666 | 45 print ("{} seconds since epoch".format(options.seconds_since_epoch)) |
| 46 print ("{} {}".format(dt, time.tzname[time.daylight])) | |
| 47 print ("{} UTC".format(dt2)) | |
| 636 | 48 |
| 49 if __name__ == '__main__': | |
| 50 main() |
