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

Inconsistent localization to timezones #6

Closed
frisi opened this issue Dec 17, 2016 · 2 comments
Closed

Inconsistent localization to timezones #6

frisi opened this issue Dec 17, 2016 · 2 comments

Comments

@frisi
Copy link
Member

frisi commented Dec 17, 2016

while testing timezone handling in plone.app.event i stumbled upon this inconsistency.

Timezone-naive DateTime objects have the same .ISO() value but behave differently when localized to UTC:

>>> unaware = DateTime('2016-06-15T07:00:00')
>>> unaware.timezoneNaive()
True
>>> unaware2=DateTime('2016/06/15 07:00:00')
>>> unaware2.ISO()
'2016-06-15T07:00:00'
>>> unaware.ISO() == unaware2.ISO()
True

>>> unaware2.toZone('UTC').ISO()
'2016-06-15T05:00:00+00:00'

>>> unaware.toZone('UTC').ISO()
'2016-06-15T07:00:00+00:00'

can this difference be explained somehow or is this a bug?

@tseaver
Copy link
Member

tseaver commented Dec 17, 2016

Because it matches the ISO 8601 timestamp forma, unaware is presumed to be already in UTC: unaware2 doesn't, and so is presumed to be in the local timezone for the server. E.g., consider this PythonScript:

foo = context.ZopeTime('2016-06-15T07:00:00')
bar = context.ZopeTime('2016/06/15 07:00:00')
print 'foo:', foo
print 'bar:', bar
return printed

which produces the following output on my US/Eastern machine:

foo: 2016/06/15 07:00:00 GMT+0
bar: 2016/06/15 07:00:00 GMT-4

This idiosyncratic behavior is intentional, although the original cause for it is lost in the mists of time.

@tseaver tseaver closed this as completed Dec 17, 2016
@frisi
Copy link
Member Author

frisi commented Dec 17, 2016

thanks for you explanation @tseaver.

i see that you've updated documentation a few years ago
(d016d48)
but this did not make it into the 3.0.x branch

what i'm looking for is a way to tell if a DateTime object has real timezone information (that was given intentionally)
or if the current timezone is just a "presumed" one.

  • adding timezone string to constructor
>>> aware = DateTime('2016/06/15 07:00:00 Europe/Vienna')
  • convert from a timezone aware python datetime object

can i use timezoneNaive for testing if the DateTime object has explicit timezone information?

the docstring suggests that it is used when converting between python datetime and zope DateTime objects.
and there might be cases where this value is None

print unaware.timezoneNaive.__doc__
The python datetime module introduces the idea of distinguishing
        between timezone aware and timezone naive datetime values. For lossless
        conversion to and from datetime.datetime record if we record this
        information using True / False. DateTime makes no distinction, when we
        don't have any information we return None here.

but for me it looks like it is something i can rely on.

it works for our unaware and aware objects

>>> unaware.timezoneNaive()
True
>>> aware.timezoneNaive()
False

and for those converted from python datetime

>>> dt_aware = pytz.timezone('Europe/Vienna').localize(datetime(2016,6,15,7,0))
>>> dt_naive = datetime(2016,6,15,7,0)
>>> DateTime(dt_naive).timezoneNaive()
True
>>> DateTime(dt_aware).timezoneNaive()
False

alternatives

i can't use timezone since it always returns a GMT offset.

>>> unaware.timezone()
'GMT+0'
>>> unaware2.timezone()
'GMT+2'

or should i check for offset information in the ISO represenatation.
it seems this can also be used to tell timezone aware and naive DateTimes apart:

>>> aware.ISO()
'2016-06-15T07:00:00+02:00'
>>> unaware.ISO()
'2016-06-15T07:00:00'
>>> unaware2.ISO()
'2016-06-15T07:00:00'

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