Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't compare offset-naive and offset-aware datetimes #283

Closed
manelclos opened this issue Jun 3, 2015 · 8 comments
Closed

can't compare offset-naive and offset-aware datetimes #283

manelclos opened this issue Jun 3, 2015 · 8 comments

Comments

@manelclos
Copy link
Contributor

I tried to import a json file that was just exported.

settings.py contains USE_TZ=True

Line number: 1 - can't compare offset-naive and offset-aware datetimes
Traceback (most recent call last):
File "/apps/local/lib/python2.7/site-packages/import_export/resources.py", line 361, in import_data
if self.skip_row(instance, original):
File "/apps/local/lib/python2.7/site-packages/import_export/resources.py", line 266, in skip_row
if field.get_value(instance) != field.get_value(original):
TypeError: can't compare offset-naive and offset-aware datetimes

Same with CSV

bmihelac added a commit that referenced this issue Jun 3, 2015
@bmihelac
Copy link
Member

bmihelac commented Jun 3, 2015

@manelclos can you add failing tests?

@manelclos
Copy link
Contributor Author

@bmihelac sure, will do. I'll look further into the issue too.

@manelclos
Copy link
Contributor Author

Tests are ok for me. Also, I have latest code working on another installation. USE_TZ seems not to be the problem. It also works on PostgreSQL. It may have something to do with the previous, original values.

Field defined as:

models.DateTimeField(auto_now_add=True)

When exporting:

ORIGINAL:  2014-02-13 13:38:04.813096+00:00 <type 'datetime.datetime'>
EXPORT:  2014-02-13 13:38:04 <type 'str'>

When importing:

INSTANCE:  2014-02-13 13:38:04 <type 'datetime.datetime'>
ORIGINAL:  2014-02-13 13:38:04.813096+00:00 <type 'datetime.datetime'>

@manelclos
Copy link
Contributor Author

Hi @bmihelac here is what I think we should do:

If the import works ok, just go ahead, this happens for me in different installations, and is also backwards compatible.

Then, if we get the TypeError exception, try to make the datetime timezone aware and compare again:

>>> value = '2014-02-13 10:37:38'
>>> format = "%Y-%m-%d %H:%M:%S"
>>> dt1 = datetime.strptime(value, format)
>>> dt1
datetime.datetime(2014, 2, 13, 10, 37, 38)
>>> from django.utils import timezone
>>> dt2 = timezone.make_aware(data, timezone.get_current_timezone())
>>> dt1.strftime(format)
'2014-02-13 10:37:38'
>>> dt2.strftime(format)
'2014-02-13 10:37:38'

https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#troubleshooting

Then, I'll have only the microseconds problem, but that should be solved by adding a DateTime widget with the proper format, right?

@bmihelac
Copy link
Member

bmihelac commented Jun 9, 2015

@manelclos what do you think about checking USE_TZ setting instead of catching exception.

We would basically replace this line with:

dt = datetime.strptime(value, format)
if settings.USE_TZ:
    dt = timezone.make_aware(dt, timezone.get_default_timezone())
return dt

@manelclos
Copy link
Contributor Author

@bmihelac Yes, makes more sense, though the impact is also bigger. I'll try the suggested change. Thanks!

@bmihelac
Copy link
Member

bmihelac commented Jun 9, 2015

Unfortunately strptime does not allow %z

@manelclos
Copy link
Contributor Author

@bmihelac can you please review?

@manelclos manelclos reopened this Jun 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants