README.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. tzlocal
  2. =======
  3. This Python module returns a ``tzinfo`` object with the local timezone information under Unix and Win-32.
  4. It requires ``pytz``, and returns ``pytz`` ``tzinfo`` objects.
  5. This module attempts to fix a glaring hole in ``pytz``, that there is no way to
  6. get the local timezone information, unless you know the zoneinfo name, and
  7. under several Linux distros that's hard or impossible to figure out.
  8. Also, with Windows different timezone system using pytz isn't of much use
  9. unless you separately configure the zoneinfo timezone name.
  10. With ``tzlocal`` you only need to call ``get_localzone()`` and you will get a
  11. ``tzinfo`` object with the local time zone info. On some Unices you will still
  12. not get to know what the timezone name is, but you don't need that when you
  13. have the tzinfo file. However, if the timezone name is readily available it
  14. will be used.
  15. Supported systems
  16. -----------------
  17. These are the systems that are in theory supported:
  18. * Windows 2000 and later
  19. * Any unix-like system with a ``/etc/localtime`` or ``/usr/local/etc/localtime``
  20. If you have one of the above systems and it does not work, it's a bug.
  21. Please report it.
  22. Please note that if you getting a time zone called ``local``, this is not a bug, it's
  23. actually the main feature of ``tzlocal``, that even if your system does NOT have a configuration file
  24. with the zoneinfo name of your time zone, it will still work.
  25. You can also use ``tzlocal`` to get the name of your local timezone, but only if your system is
  26. configured to make that possible. ``tzlocal`` looks for the timezone name in ``/etc/timezone``, ``/var/db/zoneinfo``,
  27. ``/etc/sysconfig/clock`` and ``/etc/conf.d/clock``. If your ``/etc/localtime`` is a symlink it can also extract the
  28. name from that symlink.
  29. If you need the name of your local time zone, then please make sure your system is properly configured to allow that.
  30. If it isn't configured, tzlocal will default to UTC.
  31. Usage
  32. -----
  33. Load the local timezone:
  34. >>> from tzlocal import get_localzone
  35. >>> tz = get_localzone()
  36. >>> tz
  37. <DstTzInfo 'Europe/Warsaw' WMT+1:24:00 STD>
  38. Create a local datetime:
  39. >>> from datetime import datetime
  40. >>> dt = tz.localize(datetime(2015, 4, 10, 7, 22))
  41. >>> dt
  42. datetime.datetime(2015, 4, 10, 7, 22, tzinfo=<DstTzInfo 'Europe/Warsaw' CEST+2:00:00 DST>)
  43. Lookup another timezone with `pytz`:
  44. >>> import pytz
  45. >>> eastern = pytz.timezone('US/Eastern')
  46. Convert the datetime:
  47. >>> dt.astimezone(eastern)
  48. datetime.datetime(2015, 4, 10, 1, 22, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
  49. Maintainer
  50. ----------
  51. * Lennart Regebro, regebro@gmail.com
  52. Contributors
  53. ------------
  54. * Marc Van Olmen
  55. * Benjamen Meyer
  56. * Manuel Ebert
  57. * Xiaokun Zhu
  58. * Cameris
  59. * Edward Betts
  60. * McK KIM
  61. * Cris Ewing
  62. * Ayala Shachar
  63. * Lev Maximov
  64. * Jakub Wilk
  65. * John Quarles
  66. * Preston Landers
  67. * Victor Torres
  68. * Jean Jordaan
  69. * Zackary Welch
  70. * Mickaël Schoentgen
  71. * Gabriel Corona
  72. (Sorry if I forgot someone)
  73. License
  74. -------
  75. * MIT https://opensource.org/licenses/MIT