Example #1
0
 def testLocalTimeDelta(self):
     d = localTimeDelta()
     self.assertEqual(d.microseconds, 0)
     self.assertEqual(d.seconds % 3600, 0)
     self.assertTrue(-1 <= d.days < 1)
     d = localTimeDelta(time.time())
     self.assertEqual(d.microseconds, 0)
     self.assertEqual(d.seconds % 3600, 0)
     self.assertTrue(-1 <= d.days < 1)
Example #2
0
    def setCookie(self,
                  name,
                  value,
                  path='/',
                  expires='ONCLOSE',
                  secure=False):
        """Set a cookie.

        You can also set the path (which defaults to /).

        You can also set when it expires. It can expire:

        - 'NOW': this is the same as trying to delete it, but it
          doesn't really seem to work in IE
        - 'ONCLOSE': the default behavior for cookies (expires when
          the browser closes)
        - 'NEVER': some time in the far, far future.
        - integer: a timestamp value
        - tuple or struct_time: a tuple, as created by the time module
        - datetime: a datetime.datetime object for the time (if without
          time zone, assumed to be *local*, not GMT time)
        - timedelta: a duration counted from the present, e.g.,
          datetime.timedelta(days=14) (2 weeks in the future)
        - '+...': a time in the future, '...' should be something like
          1w (1 week), 3h46m (3:45), etc.  You can use y (year),
          b (month), w (week), d (day), h (hour), m (minute),
          s (second). This is done by the MiscUtils.DateInterval.
        """
        cookie = Cookie(name, value)
        t = expires
        if isinstance(t, str):
            if t == 'ONCLOSE':
                t = None
            elif t == 'NOW':
                cookie.delete()
                return
            elif t == 'NEVER':
                t = gmtime()
                t = (t[0] + 10, ) + t[1:]
            elif t.startswith('+'):
                t = time() + timeDecode(t[1:])
        if t:
            if isinstance(t, (int, float)):
                t = gmtime(t)
            if isinstance(t, (tuple, struct_time)):
                t = strftime("%a, %d-%b-%Y %H:%M:%S GMT", t)
            if isinstance(t, timedelta):
                t = datetime.now() + t
            if isinstance(t, datetime):
                d = t.utcoffset()
                if d is None:
                    d = localTimeDelta()
                t -= d
                t = t.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
            cookie.setExpires(t)
        if path:
            cookie.setPath(path)
        if secure:
            cookie.setSecure(secure)
        self.addCookie(cookie)
Example #3
0
    def setCookie(self, name, value, path='/', expires='ONCLOSE',
            secure=False):
        """Set a cookie.

        You can also set the path (which defaults to /).
        You can also set when it expires. It can expire:
          'NOW': this is the same as trying to delete it, but it
            doesn't really seem to work in IE
          'ONCLOSE': the default behavior for cookies (expires when
            the browser closes)
          'NEVER': some time in the far, far future.
          integer: a timestamp value
          tuple or struct_time: a tuple, as created by the time module
          datetime: a datetime.datetime object for the time (if without
            time zone, assumed to be *local*, not GMT time)
          timedelta: a duration counted from the present, e.g.,
            datetime.timedelta(days=14) (2 weeks in the future)
          '+...': a time in the future, '...' should be something like
            1w (1 week), 3h46m (3:45), etc.  You can use y (year),
            b (month), w (week), d (day), h (hour), m (minute),
            s (second). This is done by the MiscUtils.DateInterval.

        """
        cookie = Cookie(name, value)
        t = expires
        if isinstance(t, basestring):
            if t == 'ONCLOSE':
                t = None
            elif t == 'NOW':
                cookie.delete()
                return
            elif t == 'NEVER':
                t = gmtime()
                t = (t[0] + 10,) + t[1:]
            elif t.startswith('+'):
                t = time() + timeDecode(t[1:])
        if t:
            if isinstance(t, (int, long, float)):
                t = gmtime(t)
            if isinstance(t, (tuple, struct_time)):
                t = strftime("%a, %d-%b-%Y %H:%M:%S GMT", t)
            if isinstance(t, timedelta):
                t = datetime.now() + t
            if isinstance(t, datetime):
                d = t.utcoffset()
                if d is None:
                    d = localTimeDelta()
                t -= d
                t = t.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
            cookie.setExpires(t)
        if path:
            cookie.setPath(path)
        if secure:
            cookie.setSecure(secure)
        self.addCookie(cookie)