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

rhymes at codespeak.net rhymes at codespeak.net
Sat Jul 8 15:17:28 CEST 2006


Author: rhymes
Date: Sat Jul  8 15:17:26 2006
New Revision: 29821

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:
implemented time.tzset(). add also an helper function _set_module_object to set attributes at module level (renamed _get_app_object to the more meaningful _set_module_object). 


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 15:17:26 2006
@@ -15,6 +15,7 @@
         'gmtime': 'interp_time.gmtime',
         'localtime': 'interp_time.localtime',
         'mktime': 'interp_time.mktime',
+        'tzset': 'interp_time.tzset'
     }
 
     # def init(self, space):

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 15:17:26 2006
@@ -72,7 +72,7 @@
     if _POSIX:
         YEAR = (365 * 24 + 6) * 3600
 
-        t = (((libc.time(byref(c_long(0)))) / YEAR) * YEAR)
+        t = (((libc.time(byref(cConfig.time_t(0)))) / YEAR) * YEAR)
         tt = cConfig.time_t(t)
         p = libc.localtime(byref(tt)).contents
         janzone = -p.tm_gmtoff
@@ -133,14 +133,20 @@
     #     return t.value
 
 def _check_float(space, seconds):
-    w_check_float = _get_app_object(space, "_check_float")
+    # this call the app level _check_float to check the type of
+    # the given seconds
+    w_check_float = _get_module_object(space, "_check_float")
     space.call_function(w_check_float, space.wrap(seconds))
     
-def _get_app_object(space, obj_name):
+def _get_module_object(space, obj_name):
     w_module = space.getbuiltinmodule('rctime')
     w_obj = space.getattr(w_module, space.wrap(obj_name))
     return w_obj
 
+def _set_module_object(space, obj_name, obj_value):
+    w_module = space.getbuiltinmodule('rctime')
+    w_obj = space.setattr(w_module, space.wrap(obj_name), space.wrap(obj_value))    
+
 def _get_floattime(space, w_seconds):
     # this check is done because None will be automatically wrapped
     if space.is_w(w_seconds, space.w_None):
@@ -163,7 +169,7 @@
     time_tuple.append(space.wrap(t.tm_yday + 1)) # want january, 1 == 1
     time_tuple.append(space.wrap(t.tm_isdst))
     
-    w_struct_time = _get_app_object(space, 'struct_time')
+    w_struct_time = _get_module_object(space, 'struct_time')
     w_time_tuple = space.newtuple(time_tuple)
     return space.call_function(w_struct_time, w_time_tuple)
 
@@ -178,7 +184,7 @@
     buf.tm_yday = space.int_w(w_tup[7])
     buf.tm_isdst = space.int_w(w_tup[8])
 
-    w_accept2dyear = _get_app_object(space, "accept2dyear")
+    w_accept2dyear = _get_module_object(space, "accept2dyear")
     accept2dyear = space.int_w(w_accept2dyear)
     
     if y < 1900:
