def process_date(value, lookup_type): value = ensure_datetime(value) ret = datetime.datetime.fromtimestamp(0) POSSIBLE_LOOKUPS = ("year", "month", "day", "hour", "minute", "second") ret = ret.replace( value.year, value.month if lookup_type in POSSIBLE_LOOKUPS[1:] else ret.month, value.day if lookup_type in POSSIBLE_LOOKUPS[2:] else ret.day, value.hour if lookup_type in POSSIBLE_LOOKUPS[3:] else ret.hour, value.minute if lookup_type in POSSIBLE_LOOKUPS[4:] else ret.minute, value.second if lookup_type in POSSIBLE_LOOKUPS[5:] else ret.second, ) return ret
def convert_datetime_fields(result): fields = [ x for x in self.query.model._meta.fields if x.get_internal_type() in ("DateTimeField", "DateField", "TimeField") ] for field in fields: column = field.column if isinstance(result, dict): # sometimes it's a key! value = result.get(column) else: value = None if value is not None: result[column] = ensure_datetime(value) return result
def field_conv_day_only(value): value = ensure_datetime(value) return datetime(value.year, value.month, value.day, 0, 0)
def field_conv_year_only(value): value = ensure_datetime(value) return datetime(value.year, 1, 1, 0, 0)