Package eoxserver :: Package core :: Package util :: Module timetools
[hide private]
[frames] | no frames]

Source Code for Module eoxserver.core.util.timetools

  1  #------------------------------------------------------------------------------- 
  2  # $Id: timetools.py 1527 2012-03-14 15:46:56Z schindlerf $ 
  3  # 
  4  # Project: EOxServer <http://eoxserver.org> 
  5  # Authors: Stephan Krause <stephan.krause@eox.at> 
  6  #          Stephan Meissl <stephan.meissl@eox.at> 
  7  # 
  8  #------------------------------------------------------------------------------- 
  9  # Copyright (C) 2011 EOX IT Services GmbH 
 10  # 
 11  # Permission is hereby granted, free of charge, to any person obtaining a copy 
 12  # of this software and associated documentation files (the "Software"), to deal 
 13  # in the Software without restriction, including without limitation the rights 
 14  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 15  # copies of the Software, and to permit persons to whom the Software is  
 16  # furnished to do so, subject to the following conditions: 
 17  # 
 18  # The above copyright notice and this permission notice shall be included in all 
 19  # copies of this Software or works derived from this Software. 
 20  # 
 21  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 22  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 23  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 24  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 25  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 26  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 27  # THE SOFTWARE. 
 28  #------------------------------------------------------------------------------- 
 29   
 30  import re 
 31  from datetime import datetime, tzinfo, timedelta 
 32   
 33  from eoxserver.core.exceptions import InvalidParameterException 
 34   
 35  # pre-compile the regular expression for date/time matching 
 36   
 37  date_regex = r"(?P<year>\d{4})[-]?(?P<month>\d{2})[-]?(?P<day>\d{2})" 
 38  time_regex = r"(?P<hour>\d{2})(:?(?P<minute>\d{2})(:?(?P<second>\d{2}))?)?" 
 39  tz_regex = r"(?P<tz_expr>Z|(?P<tz_sign>[+-])(?P<tz_hours>\d{2}):?(?P<tz_minutes>\d{2})?)?" 
 40  datetime_regex = date_regex + r"(T" + time_regex + tz_regex + ")?" 
 41   
 42  datetime_regex_obj = re.compile(datetime_regex) 
 43   
44 -class UTCOffsetTimeZoneInfo(tzinfo):
45 - def __init__(self):
46 super(UTCOffsetTimeZoneInfo, self).__init__ 47 48 self.offset_td = timedelta()
49
50 - def setOffsets(self, offset_sign, offset_hours, offset_minutes):
51 if offset_sign == "+": 52 self.offset_td = timedelta(hours = offset_hours, minutes = offset_minutes) 53 else: 54 self.offset_td = timedelta(hours = -offset_hours, minutes = -offset_minutes)
55
56 - def utcoffset(self, dt):
57 return self.offset_td
58
59 - def dst(self, dt):
60 return timedelta()
61
62 - def tzname(self, dt):
63 return None
64
65 -def _convert(s):
66 if s is None: 67 return 0 68 else: 69 return int(s)
70
71 -def getDateTime(s):
72 match = datetime_regex_obj.match(s) 73 if match is None: 74 raise InvalidParameterException( 75 "'%s' does not match any known datetime format." % s 76 ) 77 78 year = int(match.group("year")) 79 month = int(match.group("month")) 80 day = int(match.group("day")) 81 82 hour = _convert(match.group("hour")) 83 minute = _convert(match.group("minute")) 84 second = _convert(match.group("second")) 85 86 if match.group("tz_expr") in (None, "Z"): 87 offset_sign = "+" 88 offset_hours = 0 89 offset_minutes = 0 90 else: 91 offset_sign = match.group("tz_sign") 92 offset_hours = _convert(match.group("tz_hours")) 93 offset_minutes = _convert(match.group("tz_minutes")) 94 95 tzi = UTCOffsetTimeZoneInfo() 96 tzi.setOffsets(offset_sign, offset_hours, offset_minutes) 97 98 try: 99 dt = datetime(year, month, day, hour, minute, second, 0, tzi) 100 except ValueError: 101 raise InvalidParameterException("Invalid date/time '%s'" % s) 102 103 utc = UTCOffsetTimeZoneInfo() 104 utct = dt.astimezone(utc) 105 106 return utct
107
108 -def isotime(dt):
109 return dt.strftime("%Y-%m-%dT%H:%M:%SZ")
110