我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用django.utils.formats.time_format()。
def get_display_date(self): tz = timezone.get_current_timezone() start_time = self.start_time.astimezone(tz) end_time = self.end_time.astimezone(tz) if start_time.date() == end_time.date(): date = formats.date_format(start_time, 'DATE_FORMAT') return _("le {date}, de {start_hour} à {end_hour}").format( date=date, start_hour=formats.time_format(start_time, 'TIME_FORMAT'), end_hour=formats.time_format(end_time, 'TIME_FORMAT') ) return _("du {start_date}, {start_time} au {end_date}, {end_time}").format( start_date=formats.date_format(start_time, 'DATE_FORMAT'), start_time=formats.date_format(start_time, 'TIME_FORMAT'), end_date=formats.date_format(end_time, 'DATE_FORMAT'), end_time=formats.date_format(end_time, 'TIME_FORMAT'), )
def time(value, arg=None): """Formats a time according to the given format.""" if value in (None, ''): return '' if arg is None: arg = settings.TIME_FORMAT try: return formats.time_format(value, arg) except AttributeError: try: return time_format(value, arg) except AttributeError: return ''
def time(value, arg=None): """Formats a time according to the given format.""" if value in (None, ''): return '' try: return formats.time_format(value, arg) except (AttributeError, TypeError): try: return time_format(value, arg) except (AttributeError, TypeError): return ''
def update_data(self): # TODO: handle whole day events, display multiple events now = timezone.now() tonight = now.replace(hour=23, minute=59) self.dtstart = None self.summary = None calendar = Calendar() r = requests.get(self.ics_url) calendar = calendar.from_ical(r.text) for event in calendar.walk(): if event.get('dtstart') and event.get('dtend'): dtstart = event.get('dtstart').dt dtend = event.get('dtend').dt if not isinstance(dtstart, datetime) or not isinstance(dtend, datetime): dtstart = datetime.fromordinal(dtstart.toordinal()) dtend = datetime.fromordinal(dtend.toordinal()) try: dtstart = timezone.localtime(dtstart, timezone.utc) dtend = timezone.localtime(dtend, timezone.utc) except ValueError: dtstart = dtstart.replace(tzinfo=timezone.utc) dtend = dtend.replace(tzinfo=timezone.utc) if now <= dtend <= tonight: dtstart = timezone.localtime(dtstart, pytz.timezone('Europe/Paris')) self.dtstart = formats.time_format(dtstart, 'TIME_FORMAT') self.summary = str(unidecode.unidecode(event.get('summary'))) self.save()
def update_data(self): now = timezone.localtime(timezone.now(), self.timezone) self.time = formats.time_format(now, 'TIME_FORMAT') self.date = formats.date_format(now, 'SHORT_DATE_FORMAT') self.save()