Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.date_string = "1380-08-02"
self.datetime = jdatetime.datetime(1380, 8, 2, 12, 12, 12)
self.bar_time = BarTime(name="foo time", datetime=self.datetime)
self.bar_time.save()
def test_lookup_date_with_no_tz(self):
jdt1 = jdatetime.datetime(1392, 3, 12, 10, 22, 23, 240000)
m1 = BarTime.objects.create(name="with timezone", datetime=jdt1)
k = BarTime.objects.filter(datetime=jdt1)
self.assertEqual(k[0].datetime.strftime('%z'), '')
def get_db_prep_value(self, value, connection, prepared=False):
# Casts dates into the format expected by the backend
if not prepared:
value = self.get_prep_value(value)
if isinstance(value, jdatetime.datetime):
value = value.togregorian().date()
if isinstance(value, jdatetime.date):
value = value.togregorian()
if StrictVersion(django.get_version()) >= StrictVersion('1.9'):
return connection.ops.adapt_datefield_value(value)
else:
return connection.ops.value_to_db_date(value)
def to_python(self, value):
"""
Validates that the input can be converted to a datetime. Returns a
Python datetime.datetime object.
"""
if value in validators.EMPTY_VALUES:
return None
if isinstance(value, jdatetime.datetime):
return from_current_timezone(value)
if isinstance(value, jdatetime.date):
result = jdatetime.datetime(value.year, value.month, value.day)
return from_current_timezone(result)
if isinstance(value, list):
# Input comes from a SplitDateTimeWidget, for example. So, it's two
# components: date and time.
if len(value) != 2:
raise exceptions.ValidationError(self.error_messages['invalid'])
if value[0] in validators.EMPTY_VALUES and value[1] in validators.EMPTY_VALUES:
return None
value = '%s %s' % tuple(value)
groups = re.search(
r'(?P[\d]{1,4})-(?P[\d]{1,2})-(?P[\d]{1,2}) '
r'(?P[\d]{1,2}):(?P[\d]{1,2})'
def to_python(self, value):
if value is None:
return value
if isinstance(value, jdatetime.datetime):
return value
if isinstance(value, jdatetime.date):
try:
return jdatetime.datetime(value.year, value.month, value.day)
except ValueError:
raise exceptions.ValidationError(
self.error_messages['invalid'])
return self.parse_date(value)
def parse_date(self, datetime_obj):
"Take a datetime object and convert it to jalali date"
if isinstance(datetime_obj, datetime.datetime):
try:
if datetime_obj.year < 1700:
return jdatetime.datetime(
datetime_obj.year, datetime_obj.month,
datetime_obj.day, datetime_obj.hour,
datetime_obj.minute, datetime_obj.second,
datetime_obj.microsecond, datetime_obj.tzinfo)
else:
return jdatetime.datetime.fromgregorian(
datetime=datetime_obj)
except ValueError:
raise exceptions.ValidationError(
self.error_messages['invalid'])
if isinstance(datetime_obj, datetime.date):
try:
if datetime_obj.year < 1700:
return jdatetime.datetime(datetime_obj.year,
datetime_obj.month,
datetime_obj.day)
kwargs = {'microsecond': usecs}
formats = [
('%Y-%m-%d %H:%M:%S', 6),
('%Y-%m-%d %H:%M', 5),
('%Y-%m-%d', 3),
]
for format, c in formats:
try: # Seconds are optional, so try converting seconds first.
t = time.strptime(datetime_obj, format)
if t.tm_year > 1700:
return datetime.datetime(
*time.strptime(datetime_obj, format)[:c],
**kwargs)
else:
return jdatetime.datetime(
*time.strptime(datetime_obj, format)[:c],
**kwargs)
except ValueError:
try:
return jdatetime.datetime.strptime(
datetime_obj,
'%Y-%m-%d %H:%M'
).replace(**kwargs)
except ValueError:
pass
raise exceptions.ValidationError(self.error_messages['invalid'])