annotate python/epoch2date.py @ 674:df9d5093573e

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