Skip to content

Add default tzinfo#475

Merged
pganssle merged 2 commits into
dateutil:masterfrom
pganssle:add-default-tzinfo
Oct 26, 2017
Merged

Add default tzinfo#475
pganssle merged 2 commits into
dateutil:masterfrom
pganssle:add-default-tzinfo

Conversation

@pganssle

Copy link
Copy Markdown
Member

As a partial and possibly more general fix to #94, this adds a tzinfo to any datetime that doesn't have one, and otherwise leaves it alone.

@pganssle pganssle added this to the 2.7.0 milestone Oct 14, 2017
@pganssle

Copy link
Copy Markdown
Member Author

Ping @nealmcb

@nealmcb

nealmcb commented Oct 14, 2017

Copy link
Copy Markdown

Thanks for working on this!

So my proposal was implementing something like this:

>>> parse("00:00", defaulttz=tzutc())

And it seems that this would be the proposed implementation from this PR a6aae67:

>>> from dateutil.utils import default_tzinfo
>>> default_tzinfo(parse("0:00"), tzutc())

I guess that works, and if people get their date objects from somewhere other than parse, it is indeed more general. But it is less discoverable for people looking at the parse documentation. Would it make sense to put an example there?

@nealmcb

nealmcb commented Oct 14, 2017

Copy link
Copy Markdown

I guess a practical example would be something that takes two strings to be parsed from some list of meeting times, in which one string has timezone info, and the other doesn't. Both could be parsed, with a default of the NYC timezone provided, and the results printed out.

Comment thread dateutil/utils.py
"""

dt = datetime.now(tzinfo)
return datetime.combine(dt.date(), time(0, tzinfo=tzinfo))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for today(tzinfo), but I'm very wary of implementing behavior that differs from same-named stdlib behavior. btw:

>>> tz = pytz.timezone('US/Pacific')
>>> pd.Timestamp.today(tz)
Timestamp('2017-10-14 17:19:04.081315-0700', tz='US/Pacific')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel Sorry, I miscalculated what would be shown in the diff here, this discussion should be taking place on #474, so I've responded here

Comment thread dateutil/utils.py
if dt.tzinfo is not None:
return dt
else:
return dt.replace(tzinfo=tzinfo)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

from dateutil import utils
from dateutil.utils import within_delta

from freezegun import freeze_time

@jbrockmendel jbrockmendel Oct 15, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Now if I can figure out #463 to mock tzlocal, we'll make a mockery of this whole endeavor.

@pganssle

Copy link
Copy Markdown
Member Author

@nealmcb Yes, that's not a bad idea. After we settle on the final interface here, I can add it in to the documentation for the parser.

@pganssle

Copy link
Copy Markdown
Member Author

@nealmcb Added an example in the default_tzinfo documentation. Look good?

@jbrockmendel

Copy link
Copy Markdown
Contributor

LGTM

@pganssle
pganssle merged commit 6932824 into dateutil:master Oct 26, 2017
@pganssle pganssle mentioned this pull request Mar 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants