def test_DT(self): # Python datetime with valid zone. Zope converts it to GMT+1... # TODO: DateTime better shouldn't do this! cet = pytz.timezone('CET') self.assertEqual(DT(datetime(2011, 11, 11, 11, 0, 0, tzinfo=cet)), DateTime('2011/11/11 11:00:00 GMT+1')) # Python dates get converted to a DateTime with timecomponent including # a timezone self.assertEqual(DT(date(2011, 11, 11)), DateTime('2011/11/11 00:00:00 UTC')) # DateTime with valid zone self.assertEqual(DT(DateTime(2011, 11, 11, 11, 0, 0, 'Europe/Vienna')), DateTime('2011/11/11 11:00:00 Europe/Vienna')) # Zope DateTime with valid DateTime zone but invalid pytz is kept as is self.assertEqual(DT(DateTime(2011, 11, 11, 11, 0, 0, 'GMT+1')), DateTime('2011/11/11 11:00:00 GMT+1')) # Invalid datetime zones are converted to the portal timezone # Testing with no timezone self.assertEqual(DT(datetime(2011, 11, 11, 11, 0, 0)), DateTime('2011/11/11 11:00:00 UTC')) # Conversion from string self.assertEqual(DT('2011/11/11 11:00:00 Europe/Vienna'), DateTime('2011/11/11 11:00:00 Europe/Vienna')) # TEST WITH/WITHOUT MICROSECONDS # From Python datetime tz = pytz.timezone('Europe/Vienna') # exact=False self.assertEqual( DT(datetime(2012, 12, 12, 10, 10, 10, 123456, tzinfo=tz), exact=False), DateTime('2012/12/12 10:10:10 Europe/Vienna')) # exact=True self.assertEqual( DT(datetime(2012, 12, 12, 10, 10, 10, 123456, tzinfo=tz), exact=True), DateTime('2012/12/12 10:10:10.123456 Europe/Vienna')) # From Zope DateTime # Exact=False self.assertEqual( DT(DateTime(2012, 12, 12, 10, 10, 10.123456, 'Europe/Vienna'), exact=False), DateTime('2012/12/12 10:10:10 Europe/Vienna')) # Exact=True self.assertEqual( DT(DateTime(2012, 12, 12, 10, 10, 10.123456, 'Europe/Vienna'), exact=True), DateTime('2012/12/12 10:10:10.123456 Europe/Vienna'))
def ticket_title_generator(obj): """Generate a title for the ticket, also using event information. """ event = obj ret = { 'title': obj.title, 'eventtitle': '', 'eventstart': '', 'eventend': '' } if ITicketOccurrence.providedBy(event): event = aq_parent(aq_parent(event)) # Traverse to the Occurrence object if IATEvent.providedBy(event): # get the request out of thin air to be able to publishTraverse to # the transient Occurrence object. traverser = OccTravAT(event, getRequest()) elif IDXEvent.providedBy(event): # TODO traverser = OccTravDX(event, getRequest()) else: raise NotImplementedError( u"There is no event occurrence traverser implementation for " u"this kind of object.") try: event = traverser.publishTraverse(getRequest(), obj.id) except KeyError: # Maybe the ticket occurrence isn't valid anymore because the # event occurence doesn't exist anymore. # Just ignore that case. return ret elif ITicket.providedBy(event): event = aq_parent(event) if IEvent.providedBy(event) or IOccurrence.providedBy(event): acc = IEventAccessor(event) lstart = ulocalized_time(DT(acc.start), long_format=True, context=event) lend = ulocalized_time(DT(acc.start), long_format=True, context=event) # XXX: no unicode, store as utf-8 encoded string instead ret = dict( title=u'%s - %s (%s - %s)' % ( safe_unicode(acc.title), safe_unicode(obj.title), lstart, lend, ), eventtitle=acc.title, eventstart=acc.start, eventend=acc.end, ) return ret
def _dt_setter(self, fieldtoset, value, **kwargs): """Always set the date in UTC, saving the timezone in another field. But since the timezone value isn't known at the time of saving the form, we have to save it timezone-naive first and let timezone_handler convert it to the target zone afterwards. """ # Note: The name of the first parameter shouldn't be field, because # it's already in kwargs in some case. if not isinstance(value, DateTime): value = DT(value) # This way, we set DateTime timezoneNaive value = DateTime( "%04d-%02d-%02dT%02d:%02d:%02d" % ( value.year(), value.month(), value.day(), value.hour(), value.minute(), int(value.second()), # No microseconds ) ) self.getField(fieldtoset).set(self, value, **kwargs)
def test_default_start_DT(self): DTS = default_start_DT() DTN = DT(localized_now()) self.assertTrue(DTS.year() == DTN.year() and DTS.month() == DTN.month() and DTS.day() == DTN.day() and DTS.hour() == DTN.hour() and DTS.minute() == DTN.minute())
def test_default_end_DT(self): DTE = default_end_DT() DTN = DT(localized_now() + datetime.timedelta(hours=DEFAULT_END_DELTA)) self.assertTrue(DTE.year() == DTN.year() and DTE.month() == DTN.month() and DTE.day() == DTN.day() and DTE.hour() == DTN.hour() and DTE.minute() == DTN.minute())
def _dt_setter(self, fieldtoset, value, **kwargs): """Always set the date in UTC, saving the timezone in another field. But since the timezone value isn't known at the time of saving the form, we have to save it timezone-naive first and let timezone_handler convert it to the target zone afterwards. """ # Note: The name of the first parameter shouldn't be field, because # it's already in kwargs in some case. if not isinstance(value, DateTime): value = DT(value) # This way, we set DateTime timezoneNaive value = DateTime('%04d-%02d-%02dT%02d:%02d:%02d' % ( value.year(), value.month(), value.day(), value.hour(), value.minute(), int(value.second()) # No microseconds )) self.getField(fieldtoset).set(self, value, **kwargs)
def last_modified(self, value): tz = default_timezone(self.context, as_tzinfo=True) mod = DT(pydt(value, missing_zone=tz)) setattr(self.context, 'modification_date', mod)
def default_end(): return DT(default_end_dt())
def default_start(): return DT(default_start_dt())
def end_indexer(obj): event = IEventBasic(obj) if event.end is None: return None return DT(event.end)
def start_indexer(obj): event = IEventBasic(obj) if event.start is None: return None return DT(event.start)
def dates_for_display_user_timezone(occurrence): """ Return a dictionary containing pre-calculated information for building <start>-<end> date strings. Keys are: 'start_date' - date string of the start date 'start_time' - time string of the start date 'end_date' - date string of the end date 'end_time' - time string of the end date 'start_iso' - start date in iso format 'end_iso' - end date in iso format 'same_day' - event ends on the same day 'same_time' - event ends at same time 'whole_day' - whole day events 'open_end' - events without end time :param occurrence: Event or occurrence object. :type occurrence: IEvent, IOccurrence or IEventAccessor based object. :returns: Dictionary with date strings. :rtype: dict The behavior os ulocalized_time() with time_only is odd. Setting time_only=False should return the date part only and *not* the time NOTE: these tests are not run, but serve as documentation. TODO: remove. >>> from DateTime import DateTime >>> start = DateTime(2010,3,16,14,40) >>> from zope.component.hooks import getSite >>> site = getSite() >>> ulocalized_time(start, False, time_only=True, context=site) u'14:40' >>> ulocalized_time(start, False, time_only=False, context=site) u'14:40' >>> ulocalized_time(start, False, time_only=None, context=site) u'16.03.2010' """ if IEventAccessor.providedBy(occurrence): acc = occurrence occurrence = occurrence.context else: acc = IEventAccessor(occurrence) if acc.start is None or acc.end is None: # Eventually optional start/end dates from a potentially Event. return None timezone = getUserPytzTimezone() start = acc.start.astimezone(timezone) end = acc.end.astimezone(timezone) # this needs to separate date and time as ulocalized_time does DT_start = DT(start) DT_end = DT(end) current_user = api.user.get_current() try: format_time = current_user.getProperty('format_time') except: format_time = '' start_date = ulocalized_time( DT_start, long_format=False, time_only=None, context=occurrence ) start_time = ulocalized_time( DT_start, long_format=False, time_only=True, context=occurrence ) end_date = ulocalized_time( DT_end, long_format=False, time_only=None, context=occurrence ) end_time = ulocalized_time( DT_end, long_format=False, time_only=True, context=occurrence ) same_day = is_same_day(start, end) same_time = is_same_time(start, end) # set time fields to None for whole day events if acc.whole_day: start_time = end_time = None if acc.open_end: end_time = None start_iso = acc.whole_day and start.date().isoformat()\ or start.isoformat() end_iso = acc.whole_day and end.date().isoformat()\ or end.isoformat() if format_time != None and format_time != '': if start_time != None: if 'PM' in start_time or 'AM' in start_time or 'pm' in start_time or 'am' in start_time: DT_start_time = datetime.strptime(str(start_time), '%I:%M %p') else: DT_start_time = datetime.strptime(str(start_time), '%H:%M') if 'hh:i A' in format_time: start_time = DT_start_time.strftime('%I:%M %p') else: start_time = DT_start_time.strftime('%H:%M') if end_time != None: if 'PM' in end_time or 'AM' in end_time or 'pm' in end_time or 'am' in end_time: DT_end_time = datetime.strptime(str(end_time), '%I:%M %p') else: DT_end_time = datetime.strptime(str(end_time), '%H:%M') if 'hh:i A' in format_time: end_time = DT_end_time.strftime('%I:%M %p') else: end_time = DT_end_time.strftime('%H:%M') return dict( # Start start_date=start_date, start_time=start_time, start_iso=start_iso, # End end_date=end_date, end_time=end_time, end_iso=end_iso, # Meta same_day=same_day, same_time=same_time, whole_day=acc.whole_day, open_end=acc.open_end, )
def get_future_events(self): """Customize which properties we want to show in pt.""" events = [] ts = api.portal.get_tool(name='translation_service') results = self._query_future_events() timezone = getUserPytzTimezone() for event in results: start = event.start.astimezone(timezone) end = event.end.astimezone(timezone) if event.portal_type == 'Occurrence': ocurrence = event event = IEventAccessor(ocurrence) event_url = event.url else: event_url = event.absolute_url() current_user = api.user.get_current() try: format_time = current_user.getProperty('format_time') except: format_time = '' DT_start = DT(start) if event.whole_day: start_time = '' else: start_time = ulocalized_time(DT_start, long_format=False, time_only=True, context=event) if format_time != None and format_time != '': if start_time != None: if 'PM' in start_time or 'AM' in start_time or 'pm' in start_time or 'am' in start_time: DT_start_time = datetime.strptime( str(start_time), '%I:%M %p') else: DT_start_time = datetime.strptime( str(start_time), '%H:%M') if 'hh:i A' in format_time: start_time = DT_start_time.strftime('%I:%M %p') else: start_time = DT_start_time.strftime('%H:%M') description = abrevia(event.description, 100) if event.description else None location = event.location if event.location else None info = { 'url': event_url, 'firstday': start.day, 'firstmonth': PLMF(ts.month_msgid(start.month)), 'abbrfirstmonth': PLMF(ts.month_msgid(start.month)), 'firstyear': start.year, 'lastday': end.day, 'lastmonth': PLMF(ts.month_msgid(end.month)), 'abbrlastmonth': PLMF(ts.month_msgid(end.month)), 'lastyear': end.year, 'title': abrevia(event.title, 60), 'descr': description, 'location': location, 'timezone': event.timezone, 'showflip': location or description, 'starttime': start_time } events.append(info) return events