summaryrefslogtreecommitdiff
path: root/tests/test_times.py
blob: 34e52478b460d257c363282f04e01ea2b199cf45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
try:
    import unittest2 as unittest
except ImportError:
    import unittest  # noqa
from datetime import datetime

import pytz
import times


class TestTimes(unittest.TestCase):
    def setUp(self):
        est = pytz.timezone('EST')
        ams = pytz.timezone('Europe/Amsterdam')

        self.time_in_ny = est.localize(datetime(2012, 2, 1, 6, 56, 31))
        self.time_in_ams = ams.localize(datetime(2012, 2, 1, 12, 56, 31))
        self.sometime_univ = datetime(2012, 2, 1, 11, 56, 31)

    def test_now(self):  # noqa
        """times.now() has no attached timezone info"""
        now = times.now()
        self.assertIsNone(now.tzinfo)

    def test_to_universal_with_tzinfo(self):  # noqa
        """Convert local dates with timezone info to universal date"""
        ny_time = self.time_in_ny
        ams_time = self.time_in_ams

        self.assertEquals(times.to_universal(ny_time),
                          self.sometime_univ)
        self.assertEquals(times.to_universal(ams_time),
                          self.sometime_univ)

        self.assertEquals(ny_time.hour, 6)
        self.assertEquals(times.to_universal(ny_time).hour, 11)

        self.assertEquals(ams_time.hour, 12)
        self.assertEquals(times.to_universal(ams_time).hour, 11)

        # Test alias from_local, too
        self.assertEquals(times.from_local(ny_time), self.sometime_univ)

    def test_to_universal_without_tzinfo(self):
        """Convert local dates without timezone info to universal date"""

        # Same as above, but with tzinfo stripped off (as if a NY and Amsterdam
        # user used datetime.now())
        ny_time = self.time_in_ny.replace(tzinfo=None)
        ams_time = self.time_in_ams.replace(tzinfo=None)

        # When time has no tzinfo attached, it should be specified explicitly
        est = 'EST'
        self.assertEquals(times.to_universal(ny_time, est),
                          self.sometime_univ)

        # ...or simply with a string
        self.assertEquals(times.to_universal(ams_time, 'Europe/Amsterdam'),
                          self.sometime_univ)

    def test_to_universal_with_unix_timestamp(self):
        """Convert UNIX timestamps to universal date"""
        unix_time = 1328257004.456  # as returned by time.time()
        self.assertEquals(
            times.to_universal(unix_time),
            datetime(2012, 2, 3, 8, 16, 44, 456000)
        )

    def test_to_universal_with_string(self):
        dt = self.sometime_univ

        # Timezone-aware strings
        self.assertEquals(dt, times.to_universal('2012-02-02 00:56:31+13:00'))
        self.assertEquals(dt, times.to_universal('2012-02-01 12:56:31+01:00'))
        self.assertEquals(dt, times.to_universal('2012-02-01 06:56:31-05:00'))

        # Timezone-less strings require explicit source timezone
        self.assertEquals(dt, times.to_universal('2012-02-02 00:56:31', 'Pacific/Auckland'))
        self.assertEquals(dt, times.to_universal('2012-02-01 12:56:31', 'CET'))
        self.assertEquals(dt, times.to_universal('2012-02-01 06:56:31', 'EST'))

        # Without a timezone, UTC is assumed
        self.assertEquals(dt, times.to_universal('2012-02-01 11:56:31'))

    def test_to_universal_rejects_non_date_arguments(self):
        """to_universal rejects non-date arguments"""
        with self.assertRaises(TypeError):
            times.to_universal([1, 2, 3])

    def test_convert_unix_time_to_datetime(self):  # noqa
        """Can convert from UNIX time to universal time."""
        unix_time = 1328257004.456  # as returned by time.time()
        self.assertEquals(
            times.from_unix(unix_time),
            datetime(2012, 2, 3, 8, 16, 44, 456000)
        )

        self.assertEquals(
            times.format(times.from_unix(unix_time), 'UTC'),
            '2012-02-03T08:16:44.456000+00:00')
        self.assertEquals(
            times.format(times.from_unix(unix_time), 'Europe/Amsterdam'),
            '2012-02-03T09:16:44.456000+01:00')
        self.assertEquals(
            times.format(times.from_unix(unix_time), 'Pacific/Auckland'),
            '2012-02-03T21:16:44.456000+13:00')

    def test_convert_non_numeric_from_unix(self):
        """from_unix refuses to accept non-numeric input"""
        with self.assertRaises(TypeError):
            times.from_unix('lol')

    def test_convert_datetime_to_unix_time(self):  # noqa
        """Can convert UNIX time to universal time."""
        self.assertEquals(
            times.to_unix(datetime(2012, 2, 3, 8, 16, 44)),
            1328257004.0
        )

    def test_convert_non_numeric_to_unix(self):
        """to_unix refuses to accept non-numeric input"""
        with self.assertRaises(TypeError):
            times.to_unix('lol')

    def test_convert_universal_to_local(self):  # noqa
        """Convert universal time to local time"""
        univ = self.sometime_univ
        self.assertEquals(times.to_local(univ, 'Europe/Amsterdam'),
                          self.time_in_ams)
        self.assertEquals(times.to_local(univ, 'EST'),
                          self.time_in_ny)

    def test_convert_refuses_local_to_local(self):
        """Refuses to convert between timezones directly"""
        loc = self.time_in_ams
        with self.assertRaises(ValueError):
            times.to_local(loc, 'Europe/Amsterdam')

    def test_format_without_tzinfo(self):  # noqa
        """Format times without timezone info"""
        dt = self.sometime_univ
        auckland = 'Pacific/Auckland'
        est = 'EST'
        ams = 'Europe/Amsterdam'
        self.assertEquals(times.format(dt, auckland),
                          '2012-02-02T00:56:31+13:00')
        self.assertEquals(times.format(dt, ams), '2012-02-01T12:56:31+01:00')
        self.assertEquals(times.format(dt, est), '2012-02-01T06:56:31-05:00')

    def test_custom_format(self):
        dt = self.sometime_univ
        auckland = 'Pacific/Auckland'
        est = 'EST'
        self.assertEquals(times.format(dt, auckland, '%H'), '00')
        self.assertEquals(times.format(dt, est, '%H'), '06')

    def test_format_refuses_local_times(self):
        """Format refuses local time input"""
        auckland = 'Pacific/Auckland'
        with self.assertRaises(ValueError):
            times.format(self.time_in_ams, auckland)

    def test_convert_between_unix_times(self):  # noqa
        """Can convert UNIX time to universal time and back."""
        given_unix = 1328257004.456  # as returned by time.time()
        self.assertEquals(
            times.to_unix(times.from_unix(given_unix)),
            int(given_unix)
        )

        given_dt = self.sometime_univ
        self.assertEquals(times.from_unix(times.to_unix(given_dt)),
                          given_dt)