README.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. tzlocal
  2. =======
  3. API CHANGE!
  4. -----------
  5. With version 3.0 of tzlocal, tzlocal no longer returned `pytz` objects, but
  6. `zoneinfo` objects, which has a different API. Since 4.0, it now restored
  7. partial compatibility for `pytz` users through Paul Ganssle's
  8. `pytz_deprecation_shim`.
  9. tzlocal 4.0 also adds an official function `get_localzone_name()` to get only
  10. the timezone name, instead of a timezone object. On unix, it can raise an
  11. error if you don't have a timezone name configured, where `get_localzone()`
  12. will succeed, so only use that if you need the timezone name.
  13. 4.0 also adds way more information on what is going wrong in your
  14. configuration when the configuration files are unclear or contradictory.
  15. Version 5.0 removes the `pytz_deprecation_shim`, and now only returns
  16. `zoneinfo` objects, like verion 3.0 did. If you need `pytz` objects, you have
  17. to stay on version 4.0. If there are bugs in version 4.0, I will release
  18. updates, but there will be no further functional changes on the 4.x branch.
  19. Info
  20. ----
  21. This Python module returns the `IANA time zone name
  22. <https://www.iana.org/time-zones>`_ for your local time zone or a ``tzinfo``
  23. object with the local timezone information, under Unix and Windows.
  24. It requires Python 3.8 or later, and will use the ``backports.tzinfo``
  25. package, for Python 3.8.
  26. This module attempts to fix a glaring hole in the ``pytz`` and ``zoneinfo``
  27. modules, that there is no way to get the local timezone information, unless
  28. you know the zoneinfo name, and under several Linux distros that's hard or
  29. impossible to figure out.
  30. With ``tzlocal`` you only need to call ``get_localzone()`` and you will get a
  31. ``tzinfo`` object with the local time zone info. On some Unices you will
  32. still not get to know what the timezone name is, but you don't need that when
  33. you have the tzinfo file. However, if the timezone name is readily available
  34. it will be used.
  35. What it's not for
  36. -----------------
  37. It's not for converting the current time between UTC and your local time. There are
  38. other, simpler ways of doing this. This is ig you need to know things like the name
  39. of the time zone, or if you need to be able to convert between your time zone and
  40. another time zone for times that are in the future or in the past.
  41. For current time conversions to and from UTC, look in the Python ``time`` module.
  42. Supported systems
  43. -----------------
  44. These are the systems that are in theory supported:
  45. * Windows 2000 and later
  46. * Any unix-like system with a ``/etc/localtime`` or ``/usr/local/etc/localtime``
  47. If you have one of the above systems and it does not work, it's a bug.
  48. Please report it.
  49. Please note that if you are getting a time zone called ``local``, this is not
  50. a bug, it's actually the main feature of ``tzlocal``, that even if your
  51. system does NOT have a configuration file with the zoneinfo name of your time
  52. zone, it will still work.
  53. You can also use ``tzlocal`` to get the name of your local timezone, but only
  54. if your system is configured to make that possible. ``tzlocal`` looks for the
  55. timezone name in ``/etc/timezone``, ``/var/db/zoneinfo``,
  56. ``/etc/sysconfig/clock`` and ``/etc/conf.d/clock``. If your
  57. ``/etc/localtime`` is a symlink it can also extract the name from that
  58. symlink.
  59. If you need the name of your local time zone, then please make sure your
  60. system is properly configured to allow that.
  61. If your unix system doesn't have a timezone configured, tzlocal will default
  62. to UTC.
  63. Notes on Docker
  64. ---------------
  65. It turns out that Docker images frequently have broken timezone setups.
  66. This usually resuts in a warning that the configuration is wrong, or that
  67. the timezone offset doesn't match the found timezone.
  68. The easiest way to fix that is to set a TZ variable in your docker setup
  69. to whatever timezone you want, which is usually the timezone your host
  70. computer has.
  71. Usage
  72. -----
  73. Load the local timezone:
  74. >>> from tzlocal import get_localzone
  75. >>> tz = get_localzone()
  76. >>> tz
  77. zoneinfo.ZoneInfo(key='Europe/Warsaw')
  78. Create a local datetime:
  79. >>> from datetime import datetime
  80. >>> dt = datetime(2015, 4, 10, 7, 22, tzinfo=tz)
  81. >>> dt
  82. datetime.datetime(2015, 4, 10, 7, 22, tzinfo=zoneinfo.ZoneInfo(key='Europe/Warsaw'))
  83. Lookup another timezone with ``zoneinfo`` (``backports.zoneinfo`` on Python 3.8 or earlier):
  84. >>> from zoneinfo import ZoneInfo
  85. >>> eastern = ZoneInfo('US/Eastern')
  86. Convert the datetime:
  87. >>> dt.astimezone(eastern)
  88. datetime.datetime(2015, 4, 10, 1, 22, tzinfo=zoneinfo.ZoneInfo(key='US/Eastern'))
  89. If you just want the name of the local timezone, use `get_localzone_name()`:
  90. >>> from tzlocal import get_localzone_name
  91. >>> get_localzone_name()
  92. "Europe/Warsaw"
  93. Please note that under Unix, `get_localzone_name()` may fail if there is no zone
  94. configured, where `get_localzone()` would generally succeed.
  95. Troubleshooting
  96. ---------------
  97. If you don't get the result you expect, try running it with debugging turned on.
  98. Start a python interpreter that has tzlocal installed, and run the following code::
  99. import logging
  100. logging.basicConfig(level="DEBUG")
  101. import tzlocal
  102. tzlocal.get_localzone()
  103. The output should look something like this, and this will tell you what
  104. configurations were found::
  105. DEBUG:root:/etc/timezone found, contents:
  106. Europe/Warsaw
  107. DEBUG:root:/etc/localtime found
  108. DEBUG:root:2 found:
  109. {'/etc/timezone': 'Europe/Warsaw', '/etc/localtime is a symlink to': 'Europe/Warsaw'}
  110. zoneinfo.ZoneInfo(key='Europe/Warsaw')
  111. Development
  112. -----------
  113. For ease of development, there is a Makefile that will help you with basic tasks,
  114. like creating a development environment with all the necessary tools (although
  115. you need a supported Python version installed first)::
  116. $ make devenv
  117. To run tests::
  118. $ make test
  119. Check the syntax::
  120. $ make check
  121. Maintainer
  122. ----------
  123. * Lennart Regebro, regebro@gmail.com
  124. Contributors
  125. ------------
  126. * Marc Van Olmen
  127. * Benjamen Meyer
  128. * Manuel Ebert
  129. * Xiaokun Zhu
  130. * Cameris
  131. * Edward Betts
  132. * McK KIM
  133. * Cris Ewing
  134. * Ayala Shachar
  135. * Lev Maximov
  136. * Jakub Wilk
  137. * John Quarles
  138. * Preston Landers
  139. * Victor Torres
  140. * Jean Jordaan
  141. * Zackary Welch
  142. * Mickaël Schoentgen
  143. * Gabriel Corona
  144. * Alex Grönholm
  145. * Julin S
  146. * Miroslav Šedivý
  147. * revansSZ
  148. * Sam Treweek
  149. * Peter Di Pasquale
  150. * Rongrong
  151. (Sorry if I forgot someone)
  152. License
  153. -------
  154. * MIT https://opensource.org/licenses/MIT