rfc2579_date_time.py 10.3 KB
Newer Older
1
#!/usr/bin/env python
2 3 4
# -*- coding: utf-8 -*-
"""Tests for the RFC2579 date-time implementation."""

5 6
from __future__ import unicode_literals

7
import decimal
8 9 10 11 12
import unittest

from dfdatetime import rfc2579_date_time


13
class RFC2579DateTimeInvalidYear(rfc2579_date_time.RFC2579DateTime):
14
  """RFC2579 date-time for testing invalid year."""
15

16
  def _CopyDateTimeFromString(self, time_string):
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    """Copies a date and time from a string.

    Args:
      time_string (str): date and time value formatted as:
          YYYY-MM-DD hh:mm:ss.######[+-]##:##

          Where # are numeric digits ranging from 0 to 9 and the seconds
          fraction can be either 3 or 6 digits. The time of day, seconds
          fraction and time zone offset are optional. The default time zone
          is UTC.

    Returns:
      dict[str, int]: date and time values, such as year, month, day of month,
          hours, minutes, seconds, microseconds.

    Raises:
      ValueError: if the time string is invalid or not supported.
    """
    return {
36 37 38 39 40 41
        'year': 70000,
        'month': 1,
        'day_of_month': 2,
        'hours': 0,
        'minutes': 0,
        'seconds': 0}
42 43 44


class RFC2579DateTimeTest(unittest.TestCase):
45 46 47 48 49 50 51 52 53 54
  """Tests for the RFC2579 date-time."""

  # pylint: disable=protected-access

  def testInitialize(self):
    """Tests the initialization function."""
    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime()
    self.assertIsNotNone(rfc2579_date_time_object)

    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime(
55
        rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '+', 2, 0))
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    self.assertIsNotNone(rfc2579_date_time_object)
    self.assertEqual(rfc2579_date_time_object.year, 2010)
    self.assertEqual(rfc2579_date_time_object.month, 8)
    self.assertEqual(rfc2579_date_time_object.day_of_month, 12)
    self.assertEqual(rfc2579_date_time_object.hours, 18)
    self.assertEqual(rfc2579_date_time_object.minutes, 6)
    self.assertEqual(rfc2579_date_time_object.seconds, 31)
    self.assertEqual(rfc2579_date_time_object.deciseconds, 6)

    with self.assertRaises(ValueError):
      rfc2579_date_time.RFC2579DateTime(
          rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31))

    with self.assertRaises(ValueError):
      rfc2579_date_time.RFC2579DateTime(
71
          rfc2579_date_time_tuple=(65537, 8, 12, 20, 6, 31, 6, '+', 2, 0))
72 73 74

    with self.assertRaises(ValueError):
      rfc2579_date_time.RFC2579DateTime(
75
          rfc2579_date_time_tuple=(2010, 13, 12, 20, 6, 31, 6, '+', 2, 0))
76 77 78

    with self.assertRaises(ValueError):
      rfc2579_date_time.RFC2579DateTime(
79
          rfc2579_date_time_tuple=(2010, 8, 32, 20, 6, 31, 6, '+', 2, 0))
80 81 82

    with self.assertRaises(ValueError):
      rfc2579_date_time.RFC2579DateTime(
83
          rfc2579_date_time_tuple=(2010, 8, 12, 24, 6, 31, 6, '+', 2, 0))
84 85 86

    with self.assertRaises(ValueError):
      rfc2579_date_time.RFC2579DateTime(
87
          rfc2579_date_time_tuple=(2010, 8, 12, 20, 61, 31, 6, '+', 2, 0))
88 89 90

    with self.assertRaises(ValueError):
      rfc2579_date_time.RFC2579DateTime(
91
          rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 61, 6, '+', 2, 0))
92 93 94

    with self.assertRaises(ValueError):
      rfc2579_date_time.RFC2579DateTime(
95
          rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 11, '+', 2, 0))
96 97 98

    with self.assertRaises(ValueError):
      rfc2579_date_time.RFC2579DateTime(
99
          rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '#', 2, 0))
