Code source de pywws.TimeZone

#!/usr/bin/env python

# pywws - Python software for USB Wireless Weather Stations
# Copyright (C) 2008-13  Jim Easterbrook

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

"""Provide a couple of :py:class:`datetime.tzinfo` objects
representing local time and UTC.


This module provides two :py:class:`datetime.tzinfo` objects
representing UTC and local time zones. These are used to convert
timestamps to and from UTC and local time. The weather station
software stores data with UTC timestamps, to avoid problems with
daylight savings time, but the template and plot programs output data
with local times.

The module is copied directly from the :py:class:`datetime.tzinfo`
module documentation.

Detailed API


from datetime import tzinfo, timedelta, datetime
import time as _time

ZERO = timedelta(0)
HOUR = timedelta(hours=1)

[docs]class UTC(tzinfo): """UTC"""
[docs] def utcoffset(self, dt): return ZERO
[docs] def tzname(self, dt): return "UTC"
[docs] def dst(self, dt): return ZERO
utc = UTC() STDOFFSET = timedelta(seconds = -_time.timezone) if _time.daylight: DSTOFFSET = timedelta(seconds = -_time.altzone) else: DSTOFFSET = STDOFFSET DSTDIFF = DSTOFFSET - STDOFFSET
[docs]class LocalTimezone(tzinfo): """Local time"""
[docs] def utcoffset(self, dt): if self._isdst(dt): return DSTOFFSET else: return STDOFFSET
[docs] def dst(self, dt): if self._isdst(dt): return DSTDIFF else: return ZERO
[docs] def tzname(self, dt): return _time.tzname[self._isdst(dt)]
def _isdst(self, dt): tt = (dt.year, dt.month,, dt.hour, dt.minute, dt.second, dt.weekday(), 0, -1) stamp = _time.mktime(tt) tt = _time.localtime(stamp) return tt.tm_isdst > 0
Local = LocalTimezone()