annotate python/parse_date.py @ 835:891c38647775

-> http://k0s.org/hg/hq/
author Jeff Hammel <k0scist@gmail.com>
date Sat, 25 Feb 2017 13:10:40 -0800
parents ca1f21d9e621
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
716
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 parse date
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 # imports
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import calendar
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import datetime
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import sys
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 import time
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 from .formatting import format_table
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 from dateutil.parser import parse
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 __all__ = ['main', 'parse_date', 'epoch2local', 'epoch2utc', 'is_dst', 'timezone']
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 def is_dst(localtime=None):
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 """returns if daylight savings time is in effect locally"""
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 return time.localtime(localtime).tm_isdst > 0
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 def timezone(localtime=None):
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 """returns name of local timezone"""
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 return time.tzname[int(is_dst(localtime))]
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 def epoch2local(datestamp):
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 """convert epoch to local time"""
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 return datetime.datetime.fromtimestamp(float(datestamp))
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 def epoch2utc(datestamp):
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 """convert epoch to UTC"""
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 return datetime.datetime.utcfromtimestamp(float(datestamp))
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 def parse_date(datestamp, utc=False):
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 """returns seconds since epoch from the supplied date"""
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 try:
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 # already epoch timestamp
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 return float(datestamp)
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 except ValueError:
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 pass
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 # parse the string
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 parsed_date = parse(datestamp)
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 # convert this to seconds since epoch
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 if utc:
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 return float(calendar.timegm(parsed_date.timetuple()))
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 else:
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 return time.mktime(parsed_date.timetuple())
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 def main(args=sys.argv[1:]):
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 # parse command line
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 parser = argparse.ArgumentParser(description=__doc__)
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 parser.add_argument('date', nargs='+',
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 help="local date to parse")
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66 parser.add_argument('--utc', dest='utc',
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 action='store_true', default=False,
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 help="indicate date is in UTC")
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69 options = parser.parse_args(args)
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 # parse each date
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 epochs = [parse_date(d, options.utc) for d in options.date]
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 # display results
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 header = ['epoch', 'local', 'UTC']
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76 print (format_table([[d, '{} {}'.format(epoch2local(d), timezone(d)), epoch2utc(d)] for d in epochs],
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77 header=header, joiner='|'))
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 if __name__ == '__main__':
ca1f21d9e621 parse date example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81 main()