100 101 102

    with self.assertRaises(ValueError):
      rfc2579_date_time.RFC2579DateTime(
103
          rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '+', 14, 0))
104 105 106

    with self.assertRaises(ValueError):
      rfc2579_date_time.RFC2579DateTime(
107
          rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '+', 2, 60))
108

109 110 111 112 113 114 115 116 117 118 119 120 121
  def testGetNormalizedTimestamp(self):
    """Tests the _GetNormalizedTimestamp function."""
    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime(
        rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '+', 0, 0))

    normalized_timestamp = rfc2579_date_time_object._GetNormalizedTimestamp()
    self.assertEqual(normalized_timestamp, decimal.Decimal('1281643591.6'))

    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime()

    normalized_timestamp = rfc2579_date_time_object._GetNormalizedTimestamp()
    self.assertIsNone(normalized_timestamp)

122 123
  def testCopyFromDateTimeString(self):
    """Tests the CopyFromDateTimeString function."""
124 125 126
    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime()

    expected_number_of_seconds = 1281571200
127
    rfc2579_date_time_object.CopyFromDateTimeString('2010-08-12')
128 129 130 131 132 133 134 135 136 137 138
    self.assertEqual(
        rfc2579_date_time_object._number_of_seconds, expected_number_of_seconds)
    self.assertEqual(rfc2579_date_time_object.year, 2010)
    self.assertEqual(rfc2579_date_time_object.month, 8)
    self.assertEqual(rfc2579_date_time_object.day_of_month, 12)
    self.assertEqual(rfc2579_date_time_object.hours, 0)
    self.assertEqual(rfc2579_date_time_object.minutes, 0)
    self.assertEqual(rfc2579_date_time_object.seconds, 0)
    self.assertEqual(rfc2579_date_time_object.deciseconds, 0)

    expected_number_of_seconds = 1281647191
139
    rfc2579_date_time_object.CopyFromDateTimeString('2010-08-12 21:06:31')
140 141 142 143 144 145 146 147 148 149 150
    self.assertEqual(
        rfc2579_date_time_object._number_of_seconds, expected_number_of_seconds)
    self.assertEqual(rfc2579_date_time_object.year, 2010)
    self.assertEqual(rfc2579_date_time_object.month, 8)
    self.assertEqual(rfc2579_date_time_object.day_of_month, 12)
    self.assertEqual(rfc2579_date_time_object.hours, 21)
    self.assertEqual(rfc2579_date_time_object.minutes, 6)
    self.assertEqual(rfc2579_date_time_object.seconds, 31)
    self.assertEqual(rfc2579_date_time_object.deciseconds, 0)

    expected_number_of_seconds = 1281647191
151 152
    rfc2579_date_time_object.CopyFromDateTimeString(
        '2010-08-12 21:06:31.546875')
153 154 155 156 157 158 159 160 161 162 163
    self.assertEqual(
        rfc2579_date_time_object._number_of_seconds, expected_number_of_seconds)
    self.assertEqual(rfc2579_date_time_object.year, 2010)
    self.assertEqual(rfc2579_date_time_object.month, 8)
    self.assertEqual(rfc2579_date_time_object.day_of_month, 12)
    self.assertEqual(rfc2579_date_time_object.hours, 21)
    self.assertEqual(rfc2579_date_time_object.minutes, 6)
    self.assertEqual(rfc2579_date_time_object.seconds, 31)
    self.assertEqual(rfc2579_date_time_object.deciseconds, 5)

    expected_number_of_seconds = 1281650791
164 165
    rfc2579_date_time_object.CopyFromDateTimeString(
        '2010-08-12 21:06:31.546875-01:00')
166 167 168 169 170 171 172 173 174 175 176
    self.assertEqual(
        rfc2579_date_time_object._number_of_seconds, expected_number_of_seconds)
    self.assertEqual(rfc2579_date_time_object.year, 2010)
    self.assertEqual(rfc2579_date_time_object.month, 8)
    self.assertEqual(rfc2579_date_time_object.day_of_month, 12)
    self.assertEqual(rfc2579_date_time_object.hours, 22)
    self.assertEqual(rfc2579_date_time_object.minutes, 6)
    self.assertEqual(rfc2579_date_time_object.seconds, 31)
    self.assertEqual(rfc2579_date_time_object.deciseconds, 5)

    expected_number_of_seconds = 1281643591
177 178
    rfc2579_date_time_object.CopyFromDateTimeString(
        '2010-08-12 21:06:31.546875+01:00')
179 180 181 182 183 184 185 186 187 188 189
    self.assertEqual(
        rfc2579_date_time_object._number_of_seconds, expected_number_of_seconds)
    self.assertEqual(rfc2579_date_time_object.year, 2010)
    self.assertEqual(rfc2579_date_time_object.month, 8)
    self.assertEqual(rfc2579_date_time_object.day_of_month, 12)
    self.assertEqual(rfc2579_date_time_object.hours, 20)
    self.assertEqual(rfc2579_date_time_object.minutes, 6)
    self.assertEqual(rfc2579_date_time_object.seconds, 31)
    self.assertEqual(rfc2579_date_time_object.deciseconds, 5)

    expected_number_of_seconds = -11644387200
190
    rfc2579_date_time_object.CopyFromDateTimeString('1601-01-02 00:00:00')
191 192 193 194 195 196 197 198 199 200
    self.assertEqual(
        rfc2579_date_time_object._number_of_seconds, expected_number_of_seconds)
    self.assertEqual(rfc2579_date_time_object.year, 1601)
    self.assertEqual(rfc2579_date_time_object.month, 1)
    self.assertEqual(rfc2579_date_time_object.day_of_month, 2)
    self.assertEqual(rfc2579_date_time_object.hours, 0)
    self.assertEqual(rfc2579_date_time_object.minutes, 0)
    self.assertEqual(rfc2579_date_time_object.seconds, 0)
    self.assertEqual(rfc2579_date_time_object.deciseconds, 0)

201 202
    rfc2579_date_time_object = RFC2579DateTimeInvalidYear()

203
    with self.assertRaises(ValueError):
204
      rfc2579_date_time_object.CopyFromDateTimeString('9999-01-02 00:00:00')
205

206 207 208 209 210 211
  def testCopyToDateTimeString(self):
    """Tests the CopyToDateTimeString function."""
    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime(
        rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '+', 0, 0))

    date_time_string = rfc2579_date_time_object.CopyToDateTimeString()
212
    self.assertEqual(date_time_string, '2010-08-12 20:06:31.6')
213 214 215 216 217 218

    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime()

    date_time_string = rfc2579_date_time_object.CopyToDateTimeString()
    self.assertIsNone(date_time_string)

219 220 221 222 223 224 225 226
  def testCopyToDateTimeStringISO8601(self):
    """Tests the CopyToDateTimeStringISO8601 function."""
    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime(
        rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '+', 0, 0))

    date_time_string = rfc2579_date_time_object.CopyToDateTimeStringISO8601()
    self.assertEqual(date_time_string, '2010-08-12T20:06:31.6Z')

227 228
  def testGetDate(self):
    """Tests the GetDate function."""
229
    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime(
230
        rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '+', 0, 0))
231

232 233
    date_tuple = rfc2579_date_time_object.GetDate()
    self.assertEqual(date_tuple, (2010, 8, 12))
234 235 236

    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime()

237 238
    date_tuple = rfc2579_date_time_object.GetDate()
    self.assertEqual(date_tuple, (None, None, None))
239

240 241 242 243 244 245 246 247 248 249 250 251 252
  def testGetTimeOfDay(self):
    """Tests the GetTimeOfDay function."""
    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime(
        rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, '+', 0, 0))

    time_of_day_tuple = rfc2579_date_time_object.GetTimeOfDay()
    self.assertEqual(time_of_day_tuple, (20, 6, 31))

    rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime()

    time_of_day_tuple = rfc2579_date_time_object.GetTimeOfDay()
    self.assertEqual(time_of_day_tuple, (None, None, None))

253 254 255

if __name__ == '__main__':
  unittest.main()