annotate python/epoch2date.py @ 924:fc88a12acf48 default tip

add lfs filter
author Jeff Hammel <k0scist@gmail.com>
date Wed, 04 Dec 2024 15:50:53 -0800
parents 7c4ee496794c
children
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',
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
Jeff Hammel <k0scist@gmail.com>
parents: 636
diff changeset
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
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 options = parser.parse_args(args)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
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
Jeff Hammel <k0scist@gmail.com>
parents: 636
diff changeset
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
Jeff Hammel <k0scist@gmail.com>
parents: 676
diff changeset
55 if time.daylight and time.localtime(s).tm_isdst:
Jeff Hammel <k0scist@gmail.com>
parents: 676
diff changeset
56 tz_index = 1
Jeff Hammel <k0scist@gmail.com>
parents: 676
diff changeset
57 else:
Jeff Hammel <k0scist@gmail.com>
parents: 676
diff changeset
58 tz_index = 0
Jeff Hammel <k0scist@gmail.com>
parents: 676
diff changeset
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
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 if __name__ == '__main__':
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 main()