python3 - 2to3 + miscell obvious tweaks
[sfa.git] / sfa / util / sfatime.py
1 #----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and/or hardware specification (the "Work") to
6 # deal in the Work without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Work, and to permit persons to whom the Work
9 # is furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
13 #
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
21 # IN THE WORK.
22 #----------------------------------------------------------------------
23
24
25 import time
26 import datetime
27 import dateutil.parser
28 import calendar
29 import re
30
31 from sfa.util.sfalogging import logger
32 from sfa.util.py23 import StringType
33
34 SFATIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
35
36
37 def utcparse(input):
38     """ Translate a string into a time using dateutil.parser.parse but make sure it's in UTC time and strip
39 the timezone, so that it's compatible with normal datetime.datetime objects.
40
41 For safety this can also handle inputs that are either timestamps, or datetimes
42 """
43
44     def handle_shorthands(input):
45         """recognize string like +5d or +3w or +2m as 
46         2 days, 3 weeks or 2 months from now"""
47         if input.startswith('+'):
48             match = re.match(r"([0-9]+)([dwm])", input[1:])
49             if match:
50                 how_many = int(match.group(1))
51                 what = match.group(2)
52                 if what == 'd':
53                     d = datetime.timedelta(days=how_many)
54                 elif what == 'w':
55                     d = datetime.timedelta(weeks=how_many)
56                 elif what == 'm':
57                     d = datetime.timedelta(weeks=4 * how_many)
58                 return datetime.datetime.utcnow() + d
59
60     # prepare the input for the checks below by
61     # casting strings ('1327098335') to ints
62     if isinstance(input, StringType):
63         try:
64             input = int(input)
65         except ValueError:
66             try:
67                 new_input = handle_shorthands(input)
68                 if new_input is not None:
69                     input = new_input
70             except:
71                 import traceback
72                 traceback.print_exc()
73
74     # here we go
75     if isinstance(input, datetime.datetime):
76         #logger.info ("argument to utcparse already a datetime - doing nothing")
77         return input
78     elif isinstance(input, StringType):
79         t = dateutil.parser.parse(input)
80         if t.utcoffset() is not None:
81             t = t.utcoffset() + t.replace(tzinfo=None)
82         return t
83     elif isinstance(input, (int, float)):
84         return datetime.datetime.fromtimestamp(input)
85     else:
86         logger.error("Unexpected type in utcparse [%s]" % type(input))
87
88
89 def datetime_to_string(dt):
90     return datetime.datetime.strftime(dt, SFATIME_FORMAT)
91
92
93 def datetime_to_utc(dt):
94     return time.gmtime(datetime_to_epoch(dt))
95
96 # see https://docs.python.org/2/library/time.html
97 # all timestamps are in UTC so time.mktime() would be *wrong*
98
99
100 def datetime_to_epoch(dt):
101     return int(calendar.timegm(dt.timetuple()))
102
103
104 def add_datetime(input, days=0, hours=0, minutes=0, seconds=0):
105     """
106     Adjust the input date by the specified delta (in seconds).
107     """
108     dt = utcparse(input)
109     return dt + datetime.timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)
110
111 if __name__ == '__main__':
112         # checking consistency
113     print(20 * 'X')
114     print(("Should be close to zero: %s" %
115            (datetime_to_epoch(datetime.datetime.utcnow()) - time.time())))
116     print(20 * 'X')
117     for input in [
118             '+2d',
119             '+3w',
120             '+2m',
121             1401282977.575632,
122             1401282977,
123             '1401282977',
124             '2014-05-28',
125             '2014-05-28T15:18',
126             '2014-05-28T15:18:30',
127     ]:
128         print("input=%20s -> parsed %s" %
129               (input, datetime_to_string(utcparse(input))))