Skip to content

How to specify a default time zone for parse(), and nothing else #94

@nealmcb

Description

@nealmcb

I'd like to be able to provide a default timezone when parsing, to be used only if the dates I'm parsing don't specify one. That way my code would work in an ideal world, where all the dates provided to me have clear timezones, and also in the real world, where I have to figure out what time zone they expect me to assume, and tell the parser that. Sort of the opposite of ignoring the timezone via ignoretz.

That is exactly what tzinfos seemed to offer, based on the 2.4.2 documentation. But since that's not the case (as noted in #80), it would be great to document a clean way to do it.

This works pretty well:

>>> print dateutil.parser.parse("00:00", default=dateutil.parser.parse("00:00Z"))
2015-06-15 00:00:00+00:00

But I worry that it is providing more defaults than I really want to, like hours, minutes, and it is even more unwieldy when I want a default other than UTC.

So I'd prefer a more explicit and efficient shorthand, like parse("00:00", defaulttz=tzutc())

Note also, at https://dateutil.readthedocs.org/en/latest/parser.html: "If a time zone is omitted, it is assumed to be UTC". That seems to be wrong: I get tzinfo of None when parsing a string with no time zone:

>>> print dateutil.parser.parse("2000-01-01T00:00").tzinfo
None

Or perhaps parse changed since '2.2', the version on which I ran that, in which case clarifying when it changed would be helpful, though scary since it wouldn't be backwards-compatible.

Even so, I love dateutil. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions