dateutil / dateutil
1
# -*- coding: utf-8 -*-
2 20
from __future__ import unicode_literals
3 20
from datetime import timedelta, datetime
4

5 20
import unittest
6

7 20
from dateutil import tz
8 20
from dateutil import utils
9 20
from dateutil.tz import UTC
10 20
from dateutil.utils import within_delta
11

12 20
from freezegun import freeze_time
13

14 20
NYC = tz.gettz("America/New_York")
15

16

17 20
class UtilsTest(unittest.TestCase):
18 20
    @freeze_time(datetime(2014, 12, 15, 1, 21, 33, 4003))
19 2
    def testToday(self):
20 20
        self.assertEqual(utils.today(), datetime(2014, 12, 15, 0, 0, 0))
21

22 20
    @freeze_time(datetime(2014, 12, 15, 12), tz_offset=5)
23 2
    def testTodayTzInfo(self):
24 20
        self.assertEqual(utils.today(NYC),
25
                         datetime(2014, 12, 15, 0, 0, 0, tzinfo=NYC))
26

27 20
    @freeze_time(datetime(2014, 12, 15, 23), tz_offset=5)
28 2
    def testTodayTzInfoDifferentDay(self):
29 20
        self.assertEqual(utils.today(UTC),
30
                         datetime(2014, 12, 16, 0, 0, 0, tzinfo=UTC))
31

32 20
    def testDefaultTZInfoNaive(self):
33 20
        dt = datetime(2014, 9, 14, 9, 30)
34 20
        self.assertIs(utils.default_tzinfo(dt, NYC).tzinfo,
35
                      NYC)
36

37 20
    def testDefaultTZInfoAware(self):
38 20
        dt = datetime(2014, 9, 14, 9, 30, tzinfo=UTC)
39 20
        self.assertIs(utils.default_tzinfo(dt, NYC).tzinfo,
40
                      UTC)
41

42 20
    def testWithinDelta(self):
43 20
        d1 = datetime(2016, 1, 1, 12, 14, 1, 9)
44 20
        d2 = d1.replace(microsecond=15)
45

46 20
        self.assertTrue(within_delta(d1, d2, timedelta(seconds=1)))
47 20
        self.assertFalse(within_delta(d1, d2, timedelta(microseconds=1)))
48

49 20
    def testWithinDeltaWithNegativeDelta(self):
50 20
        d1 = datetime(2016, 1, 1)
51 20
        d2 = datetime(2015, 12, 31)
52

53 20
        self.assertTrue(within_delta(d2, d1, timedelta(days=-1)))

Read our documentation on viewing source code .

Loading