@@ -309,7 +315,7 @@
     """
 
     # rpython does not support that a variable has two incompatible builtins
-    # as value so we have to duplicate the code. NOT GOOD! see localtime()
+    # as value so we have to duplicate the code. NOT GOOD! see localtime() too
     seconds = _get_floattime(space, w_seconds)
     whent = cConfig.time_t(int(seconds))
     p = libc.gmtime(byref(whent))
@@ -366,3 +372,27 @@
 
     return space.wrap(float(tt))
 mktime.unwrap_spec = [ObjSpace, W_Root]
+
+if _POSIX:
+    def tzset(space):
+        """tzset()
+
+        Initialize, or reinitialize, the local timezone to the value stored in
+        os.environ['TZ']. The TZ environment variable should be specified in
+        standard Unix timezone format as documented in the tzset man page
+        (eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently
+        fall back to UTC. If the TZ environment variable is not set, the local
+        timezone is set to the systems best guess of wallclock time.
+        Changing the TZ environment variable without calling tzset *may* change
+        the local timezone used by methods such as localtime, but this behaviour
+        should not be relied on"""
+
+        libc.tzset()
+        
+        # reset timezone, altzone, daylight and tzname
+        timezone, daylight, tzname, altzone = _init_timezone()
+        _set_module_object(space, "timezone", timezone)
+        _set_module_object(space, 'daylight', daylight)
+        _set_module_object(space, 'tzname', tzname)
+        _set_module_object(space, 'altzone', altzone)
+    tzset.unwrap_spec = [ObjSpace]

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 15:17:26 2006
@@ -123,69 +123,72 @@
         st_time = rctime.struct_time(tup)
         assert str(st_time) == str(tup)
         assert len(st_time) == len(tup)
-    # 
-    # def test_tzset():
-    #     if not hasattr(rctime, "tzset"):
-    #         py.test.skip("available only under Unix")
-    # 
-    #     # epoch time of midnight Dec 25th 2002. Never DST in northern
-    #     # hemisphere.
-    #     xmas2002 = 1040774400.0
-    # 
-    #     # these formats are correct for 2002, and possibly future years
-    #     # this format is the 'standard' as documented at:
-    #     # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
-    #     # They are also documented in the tzset(3) man page on most Unix
-    #     # systems.
-    #     eastern = 'EST+05EDT,M4.1.0,M10.5.0'
-    #     victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
-    #     utc = 'UTC+0'
-    # 
-    #     org_TZ = os.environ.get('TZ', None)
-    #     try:
-    #         # Make sure we can switch to UTC time and results are correct
-    #         # Note that unknown timezones default to UTC.
-    #         # Note that altzone is undefined in UTC, as there is no DST
-    #         os.environ['TZ'] = eastern
-    #         rctime.tzset()
-    #         os.environ['TZ'] = utc
-    #         rctime.tzset()
-    #         assert rctime.gmtime(xmas2002) == rctime.localtime(xmas2002)
-    #         assert rctime.daylight == 0
-    #         assert rctime.timezone == 0
-    #         assert rctime.localtime(xmas2002).tm_isdst == 0
-    # 
-    #         # make sure we can switch to US/Eastern
-    #         os.environ['TZ'] = eastern
-    #         rctime.tzset()
-    #         assert rctime.gmtime(xmas2002) != rctime.localtime(xmas2002)
-    #         assert rctime.tzname == ('EST', 'EDT')
-    #         assert len(rctime.tzname) == 2
-    #         assert rctime.daylight == 1
-    #         assert rctime.timezone == 18000
-    #         assert rctime.altzone == 14400
-    #         assert rctime.localtime(xmas2002).tm_isdst == 0
-    # 
-    #         # now go to the southern hemisphere.
-    #         os.environ['TZ'] = victoria
-    #         rctime.tzset()
-    #         assert rctime.gmtime(xmas2002) != rctime.localtime(xmas2002)
-    #         assert rctime.tzname[0] == 'AEST'
-    #         assert rctime.tzname[1] == 'AEDT'
-    #         assert len(rctime.tzname) == 2
-    #         assert rctime.daylight == 1
-    #         assert rctime.timezone == -36000
-    #         assert rctime.altzone == -39600
-    #         assert rctime.localtime(xmas2002).tm_isdst == 1
-    #     finally:
-    #         # repair TZ environment variable in case any other tests
-    #         # rely on it.
-    #         if org_TZ is not None:
-    #             os.environ['TZ'] = org_TZ
-    #         elif os.environ.has_key('TZ'):
-    #             del os.environ['TZ']
-    #         rctime.tzset()
-    # 
+    
+    def test_tzset(self):
+        import rctime
+        import os
+        
+        if not os.name == "posix":
+            py.test.skip("available only under Unix")
+    
+        # epoch time of midnight Dec 25th 2002. Never DST in northern
+        # hemisphere.
+        xmas2002 = 1040774400.0
+    
+        # these formats are correct for 2002, and possibly future years
+        # this format is the 'standard' as documented at:
+        # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
+        # They are also documented in the tzset(3) man page on most Unix
+        # systems.
+        eastern = 'EST+05EDT,M4.1.0,M10.5.0'
+        victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
+        utc = 'UTC+0'
+    
+        org_TZ = os.environ.get('TZ', None)
+        try:
+            # Make sure we can switch to UTC time and results are correct
+            # Note that unknown timezones default to UTC.
+            # Note that altzone is undefined in UTC, as there is no DST
+            os.environ['TZ'] = eastern
+            rctime.tzset()
+            os.environ['TZ'] = utc
+            rctime.tzset()
+            assert rctime.gmtime(xmas2002) == rctime.localtime(xmas2002)
+            assert rctime.daylight == 0
+            assert rctime.timezone == 0
+            assert rctime.localtime(xmas2002).tm_isdst == 0
+    
+            # make sure we can switch to US/Eastern
+            os.environ['TZ'] = eastern
+            rctime.tzset()
+            assert rctime.gmtime(xmas2002) != rctime.localtime(xmas2002)
+            assert rctime.tzname == ('EST', 'EDT')
+            assert len(rctime.tzname) == 2
+            assert rctime.daylight == 1
+            assert rctime.timezone == 18000
+            assert rctime.altzone == 14400
+            assert rctime.localtime(xmas2002).tm_isdst == 0
+    
+            # now go to the southern hemisphere.
+            os.environ['TZ'] = victoria
+            rctime.tzset()
+            assert rctime.gmtime(xmas2002) != rctime.localtime(xmas2002)
+            assert rctime.tzname[0] == 'AEST'
+            assert rctime.tzname[1] == 'AEDT'
+            assert len(rctime.tzname) == 2
+            assert rctime.daylight == 1
+            assert rctime.timezone == -36000
+            assert rctime.altzone == -39600
+            assert rctime.localtime(xmas2002).tm_isdst == 1
+        finally:
+            # repair TZ environment variable in case any other tests
+            # rely on it.
+            if org_TZ is not None:
+                os.environ['TZ'] = org_TZ
+            elif os.environ.has_key('TZ'):
+                del os.environ['TZ']
+            rctime.tzset()
+    
     # def test_strftime():
     #     tt = rctime.gmtime(t)
     #     for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',



More information about the Pypy-commit mailing list