Mercurial > hg > config
annotate python/epoch2date.py @ 806:5364185dcb9b
i dont use this anywhere yet; lets assume that i wont until i actually will be able to develop on it
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 28 Oct 2016 17:07:24 -0700 |
parents | 7c4ee496794c |
children |
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', |
676
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
36 type=float, nargs='*', default=time.time(), |
665 | 37 help="seconds since epoch input [DEFAULT: %(default)s]") |
676
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
38 parser.add_argument('--utc', dest='display_utc', |
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
39 action='store_true', default=False, |
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
40 help="display UTC time only") |
636 | 41 options = parser.parse_args(args) |
42 | |
676
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
43 if isinstance(options.seconds_since_epoch, float): |
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
44 options.seconds_since_epoch = [ options.seconds_since_epoch ] |
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
45 |
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
46 for s in options.seconds_since_epoch: |
665 | 47 |
676
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
48 # produce a datetime |
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
49 dt = datetime.datetime.fromtimestamp(s) |
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
50 dt2 = datetime.datetime.utcfromtimestamp(s) |
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
51 |
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
52 # output |
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
53 if not options.display_utc: |
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
54 print ("{} seconds since epoch".format(s)) |
719 | 55 if time.daylight and time.localtime(s).tm_isdst: |
56 tz_index = 1 | |
57 else: | |
58 tz_index = 0 | |
59 print ("{} {}".format(dt, time.tzname[tz_index])) | |
676
1e1bed921c08
allow multiple date ranges and display of UTC only
Jeff Hammel <k0scist@gmail.com>
parents:
673
diff
changeset
|
60 print ("{} UTC".format(dt2)) |
636 | 61 |
62 if __name__ == '__main__': | |
63 main() |