我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用django.utils.dateparse.parse_time()。
def to_internal_value(self, value): input_formats = getattr(self, 'input_formats', api_settings.TIME_INPUT_FORMATS) if isinstance(value, datetime.time): return value for input_format in input_formats: if input_format.lower() == ISO_8601: try: parsed = parse_time(value) except (ValueError, TypeError): pass else: if parsed is not None: return parsed else: try: parsed = self.datetime_parser(value, input_format) except (ValueError, TypeError): pass else: return parsed.time() humanized_format = humanize_datetime.time_formats(input_formats) self.fail('invalid', format=humanized_format)
def deserialize_instance(model, data): ret = model() for k, v in data.items(): if v is not None: try: f = model._meta.get_field(k) if isinstance(f, DateTimeField): v = dateparse.parse_datetime(v) elif isinstance(f, TimeField): v = dateparse.parse_time(v) elif isinstance(f, DateField): v = dateparse.parse_date(v) elif isinstance(f, BinaryField): v = force_bytes( base64.b64decode( force_bytes(v))) except FieldDoesNotExist: pass setattr(ret, k, v) return ret
def parse_schedule_item(self, item, date): """ Make single schedule for t.cast channel. Return single schedule dictionary. :param item: <div class="con active"> :param date: <strong>05:30</strong> :return schedule of movie """ schedule = dict() start_time = dateparse.parse_time(item.find('strong').text.strip()) start_datetime = date.replace(hour=start_time.hour, minute=start_time.minute) schedule['start_time'] = start_datetime schedule['end_time'] = None schedule['title'] = item.find('a').text.strip() rating = item.find('img') if rating is None: schedule['rating'] = 0 else: schedule['rating'] = self.get_rating(rating['src']) return schedule
def convert_timefield_value(self, value, expression, connection, context): if value is not None: if not isinstance(value, datetime.time): value = parse_time(value) return value
def convert_timefield_value(self, value, expression, connection, context): if value is not None and not isinstance(value, datetime.time): value = parse_time(value) return value
def from_native(self, value): if value in validators.EMPTY_VALUES: return None if isinstance(value, datetime.time): return value for fmt in self.input_formats: if fmt.lower() == ISO_8601: try: parsed = parse_time(value) except (ValueError, TypeError): pass else: if parsed is not None: return parsed else: try: parsed = datetime.datetime.strptime(value, fmt) except (ValueError, TypeError): pass else: return parsed.time() msg = self.error_messages['invalid'] % readable_time_formats(self.input_formats) raise ValidationError(msg)
def parse_schedule_item(self, item, date): """Return CJ E&M channel schedule from table row.""" schedule = dict() # Get title try: title = item.find('div', class_='program')['title'] except KeyError: # Remove span tag title = item.find('div', class_='program').text schedule['title'] = title.strip() # Get ratings rating = item.find('td', class_='rating').find('span')['class'][0] schedule['rating'] = self.get_rating(rating) # Get start_time and end_time duration = item.find('td', class_='runningTime').text start_time = timezone.datetime.combine(date, dateparse.parse_time(item.find('em').text.strip())) schedule['start_time'] = timezone.make_aware(start_time, timezone.get_current_timezone()) schedule['end_time'] = \ start_time + timezone.timedelta(minutes=MovieScheduleParser.parse_string_to_int(duration, 0)) return schedule
def to_python(self, value): if value is None: return None if isinstance(value, datetime.time): return value if isinstance(value, datetime.datetime): # Not usually a good idea to pass in a datetime here (it loses # information), but this can be a side-effect of interacting with a # database backend (e.g. Oracle), so we'll be accommodating. return value.time() try: parsed = parse_time(value) if parsed is not None: return parsed except ValueError: raise exceptions.ValidationError( self.error_messages['invalid_time'], code='invalid_time', params={'value': value}, ) raise exceptions.ValidationError( self.error_messages['invalid'], code='invalid', params={'value': value}, )
def find(pos, user): ''' Get a valid CashDiary for today from the given POS, it will return: - None: if no CashDiary is available today and older one was already closed - New CashDiary: if no CashDiary is available today but there is an older one which it was opened - Existing CashDiary: if a CashDiary is available today (open or close) ''' # Get checkpoint ck = dateparse.parse_time(getattr(settings, "CASHDIARY_CLOSES_AT", '03:00')) year = timezone.now().year month = timezone.now().month day = timezone.now().day hour = ck.hour minute = ck.minute second = ck.second checkpoint = timezone.datetime(year, month, day, hour, minute, second) # Get cashdiary = CashDiary.objects.filter(pos=pos, opened_date__gte=checkpoint).order_by("-opened_date").first() if not cashdiary: # No cashdiary found for today, check older one oldercashdiary = CashDiary.objects.filter(pos=pos, opened_date__lt=checkpoint).order_by("-opened_date").first() if oldercashdiary: if oldercashdiary.closed_user: cashdiary = None else: # Older cashdiary is not closed, we have to close it and open a new one amount_cash = oldercashdiary.amount_cash() amount_cards = oldercashdiary.amount_cards() # The older cashdiary is still opened, we have to close it and create a new one oldercashdiary.closed_cash = amount_cash oldercashdiary.closed_cards = amount_cards oldercashdiary.closed_user = user oldercashdiary.closed_date = timezone.now() oldercashdiary.save() # Open new cashdiary cashdiary = CashDiary() cashdiary.pos = pos cashdiary.opened_cash = amount_cash cashdiary.opened_cards = amount_cards cashdiary.opened_user = user cashdiary.opened_date = timezone.now() cashdiary.save() # Return the found CashDiary return cashdiary