Exemplo n.º 1
0
 def testMSTruncated(self):
     # milliseconds are kept but microseconds are lost after rounding.
     self.check(
         datetime(year=2000,
                  month=12,
                  day=25,
                  microsecond=500500,
                  tzinfo=TimeZoneInfo.utc()),
         datetime(year=2000,
                  month=12,
                  day=25,
                  microsecond=500000,
                  tzinfo=TimeZoneInfo.utc()))
Exemplo n.º 2
0
 def testUTC(self):
     self.check(
         datetime(year=2000,
                  month=12,
                  day=25,
                  microsecond=500000,
                  tzinfo=TimeZoneInfo.utc()))
    def testFileTimes(self):
        if issubclass(pywintypes.TimeType, datetime.datetime):
            from win32timezone import TimeZoneInfo
            # now() is always returning a timestamp with microseconds but the
            # file APIs all have zero microseconds, so some comparisons fail.
            now = datetime.datetime.now(tz=TimeZoneInfo.utc()).replace(
                microsecond=0)
            nowish = now + datetime.timedelta(seconds=1)
            later = now + datetime.timedelta(seconds=120)
        else:
            rc, tzi = win32api.GetTimeZoneInformation()
            bias = tzi[0]
            if rc == 2:  # daylight-savings is in effect.
                bias += tzi[-1]
            bias *= 60  # minutes to seconds...
            tick = int(time.time())
            now = pywintypes.Time(tick + bias)
            nowish = pywintypes.Time(tick + bias + 1)
            later = pywintypes.Time(tick + bias + 120)

        filename = tempfile.mktemp("-testFileTimes")
        # Windows docs the 'last time' isn't valid until the last write
        # handle is closed - so create the file, then re-open it to check.
        open(filename, "w").close()
        f = win32file.CreateFile(
            filename, win32file.GENERIC_READ | win32file.GENERIC_WRITE, 0,
            None, win32con.OPEN_EXISTING, 0, None)
        try:
            ct, at, wt = win32file.GetFileTime(f)
            self.failUnless(
                ct >= now,
                "File was created in the past - now=%s, created=%s" %
                (now, ct))
            self.failUnless(now <= ct <= nowish, (now, ct))
            self.failUnless(
                wt >= now,
                "File was written-to in the past now=%s, written=%s" %
                (now, wt))
            self.failUnless(now <= wt <= nowish, (now, wt))

            # Now set the times.
            win32file.SetFileTime(f, later, later, later, UTCTimes=True)
            # Get them back.
            ct, at, wt = win32file.GetFileTime(f)
            # XXX - the builtin PyTime type appears to be out by a dst offset.
            # just ignore that type here...
            self.failUnlessEqual(ct, later)
            self.failUnlessEqual(at, later)
            self.failUnlessEqual(wt, later)

        finally:
            f.Close()
            os.unlink(filename)