Codice sorgente per pywws.TimeZone

#!/usr/bin/env python

# pywws - Python software for USB Wireless Weather Stations
# http://github.com/jim-easterbrook/pywws
# Copyright (C) 2008-13  Jim Easterbrook  jim@jim-easterbrook.me.uk

# 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# 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.

Introduction
------------

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)

[documenti]class UTC(tzinfo): """UTC"""
[documenti] def utcoffset(self, dt): return ZERO
[documenti] def tzname(self, dt): return "UTC"
[documenti] 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
[documenti]class LocalTimezone(tzinfo): """Local time"""
[documenti] def utcoffset(self, dt): if self._isdst(dt): return DSTOFFSET else: return STDOFFSET
[documenti] def dst(self, dt): if self._isdst(dt): return DSTDIFF else: return ZERO
[documenti] def tzname(self, dt): return _time.tzname[self._isdst(dt)]
def _isdst(self, dt): tt = (dt.year, dt.month, dt.day, 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()