Skip to content

Commit

Permalink
Merge pull request #704 from weatherpattern/WIP-gettz-documentation
Browse files Browse the repository at this point in the history
Add gettz documentation.
  • Loading branch information
pganssle committed May 8, 2018
2 parents 99d0b76 + b403a00 commit 4bc9095
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ switch, and thus all their contributions are dual-licensed.
- Peter Bieringer <pb@MASKED>
- Pierre Gergondet <pierre.gergondet@MASKED> (gh: @gergondet)
- Quentin Pradet <quentin@MASKED>
- Raymond Cha (gh: @weatherpattern) **D**
- Ridhi Mahajan <ridhikmahajan@MASKED> **D**
- Roy Williams <rwilliams@MASKED>
- Rustem Saiargaliev (gh: @amureki) **D**
Expand Down
1 change: 1 addition & 0 deletions changelog.d/704.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added documentation for ``dateutil.tz.gettz``. Reported by @pganssle (gh issue #647). Fixed by @weatherpattern (gh pr #704)
71 changes: 69 additions & 2 deletions dateutil/tz/tz.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ def __repr__(self):
class tzlocal(_tzinfo):
"""
A :class:`tzinfo` subclass built around the ``time`` timezone functions.
"""
def __init__(self):
super(tzlocal, self).__init__()
Expand Down Expand Up @@ -1460,6 +1458,75 @@ def __get_gettz():
tzlocal_classes += (tzwinlocal,)

class GettzFunc(object):
"""
Retrieve a time zone object from a string representation
This function is intended to retrieve the :py:class:`tzinfo` subclass
that best represents the time zone that would be used if a POSIX
`TZ variable`_ were set to the same value.
If no argument or an empty string is passed to ``gettz``, local time
is returned:
.. code-block:: python3
>>> gettz()
tzfile('/etc/localtime')
This function is also the preferred way to map IANA tz database keys
to :class:`tzfile` objects:
.. code-block:: python3
>>> gettz('Pacific/Kiritimati')
tzfile('/usr/share/zoneinfo/Pacific/Kiritimati')
On Windows, the standard is extended to include the Windows-specific
zone names provided by the operating system:
.. code-block:: python3
>>> gettz('Egypt Standard Time')
tzwin('Egypt Standard Time')
Passing a GNU ``TZ`` style string time zone specification returns a
:class:`tzstr` object:
.. code-block:: python3
>>> gettz('AEST-10AEDT-11,M10.1.0/2,M4.1.0/3')
tzstr('AEST-10AEDT-11,M10.1.0/2,M4.1.0/3')
:param name:
A time zone name (IANA, or, on Windows, Windows keys), location of
a ``tzfile(5)`` zoneinfo file or ``TZ`` variable style time zone
specifier. An empty string, no argument or ``None`` is interpreted
as local time.
:return:
Returns an instance of one of ``dateutil``'s :py:class:`tzinfo`
subclasses.
.. versionchanged:: 2.7.0
After version 2.7.0, any two calls to ``gettz`` using the same
input strings will return the same object:
.. code-block:: python3
>>> tz.gettz('America/Chicago') is tz.gettz('America/Chicago')
True
In addition to improving performance, this ensures that
`"same zone" semantics`_ are used for datetimes in the same zone.
.. _`TZ variable`:
https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
.. _`"same zone" semantics`:
https://blog.ganssle.io/articles/2018/02/aware-datetime-arithmetic.html
"""
def __init__(self):

self.__instances = weakref.WeakValueDictionary()
Expand Down
2 changes: 2 additions & 0 deletions docs/tz.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
==
tz
==
.. autofunction:: dateutil.tz.gettz

.. automodule:: dateutil.tz
:members:
:undoc-members:

0 comments on commit 4bc9095

Please sign in to comment.