Ejemplo n.º 1
0
 def test_total_seconds(self):
     d = Duration(days=365)
     self.assertEqual(d.total_seconds(), 31536000.0)
     for total_seconds in [123456.789012, -123456.789012, 0.123456, 0, 1e6]:
         d = Duration(seconds=total_seconds)
         self.assertEqual(d.total_seconds(), total_seconds)
     # Issue8644: Test that d.total_seconds() has the same
     # accuracy as d / Duration(seconds=1).
     for ms in [-1, -2, -123]:
         d = Duration(microseconds=ms)
         self.assertEqual(d.total_seconds(), d / Duration(seconds=1))
Ejemplo n.º 2
0
    def test_accuracy(self):
        tiny = Duration.resolution
        # Make sure we can make a Duration that uses Long:
        m = Duration(2 ** 65, seconds=1234, microseconds=567890)

        # The total_microseconds() can give as accuracy:
        self.assertEqual(m.total_microseconds(), 3187597375937010519246034567890)

        # Float can handle this many days:
        self.assertEqual(m.days, int(m.total_days()))
        # Float does not have the accuracy for the seconds in this case:
        self.assertEqual(m.total_microseconds() - int(m.total_seconds() * 1000000), 1234567890)
        self.assertEqual(m.total_microseconds() - int(m.total_days() * 86400000000), 1234567890)
Ejemplo n.º 3
0
    def test_computations(self):
        eq = self.assertEqual

        a = Duration(7)  # One week
        b = Duration(0, 60)  # One minute
        c = Duration(0, 0, 1000)  # One millisecond
        eq(a + b + c, Duration(7, 60, 1000))
        eq(a - b, Duration(6, 24 * 3600 - 60))
        eq(b.__rsub__(a), Duration(6, 24 * 3600 - 60))
        eq(-a, Duration(-7))
        eq(+a, Duration(7))
        eq(-b, Duration(-1, 24 * 3600 - 60))
        eq(-c, Duration(-1, 24 * 3600 - 1, 999000))
        eq(abs(a), a)
        eq(abs(-a), a)
        eq(Duration(6, 24 * 3600), a)
        eq(Duration(0, 0, 60 * 1000000), b)
        eq(a * 10, Duration(70))
        eq(a * 10, 10 * a)
        eq(a * 10, 10 * a)
        eq(b * 10, Duration(0, 600))
        eq(10 * b, Duration(0, 600))
        eq(b * 10, Duration(0, 600))
        eq(c * 10, Duration(0, 0, 10000))
        eq(10 * c, Duration(0, 0, 10000))
        eq(c * 10, Duration(0, 0, 10000))
        eq(a * -1, -a)
        eq(b * -2, -b - b)
        eq(c * -2, -c + -c)
        eq(b * (60 * 24), (b * 60) * 24)
        eq(b * (60 * 24), (60 * b) * 24)
        eq(c * 1000, Duration(0, 1))
        eq(1000 * c, Duration(0, 1))
        eq(a // 7, Duration(1))
        eq(b // 10, Duration(0, 6))
        eq(c // 1000, Duration(0, 0, 1))
        eq(a // 10, Duration(0, 7 * 24 * 360))
        eq(a // 3600000, Duration(0, 0, 7 * 24 * 1000))
        eq(a / 0.5, Duration(14))
        eq(b / 0.5, Duration(0, 120))
        eq(a / 7, Duration(1))
        eq(b / 10, Duration(0, 6))
        eq(c / 1000, Duration(0, 0, 1))
        eq(a / 10, Duration(0, 7 * 24 * 360))
        eq(a / 3600000, Duration(0, 0, 7 * 24 * 1000))

        # Multiplication by float
        us = Duration(microseconds=1)
        eq((3 * us) * 0.5, 2 * us)
        eq((5 * us) * 0.5, 2 * us)
        eq(0.5 * (3 * us), 2 * us)
        eq(0.5 * (5 * us), 2 * us)
        eq((-3 * us) * 0.5, -2 * us)
        eq((-5 * us) * 0.5, -2 * us)

        # See http://bugs.python.org/review/23521
        eq(Duration(seconds=1) * 0.123456, Duration(microseconds=123456))
        eq(Duration(seconds=1) * 0.6112295, Duration(microseconds=611230))

        # Division by int and float
        eq((3 * us) / 2, 2 * us)
        eq((5 * us) / 2, 2 * us)
        eq((-3 * us) / 2.0, -2 * us)
        eq((-5 * us) / 2.0, -2 * us)
        eq((3 * us) / -2, -2 * us)
        eq((5 * us) / -2, -2 * us)
        eq((3 * us) / -2.0, -2 * us)
        eq((5 * us) / -2.0, -2 * us)
        for i in range(-10, 10):
            eq((i * us / 3) // us, round(i / 3))
        for i in range(-10, 10):
            eq((i * us / -3) // us, round(i / -3))

        # See http://bugs.python.org/review/23521
        eq(Duration(seconds=1) / (1 / 0.6112295), Duration(microseconds=611230))

        # http://bugs.python.org/review/11576
        eq(Duration(999999999, 86399, 999999) - Duration(999999999, 86399, 999998), Duration(0, 0, 1))
        eq(Duration(999999999, 1, 1) - Duration(999999999, 1, 0), Duration(0, 0, 1))