[pypy-svn] r29818 - in pypy/dist/pypy/module/rctime: . test

rhymes at codespeak.net rhymes at codespeak.net
Sat Jul 8 14:51:48 CEST 2006


Author: rhymes
Date: Sat Jul  8 14:51:45 2006
New Revision: 29818

Modified:
   pypy/dist/pypy/module/rctime/__init__.py
   pypy/dist/pypy/module/rctime/interp_time.py
   pypy/dist/pypy/module/rctime/test/test_rctime.py
Log:
add altzone, daylight, tzname and timezone constants

Modified: pypy/dist/pypy/module/rctime/__init__.py
==============================================================================
--- pypy/dist/pypy/module/rctime/__init__.py	(original)
+++ pypy/dist/pypy/module/rctime/__init__.py	Sat Jul  8 14:51:45 2006
@@ -4,6 +4,10 @@
 class Module(MixedModule):
     interpleveldefs = {
         'accept2dyear': 'interp_time.accept2dyear',
+        'timezone': 'interp_time.timezone',
+        'daylight': 'interp_time.daylight',
+        'tzname': 'interp_time.tzname',
+        'altzone': 'interp_time.altzone',
         'time': 'interp_time.time',
         'clock': 'interp_time.clock',
         'ctime': 'interp_time.ctime',
@@ -19,8 +23,26 @@
 #         
     def buildloaders(cls):
         from pypy.module.rctime import interp_time
+
+        # this machinery is needed to expose constants
+        # that have to be initialized one time only
+        
         Module.interpleveldefs["accept2dyear"] = 'space.wrap(%r)' %\
             interp_time._init_accept2dyear()
+        
+        timezonedict = dict()
+        timezonevalues = interp_time._init_timezone()
+        for index, key in enumerate(['timezone', 'daylight',
+            'tzname', 'altzone']):
+            val = timezonevalues[index]
+            if key == "tzname":
+                # tzname is a tuple
+                wrap = 'space.wrap(%s)' % str(val)
+            else:
+                wrap = 'space.wrap(%r)' % val
+            timezonedict[key] = wrap
+        Module.interpleveldefs.update(timezonedict)
+            
         super(Module, cls).buildloaders()
     buildloaders = classmethod(buildloaders)
 

Modified: pypy/dist/pypy/module/rctime/interp_time.py
==============================================================================
--- pypy/dist/pypy/module/rctime/interp_time.py	(original)
+++ pypy/dist/pypy/module/rctime/interp_time.py	Sat Jul  8 14:51:45 2006
@@ -55,6 +55,47 @@
 def _init_accept2dyear():
     return (1, 0)[bool(os.getenv("PYTHONY2K"))]
 
+def _init_timezone():
+    timezone = daylight = tzname = altzone = None
+
+    # if _MS_WINDOWS:
+    #     cdll.msvcrt._tzset()
+    # 
+    #     timezone = c_long.in_dll(cdll.msvcrt, "_timezone").value
+    #     if hasattr(cdll.msvcrt, "altzone"):
+    #         altzone = c_long.in_dll(cdll.msvcrt, "altzone").value
+    #     else:
+    #         altzone = timezone - 3600
+    #     daylight = c_long.in_dll(cdll.msvcrt, "_daylight").value
+    #     tzname = _tzname_t.in_dll(cdll.msvcrt, "_tzname")
+    #     tzname = (tzname.tzname_0, tzname.tzname_1)
+    if _POSIX:
+        YEAR = (365 * 24 + 6) * 3600
+
+        t = (((libc.time(byref(c_long(0)))) / YEAR) * YEAR)
+        tt = cConfig.time_t(t)
+        p = libc.localtime(byref(tt)).contents
+        janzone = -p.tm_gmtoff
+        janname = ("   ", p.tm_zone)[bool(p.tm_zone)]
+        tt = cConfig.time_t(tt.value + YEAR / 2)
+        p = libc.localtime(byref(tt)).contents
+        julyzone = -p.tm_gmtoff
+        julyname = ("   ", p.tm_zone)[bool(p.tm_zone)]
+
+        if janzone < julyzone:
+            # DST is reversed in the southern hemisphere
+            timezone = julyzone
+            altzone = janzone
+            daylight = int(janzone != julyzone)
+            tzname = (julyname, janname)
+        else:
+            timezone = janzone
+            altzone = julyzone
+            daylight = int(janzone != julyzone)
+            tzname = (janname, julyname)
+    
+    return timezone, daylight, tzname, altzone
+
 def _get_error_msg():
     errno = geterrno()
     return libc.strerror(errno)

Modified: pypy/dist/pypy/module/rctime/test/test_rctime.py
==============================================================================
--- pypy/dist/pypy/module/rctime/test/test_rctime.py	(original)
+++ pypy/dist/pypy/module/rctime/test/test_rctime.py	Sat Jul  8 14:51:45 2006
@@ -9,11 +9,11 @@
     def test_attributes(self):
         import rctime
         assert isinstance(rctime.accept2dyear, int)
-    #     assert isinstance(rctime.altzone, int)
-    #     assert isinstance(rctime.daylight, int)
-    #     assert isinstance(rctime.timezone, int)
-    #     assert isinstance(rctime.tzname, tuple)
-    # 
+        assert isinstance(rctime.altzone, int)
+        assert isinstance(rctime.daylight, int)
+        assert isinstance(rctime.timezone, int)
+        assert isinstance(rctime.tzname, tuple)
+    
     def test_sleep(self):
         import rctime
         raises(TypeError, rctime.sleep, "foo")



More information about the Pypy-commit mailing list