Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] - نسخه متنی

David Ascher, Alex Martelli, Anna Ravenscroft

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Recipe 3.8. Checking Whether Daylight Saving Time Is Currently in Effect


Credit: Doug Fort


Problem



You want to know whether daylight saving
time is in effect in your local time zone today.


Solution


It's a natural temptation to check
time.daylight for this purpose, but that
doesn't work. Instead you need:

import time
def is_dst( ):
return bool(time.localtime( ).tm_isdst)


Discussion


In my location (as in most others nowadays),
time.daylight is always 1
because time.daylight means that this time zone
has daylight saving time (DST) at some time
during the year, whether or not DST is in effect
today.

The very last item in the pseudo-tuple you get by calling
time.localtime, on the other hand, is
1 only when DST is currently in effect, otherwise
it's 0which, in my
experience, is exactly the information one usually needs to check.
This recipe wraps this check into a function, calling built-in type
bool to ensure the result is an elegant
true or False rather than a
rougher 1 or 0optional
refinements, but nice ones, I think. You could alternatively access
the relevant item as time.localtime( )[-1], but
using attribute-access syntax with the tm_isdst
attribute name is more readable.


See Also


Library Reference and Python in a
Nutshell
about module time.


/ 394