pytz - World Timezone Definitions for Python
============================================
:Author: Stuart Bishop <stuart@stuartbishop.net>
Introduction
~~~~~~~~~~~~
pytz brings the Olson tz database into Python. This library allows
accurate and cross platform timezone calculations using Python 2.4
or higher. It also solves the issue of ambiguous times at the end
of daylight saving time, which you can read more about in the Python
Library Reference (``datetime.tzinfo``).
Almost all of the Olson timezones are supported.
.. note::
This library differs from the documented Python API for
tzinfo implementations; if you want to create local wallclock
times you need to use the ``localize()`` method documented in this
document. In addition, if you perform date arithmetic on local
times that cross DST boundaries, the result may be in an incorrect
timezone (ie. subtract 1 minute from 2002-10-27 1:00 EST and you get
2002-10-27 0:59 EST instead of the correct 2002-10-27 1:59 EDT). A
``normalize()`` method is provided to correct this. Unfortunately these
issues cannot be resolved without modifying the Python datetime
implementation (see PEP-431).
Installation
~~~~~~~~~~~~
This package can either be installed from a .egg file using setuptools,
or from the tarball using the standard Python distutils.
If you are installing from a tarball, run the following command as an
administrative user::
python setup.py install
If you are installing using setuptools, you don't even need to download
anything as the latest version will be downloaded for you
from the Python package index::
easy_install --upgrade pytz
If you already have the .egg file, you can use that too::
easy_install pytz-2008g-py2.6.egg
Example & Usage
~~~~~~~~~~~~~~~
Localized times and date arithmetic
-----------------------------------
>>> from datetime import datetime, timedelta
>>> from pytz import timezone
>>> import pytz
>>> utc = pytz.utc
>>> utc.zone
'UTC'
>>> eastern = timezone('US/Eastern')
>>> eastern.zone
'US/Eastern'
>>> amsterdam = timezone('Europe/Amsterdam')
>>> fmt = '%Y-%m-%d %H:%M:%S %Z%z'
This library only supports two ways of building a localized time. The
first is to use the ``localize()`` method provided by the pytz library.
This is used to localize a naive datetime (datetime with no timezone
information):
>>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0))
>>> print(loc_dt.strftime(fmt))
2002-10-27 06:00:00 EST-0500
The second way of building a localized time is by converting an existing
localized time using the standard ``astimezone()`` method:
>>> ams_dt = loc_dt.astimezone(amsterdam)
>>> ams_dt.strftime(fmt)
'2002-10-27 12:00:00 CET+0100'
Unfortunately using the tzinfo argument of the standard datetime
constructors ''does not work'' with pytz for many timezones.
>>> datetime(2002, 10, 27, 12, 0, 0, tzinfo=amsterdam).strftime(fmt)
'2002-10-27 12:00:00 LMT+0020'
It is safe for timezones without daylight saving transitions though, such
as UTC:
>>> datetime(2002, 10, 27, 12, 0, 0, tzinfo=pytz.utc).strftime(fmt)
'2002-10-27 12:00:00 UTC+0000'
The preferred way of dealing with times is to always work in UTC,
converting to localtime only when generating output to be read
by humans.
>>> utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc)
>>> loc_dt = utc_dt.astimezone(eastern)
>>> loc_dt.strftime(fmt)
'2002-10-27 01:00:00 EST-0500'
This library also allows you to do date arithmetic using local
times, although it is more complicated than working in UTC as you
need to use the ``normalize()`` method to handle daylight saving time
and other timezone transitions. In this example, ``loc_dt`` is set
to the instant when daylight saving time ends in the US/Eastern
timezone.
>>> before = loc_dt - timedelta(minutes=10)
>>> before.strftime(fmt)
'2002-10-27 00:50:00 EST-0500'
>>> eastern.normalize(before).strftime(fmt)
'2002-10-27 01:50:00 EDT-0400'
>>> after = eastern.normalize(before + timedelta(minutes=20))
>>> after.strftime(fmt)
'2002-10-27 01:10:00 EST-0500'
Creating local times is also tricky, and the reason why working with
local times is not recommended. Unfortunately, you cannot just pass
a ``tzinfo`` argument when constructing a datetime (see the next
section for more details)
>>> dt = datetime(2002, 10, 27, 1, 30, 0)
>>> dt1 = eastern.localize(dt, is_dst=True)
>>> dt1.strftime(fmt)
'2002-10-27 01:30:00 EDT-0400'
>>> dt2 = eastern.localize(dt, is_dst=False)
>>> dt2.strftime(fmt)
'2002-10-27 01:30:00 EST-0500'
Converting between timezones also needs special attention. We also need
to use the ``normalize()`` method to ensure the conversion is correct.
>>> utc_dt = utc.localize(datetime.utcfromtimestamp(1143408899))
>>> utc_dt.strftime(fmt)
'2006-03-26 21:34:59 UTC+0000'
>>> au_tz = timezone('Australia/Sydney')
>>> au_dt = au_tz.normalize(utc_dt.astimezone(au_tz))
>>> au_dt.strftime(fmt)
'2006-03-27 08:34:59 AEDT+1100'
>>> utc_dt2 = utc.normalize(au_dt.astimezone(utc))
>>> utc_dt2.strftime(fmt)
'2006-03-26 21:34:59 UTC+0000'
You can take shortcuts when dealing with the UTC side of timezone
conversions. ``normalize()`` and ``localize()`` are not really
necessary when there are no daylight saving time transitions to
deal with.
>>> utc_dt = datetime.utcfromtimestamp(1143408899).replace(tzinfo=utc)
>>> utc_dt.strftime(fmt)
'2006-03-26 21:34:59 UTC+0000'
>>> au_tz = timezone('Australia/Sydney')
>>> au_dt = au_tz.normalize(utc_dt.astimezone(au_tz))
>>> au_dt.strftime(fmt)
'2006-03-27 08:34:59 AEDT+1100'
>>> utc_dt2 = au_dt.astimezone(utc)
>>> utc_dt2.strftime(fmt)
'2006-03-26 21:34:59 UTC+0000'
``tzinfo`` API
--------------
The ``tzinfo`` instances returned by the ``timezone()`` function have
been extended to cope with ambiguous times by adding an ``is_dst``
parameter to the ``utcoffset()``, ``dst()`` && ``tzname()`` methods.
>>> tz = timezone('America/St_Johns')
>>> normal = datetime(2009, 9, 1)
>>> ambiguous = datetime(2009, 10, 31, 23, 30)
The ``is_dst`` parameter is ignored for most timestamps. It is only used
during DST transition ambiguous periods to resulve that ambiguity.
>>> tz.utcoffset(normal, is_dst=True)
datetime.timedelta(-1, 77400)
>>> tz.dst(normal, is_dst=True)
datetime.timedelta(0, 3600)
>>> tz.tzname(normal, is_dst=True)
'NDT'
>>> tz.utcoffset(ambiguous, is_dst=True)
datetime.timedelta(-1, 77400)
>>> tz.dst(ambiguous, is_dst=True)
datetime.timedelta(0, 3600)
>>> tz.tzname(ambiguous, is_dst=True)
'NDT'
>>> tz.utcoffset(normal, is_dst=False)
datetime.timedelta(-1, 77400)
>>> tz.dst(normal, is_dst=False)
datetime.timedelta(0, 3600)
>>> tz.tzname(normal, is_dst=False)
'NDT'
>>> tz.utcoffset(ambiguous, is_dst=False)
datetime.timedelta(-1, 73800)
>>> tz.dst(ambiguous, is_dst=False)
datetime.timedelta(0)
>>> tz.tzname(ambiguous, is_dst=False)
'NST'
If ``is_dst`` is not specified, ambiguous timestamps will raise
an ``pytz.exceptions.AmbiguousTimeError`` exception.
>>> tz.utcoffset(normal)
datetime.timedelta(-1, 77400)
>>> tz.dst(normal)
datetime.timedelta(0, 3600)
>>> tz.tzname(normal)
'NDT'
>>> import pytz.exceptions
>>> try:
... tz.utcoffset(ambiguous)
... except pytz.exceptions.AmbiguousTimeError:
... print('pytz.exceptions.AmbiguousTimeError: %s' % ambiguous)
pytz.exceptions.AmbiguousTimeError: 2009-10-31 23:30:00
>>> try:
... tz.dst(ambiguous)
... except pytz.exceptions.AmbiguousTimeError:
... print('pytz.exceptions.AmbiguousTimeError: %s' % ambiguous)
pytz.exceptions.AmbiguousTimeError: 2009-10-31 23:30:00
>>> try:
... tz.tzname(ambiguous)
... except pytz.exceptions.AmbiguousTimeError:
... print('pytz.exceptions.AmbiguousTimeError: %s' % ambiguous)
pytz.exceptions.AmbiguousTimeError: 2009-10-31 23:30:00
Problems with Localtime
~~~~~~~~~~~~~~~~~~~~~~~
The major problem we have to deal with is that certain datetimes
may occur twice in a year. For example, in the US/Eastern timezone
on the last Sunday morning in October, the following sequence
happen
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
新零售大数据平台作为一个复杂的分布式系统,需要进行全方位的运维监控,以确保整个系统的稳定性和高效运行。因此,开发一个运维监控平台来集中管理和监控各个大数据组件就显得尤为重要。 在这个项目中,您将基于开源的 Hue 平台(Hadoop User Experience)进行二次开发,通过修改和扩展其源代码,使其能够支持管理和监控更多的大数据组件。具体来说,您需要为以下组件添加监控和管理功能: 1. Hadoop: 作为大数据生态系统的核心,监控 Hadoop 集群的健康状态、资源利用率和作业运行情况是至关重要的。 2. Hive: 作为构建在 Hadoop 之上的数据仓库,需要监控 Hive 的元数据、查询执行情况和性能指标。 3. HBase: 作为分布式的 NoSQL 数据库,需要监控 HBase 集群的运行状态、Region 分布、读写性能等。 4. ZooKeeper: 作为分布式协调服务,需要监控 ZooKeeper 集群的运行状态、连接情况和数据一致性。 5. Kettle: 作为数据集成工具,需要监控 Kettle 作业的执行情况、转换性能和出错信息。
资源推荐
资源详情
资源评论
收起资源包目录
新零售大数据平台-运维监控平台的开发 (2000个子文件)
_speedups.c 5KB
c_cpp.cpp 806B
ambiance.css 27KB
ace_api.css 17KB
ace_api.css 17KB
editor.css 14KB
style.css 11KB
iplastic.css 6KB
docbook.css 6KB
css-tests.css 5KB
searchbox.css 4KB
hue.css 4KB
style.css 3KB
tomorrow_night_bright.css 3KB
katzenmilch.css 3KB
dreamweaver.css 3KB
tomorrow_night_eighties.css 3KB
sqlserver.css 3KB
tomorrow_night_blue.css 3KB
chaos.css 3KB
terminal.css 3KB
crimson_editor.css 3KB
tomorrow_night.css 3KB
mono_industrial.css 3KB
chrome.css 3KB
textmate.css 3KB
pastel_on_dark.css 3KB
tomorrow.css 2KB
twilight.css 2KB
github.css 2KB
merbivore_soft.css 2KB
clouds_midnight.css 2KB
cobalt.css 2KB
monokai.css 2KB
solarized_light.css 2KB
kr_theme.css 2KB
solarized_dark.css 2KB
dawn.css 2KB
idle_fingers.css 2KB
merbivore.css 2KB
vibrant_ink.css 2KB
eclipse.css 2KB
kuroir.css 2KB
clouds.css 2KB
xcode.css 2KB
settings_menu.css 1KB
theme.css 1KB
styles.css 857B
static.css 851B
DataExport.css 658B
iphone.css 293B
css.css 253B
dart.dart 489B
golang.go 641B
editor.html 290KB
edit_session.html 227KB
virtual_renderer.html 144KB
selection.html 107KB
index.html 94KB
range.html 70KB
document.html 69KB
css-tests.html 39KB
split.html 35KB
background_tokenizer.html 21KB
search.html 18KB
anchor.html 17KB
undomanager.html 15KB
scrollbar.html 15KB
token_iterator.html 15KB
placeholder.html 15KB
epl.html 12KB
perf-test.html 12KB
ace.html 10KB
kitchen-sink.html 8KB
tokenizer.html 8KB
basic_animation.html 8KB
animate_folding.html 7KB
buttons.html 6KB
navbar.html 5KB
navbar-static-top.html 5KB
navbar-fixed-top.html 5KB
forms.html 4KB
bookmarklet.html 4KB
scrollable-page.html 4KB
mode_creator.html 3KB
cut_copy.html 2KB
forms-responsive.html 2KB
static-highlighter.html 2KB
renderloop.html 2KB
debug_mem_leak.html 2KB
command_manager.html 2KB
autoresize.html 2KB
statusbar.html 1KB
editor.html 1KB
modelist.html 1KB
login.html 1KB
client.html 1KB
keyboard_shortcuts.html 1KB
jHueTourBuilder.html 1KB
requirejs+build.html 1KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
进击的代码家
- 粉丝: 2203
- 资源: 204
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功