2007-08-15 11:28:22 -03:00
|
|
|
from datetime import tzinfo, timedelta, datetime
|
|
|
|
|
|
|
|
ZERO = timedelta(0)
|
|
|
|
HOUR = timedelta(hours=1)
|
|
|
|
|
|
|
|
# A UTC class.
|
|
|
|
|
|
|
|
class UTC(tzinfo):
|
|
|
|
"""UTC"""
|
|
|
|
|
|
|
|
def utcoffset(self, dt):
|
|
|
|
return ZERO
|
|
|
|
|
|
|
|
def tzname(self, dt):
|
|
|
|
return "UTC"
|
|
|
|
|
|
|
|
def dst(self, dt):
|
|
|
|
return ZERO
|
|
|
|
|
|
|
|
utc = UTC()
|
|
|
|
|
|
|
|
# A class building tzinfo objects for fixed-offset time zones.
|
|
|
|
# Note that FixedOffset(0, "UTC") is a different way to build a
|
|
|
|
# UTC tzinfo object.
|
|
|
|
|
|
|
|
class FixedOffset(tzinfo):
|
|
|
|
"""Fixed offset in minutes east from UTC."""
|
|
|
|
|
|
|
|
def __init__(self, offset, name):
|
|
|
|
self.__offset = timedelta(minutes = offset)
|
|
|
|
self.__name = name
|
|
|
|
|
|
|
|
def utcoffset(self, dt):
|
|
|
|
return self.__offset
|
|
|
|
|
|
|
|
def tzname(self, dt):
|
|
|
|
return self.__name
|
|
|
|
|
|
|
|
def dst(self, dt):
|
|
|
|
return ZERO
|
|
|
|
|
|
|
|
# A class capturing the platform's idea of local time.
|
|
|
|
|
|
|
|
import time as _time
|
|
|
|
|
|
|
|
STDOFFSET = timedelta(seconds = -_time.timezone)
|
|
|
|
if _time.daylight:
|
|
|
|
DSTOFFSET = timedelta(seconds = -_time.altzone)
|
|
|
|
else:
|
|
|
|
DSTOFFSET = STDOFFSET
|
|
|
|
|
|
|
|
DSTDIFF = DSTOFFSET - STDOFFSET
|
|
|
|
|
|
|
|
class LocalTimezone(tzinfo):
|
|
|
|
|
|
|
|
def utcoffset(self, dt):
|
|
|
|
if self._isdst(dt):
|
|
|
|
return DSTOFFSET
|
|
|
|
else:
|
|
|
|
return STDOFFSET
|
|
|
|
|
|
|
|
def dst(self, dt):
|
|
|
|
if self._isdst(dt):
|
|
|
|
return DSTDIFF
|
|
|
|
else:
|
|
|
|
return ZERO
|
|
|
|
|
|
|
|
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,
|
2010-12-23 20:24:11 -04:00
|
|
|
dt.weekday(), 0, 0)
|
2007-08-15 11:28:22 -03:00
|
|
|
stamp = _time.mktime(tt)
|
|
|
|
tt = _time.localtime(stamp)
|
|
|
|
return tt.tm_isdst > 0
|
|
|
|
|
|
|
|
Local = LocalTimezone()
|
|
|
|
|
|
|
|
|
|
|
|
# A complete implementation of current DST rules for major US time zones.
|
|
|
|
|
|
|
|
def first_sunday_on_or_after(dt):
|
|
|
|
days_to_go = 6 - dt.weekday()
|
|
|
|
if days_to_go:
|
|
|
|
dt += timedelta(days_to_go)
|
|
|
|
return dt
|
|
|
|
|
Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62194 | jeffrey.yasskin | 2008-04-07 01:04:28 +0200 (Mon, 07 Apr 2008) | 7 lines
Add enough debugging information to diagnose failures where the
HandlerBException is ignored, and fix one such problem, where it was thrown
during the __del__ method of the previous Popen object.
We may want to find a better way of printing verbose information so it's not
spammy when the test passes.
........
r62197 | mark.hammond | 2008-04-07 03:53:39 +0200 (Mon, 07 Apr 2008) | 2 lines
Issue #2513: enable 64bit cross compilation on windows.
........
r62198 | mark.hammond | 2008-04-07 03:59:40 +0200 (Mon, 07 Apr 2008) | 2 lines
correct heading underline for new "Cross-compiling on Windows" section
........
r62204 | gregory.p.smith | 2008-04-07 08:33:21 +0200 (Mon, 07 Apr 2008) | 4 lines
Use the new PyFile_IncUseCount & PyFile_DecUseCount calls appropriatly
within the standard library. These modules use PyFile_AsFile and later
release the GIL while operating on the previously returned FILE*.
........
r62205 | mark.summerfield | 2008-04-07 09:39:23 +0200 (Mon, 07 Apr 2008) | 4 lines
changed "2500 components" to "several thousand" since the number keeps
growning:-)
........
r62214 | georg.brandl | 2008-04-07 20:51:59 +0200 (Mon, 07 Apr 2008) | 2 lines
#2525: update timezone info examples in the docs.
........
r62219 | andrew.kuchling | 2008-04-08 01:57:07 +0200 (Tue, 08 Apr 2008) | 1 line
Write PEP 3127 section; add items
........
r62220 | andrew.kuchling | 2008-04-08 01:57:21 +0200 (Tue, 08 Apr 2008) | 1 line
Typo fix
........
r62221 | andrew.kuchling | 2008-04-08 03:33:10 +0200 (Tue, 08 Apr 2008) | 1 line
Typographical fix: 32bit -> 32-bit, 64bit -> 64-bit
........
r62227 | andrew.kuchling | 2008-04-08 23:22:53 +0200 (Tue, 08 Apr 2008) | 1 line
Add items
........
r62229 | amaury.forgeotdarc | 2008-04-08 23:27:42 +0200 (Tue, 08 Apr 2008) | 7 lines
Issue2564: Prevent a hang in "import test.autotest", which runs the entire test
suite as a side-effect of importing the module.
- in test_capi, a thread tried to import other modules
- re.compile() imported sre_parse again on every call.
........
r62230 | amaury.forgeotdarc | 2008-04-08 23:51:57 +0200 (Tue, 08 Apr 2008) | 2 lines
Prevent an error when inspect.isabstract() is called with something else than a new-style class.
........
r62231 | amaury.forgeotdarc | 2008-04-09 00:07:05 +0200 (Wed, 09 Apr 2008) | 8 lines
Issue 2408: remove the _types module
It was only used as a helper in types.py to access types (GetSetDescriptorType and MemberDescriptorType),
when they can easily be obtained with python code.
These expressions even work with Jython.
I don't know what the future of the types module is; (cf. discussion in http://bugs.python.org/issue1605 )
at least this change makes it simpler.
........
r62233 | amaury.forgeotdarc | 2008-04-09 01:10:07 +0200 (Wed, 09 Apr 2008) | 2 lines
Add a NEWS entry for previous checkin
........
r62234 | trent.nelson | 2008-04-09 01:47:30 +0200 (Wed, 09 Apr 2008) | 37 lines
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
........
r62235 | gregory.p.smith | 2008-04-09 02:25:17 +0200 (Wed, 09 Apr 2008) | 3 lines
Fix zlib crash from zlib.decompressobj().flush(val) when val was not positive.
It tried to allocate negative or zero memory. That fails.
........
r62237 | trent.nelson | 2008-04-09 02:34:53 +0200 (Wed, 09 Apr 2008) | 1 line
Fix typo with regards to self.PORT shadowing class variables with the same name.
........
r62238 | andrew.kuchling | 2008-04-09 03:08:32 +0200 (Wed, 09 Apr 2008) | 1 line
Add items
........
r62239 | jerry.seutter | 2008-04-09 07:07:58 +0200 (Wed, 09 Apr 2008) | 1 line
Changed test so it no longer runs as a side effect of importing.
........
2008-04-09 05:37:03 -03:00
|
|
|
|
|
|
|
# US DST Rules
|
|
|
|
#
|
|
|
|
# This is a simplified (i.e., wrong for a few cases) set of rules for US
|
|
|
|
# DST start and end times. For a complete and up-to-date set of DST rules
|
|
|
|
# and timezone definitions, visit the Olson Database (or try pytz):
|
|
|
|
# http://www.twinsun.com/tz/tz-link.htm
|
|
|
|
# http://sourceforge.net/projects/pytz/ (might not be up-to-date)
|
|
|
|
#
|
|
|
|
# In the US, since 2007, DST starts at 2am (standard time) on the second
|
|
|
|
# Sunday in March, which is the first Sunday on or after Mar 8.
|
|
|
|
DSTSTART_2007 = datetime(1, 3, 8, 2)
|
|
|
|
# and ends at 2am (DST time; 1am standard time) on the first Sunday of Nov.
|
|
|
|
DSTEND_2007 = datetime(1, 11, 1, 1)
|
|
|
|
# From 1987 to 2006, DST used to start at 2am (standard time) on the first
|
|
|
|
# Sunday in April and to end at 2am (DST time; 1am standard time) on the last
|
|
|
|
# Sunday of October, which is the first Sunday on or after Oct 25.
|
|
|
|
DSTSTART_1987_2006 = datetime(1, 4, 1, 2)
|
|
|
|
DSTEND_1987_2006 = datetime(1, 10, 25, 1)
|
|
|
|
# From 1967 to 1986, DST used to start at 2am (standard time) on the last
|
|
|
|
# Sunday in April (the one on or after April 24) and to end at 2am (DST time;
|
|
|
|
# 1am standard time) on the last Sunday of October, which is the first Sunday
|
|
|
|
# on or after Oct 25.
|
|
|
|
DSTSTART_1967_1986 = datetime(1, 4, 24, 2)
|
|
|
|
DSTEND_1967_1986 = DSTEND_1987_2006
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
class USTimeZone(tzinfo):
|
|
|
|
|
|
|
|
def __init__(self, hours, reprname, stdname, dstname):
|
|
|
|
self.stdoffset = timedelta(hours=hours)
|
|
|
|
self.reprname = reprname
|
|
|
|
self.stdname = stdname
|
|
|
|
self.dstname = dstname
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.reprname
|
|
|
|
|
|
|
|
def tzname(self, dt):
|
|
|
|
if self.dst(dt):
|
|
|
|
return self.dstname
|
|
|
|
else:
|
|
|
|
return self.stdname
|
|
|
|
|
|
|
|
def utcoffset(self, dt):
|
|
|
|
return self.stdoffset + self.dst(dt)
|
|
|
|
|
|
|
|
def dst(self, dt):
|
|
|
|
if dt is None or dt.tzinfo is None:
|
|
|
|
# An exception may be sensible here, in one or both cases.
|
|
|
|
# It depends on how you want to treat them. The default
|
|
|
|
# fromutc() implementation (called by the default astimezone()
|
|
|
|
# implementation) passes a datetime with dt.tzinfo is self.
|
|
|
|
return ZERO
|
|
|
|
assert dt.tzinfo is self
|
|
|
|
|
Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62194 | jeffrey.yasskin | 2008-04-07 01:04:28 +0200 (Mon, 07 Apr 2008) | 7 lines
Add enough debugging information to diagnose failures where the
HandlerBException is ignored, and fix one such problem, where it was thrown
during the __del__ method of the previous Popen object.
We may want to find a better way of printing verbose information so it's not
spammy when the test passes.
........
r62197 | mark.hammond | 2008-04-07 03:53:39 +0200 (Mon, 07 Apr 2008) | 2 lines
Issue #2513: enable 64bit cross compilation on windows.
........
r62198 | mark.hammond | 2008-04-07 03:59:40 +0200 (Mon, 07 Apr 2008) | 2 lines
correct heading underline for new "Cross-compiling on Windows" section
........
r62204 | gregory.p.smith | 2008-04-07 08:33:21 +0200 (Mon, 07 Apr 2008) | 4 lines
Use the new PyFile_IncUseCount & PyFile_DecUseCount calls appropriatly
within the standard library. These modules use PyFile_AsFile and later
release the GIL while operating on the previously returned FILE*.
........
r62205 | mark.summerfield | 2008-04-07 09:39:23 +0200 (Mon, 07 Apr 2008) | 4 lines
changed "2500 components" to "several thousand" since the number keeps
growning:-)
........
r62214 | georg.brandl | 2008-04-07 20:51:59 +0200 (Mon, 07 Apr 2008) | 2 lines
#2525: update timezone info examples in the docs.
........
r62219 | andrew.kuchling | 2008-04-08 01:57:07 +0200 (Tue, 08 Apr 2008) | 1 line
Write PEP 3127 section; add items
........
r62220 | andrew.kuchling | 2008-04-08 01:57:21 +0200 (Tue, 08 Apr 2008) | 1 line
Typo fix
........
r62221 | andrew.kuchling | 2008-04-08 03:33:10 +0200 (Tue, 08 Apr 2008) | 1 line
Typographical fix: 32bit -> 32-bit, 64bit -> 64-bit
........
r62227 | andrew.kuchling | 2008-04-08 23:22:53 +0200 (Tue, 08 Apr 2008) | 1 line
Add items
........
r62229 | amaury.forgeotdarc | 2008-04-08 23:27:42 +0200 (Tue, 08 Apr 2008) | 7 lines
Issue2564: Prevent a hang in "import test.autotest", which runs the entire test
suite as a side-effect of importing the module.
- in test_capi, a thread tried to import other modules
- re.compile() imported sre_parse again on every call.
........
r62230 | amaury.forgeotdarc | 2008-04-08 23:51:57 +0200 (Tue, 08 Apr 2008) | 2 lines
Prevent an error when inspect.isabstract() is called with something else than a new-style class.
........
r62231 | amaury.forgeotdarc | 2008-04-09 00:07:05 +0200 (Wed, 09 Apr 2008) | 8 lines
Issue 2408: remove the _types module
It was only used as a helper in types.py to access types (GetSetDescriptorType and MemberDescriptorType),
when they can easily be obtained with python code.
These expressions even work with Jython.
I don't know what the future of the types module is; (cf. discussion in http://bugs.python.org/issue1605 )
at least this change makes it simpler.
........
r62233 | amaury.forgeotdarc | 2008-04-09 01:10:07 +0200 (Wed, 09 Apr 2008) | 2 lines
Add a NEWS entry for previous checkin
........
r62234 | trent.nelson | 2008-04-09 01:47:30 +0200 (Wed, 09 Apr 2008) | 37 lines
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
........
r62235 | gregory.p.smith | 2008-04-09 02:25:17 +0200 (Wed, 09 Apr 2008) | 3 lines
Fix zlib crash from zlib.decompressobj().flush(val) when val was not positive.
It tried to allocate negative or zero memory. That fails.
........
r62237 | trent.nelson | 2008-04-09 02:34:53 +0200 (Wed, 09 Apr 2008) | 1 line
Fix typo with regards to self.PORT shadowing class variables with the same name.
........
r62238 | andrew.kuchling | 2008-04-09 03:08:32 +0200 (Wed, 09 Apr 2008) | 1 line
Add items
........
r62239 | jerry.seutter | 2008-04-09 07:07:58 +0200 (Wed, 09 Apr 2008) | 1 line
Changed test so it no longer runs as a side effect of importing.
........
2008-04-09 05:37:03 -03:00
|
|
|
# Find start and end times for US DST. For years before 1967, return
|
|
|
|
# ZERO for no DST.
|
|
|
|
if 2006 < dt.year:
|
|
|
|
dststart, dstend = DSTSTART_2007, DSTEND_2007
|
|
|
|
elif 1986 < dt.year < 2007:
|
|
|
|
dststart, dstend = DSTSTART_1987_2006, DSTEND_1987_2006
|
|
|
|
elif 1966 < dt.year < 1987:
|
|
|
|
dststart, dstend = DSTSTART_1967_1986, DSTEND_1967_1986
|
|
|
|
else:
|
|
|
|
return ZERO
|
|
|
|
|
|
|
|
start = first_sunday_on_or_after(dststart.replace(year=dt.year))
|
|
|
|
end = first_sunday_on_or_after(dstend.replace(year=dt.year))
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
# Can't compare naive to aware objects, so strip the timezone from
|
|
|
|
# dt first.
|
|
|
|
if start <= dt.replace(tzinfo=None) < end:
|
|
|
|
return HOUR
|
|
|
|
else:
|
|
|
|
return ZERO
|
|
|
|
|
|
|
|
Eastern = USTimeZone(-5, "Eastern", "EST", "EDT")
|
|
|
|
Central = USTimeZone(-6, "Central", "CST", "CDT")
|
|
|
|
Mountain = USTimeZone(-7, "Mountain", "MST", "MDT")
|
|
|
|
Pacific = USTimeZone(-8, "Pacific", "PST", "PDT")
|