def deserialize_dt(text): """ Deserialize a rfc2445 text to a datetime. The setting RECURRENCE_USE_TZ determines if a naive or timezone aware datetime is returned. If this setting is not present the setting USE_TZ is used as a default. This setting is accessed via the `.settings.deserialize_tz` function. """ try: year, month, day = int(text[:4]), int(text[4:6]), int(text[6:8]) except ValueError: raise exceptions.DeserializationError('malformed date-time: %r' % text) if u'T' in text: # time is also specified try: hour, minute, second = (int(text[9:11]), int(text[11:13]), int(text[13:15])) except ValueError: raise exceptions.DeserializationError( 'malformed date-time: %r' % text) else: # only date is specified, use midnight hour, minute, second = (0, 0, 0) if u'Z' in text: # time is in utc tzinfo = pytz.utc else: # right now there is no support for VTIMEZONE/TZID since # this is a partial implementation of rfc2445 so we'll # just use the time zone specified in the Django settings. tzinfo = localtz() dt = datetime.datetime(year, month, day, hour, minute, second, tzinfo=tzinfo) if settings.deserialize_tz(): return dt dt = dt.astimezone(localtz()) # set tz to settings.TIME_ZONE and return offset-naive datetime return datetime.datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
def deserialize_dt(text): """ Deserialize a rfc2445 text to a datetime. The setting RECURRENCE_USE_TZ determines if a naive or timezone aware datetime is returned. If this setting is not present the setting USE_TZ is used as a default. This setting is accessed via the `.settings.deserialize_tz` function. """ try: year, month, day = int(text[:4]), int(text[4:6]), int(text[6:8]) except ValueError: raise exceptions.DeserializationError('malformed date-time: %r' % text) if u'T' in text: # time is also specified try: hour, minute, second = ( int(text[9:11]), int(text[11:13]), int(text[13:15])) except ValueError: raise exceptions.DeserializationError('malformed date-time: %r' % text) else: # only date is specified, use midnight hour, minute, second = (0, 0, 0) if u'Z' in text: # time is in utc tzinfo = pytz.utc else: # right now there is no support for VTIMEZONE/TZID since # this is a partial implementation of rfc2445 so we'll # just use the time zone specified in the Django settings. tzinfo = localtz() dt = datetime.datetime( year, month, day, hour, minute, second, tzinfo=tzinfo) if settings.deserialize_tz(): return dt dt = dt.astimezone(localtz()) # set tz to settings.TIME_ZONE and return offset-naive datetime return datetime.datetime( dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
def test_fallback_to_use_tz_false(self): del settings.RECURRENCE_USE_TZ assert not r_settings.deserialize_tz()
def test_recurrence_tz_false(self): assert not r_settings.deserialize_tz()
def test_recurrence_tz_true(self): assert r_settings.deserialize_tz()