| 242 | |
| 243 | def test_naturaltime_as_documented(self): |
| 244 | """ |
| 245 | Run tests that verify the documented behavior of humanize.naturaltime. |
| 246 | https://code.djangoproject.com/ticket/23340 |
| 247 | |
| 248 | As documented: |
| 249 | Examples (when 'now' is 17 Feb 2007 16:30:00): |
| 250 | |
| 251 | * ``17 Feb 2007 16:30:00`` becomes ``now``. |
| 252 | * ``17 Feb 2007 16:29:31`` becomes ``29 seconds ago``. |
| 253 | * ``17 Feb 2007 16:29:00`` becomes ``a minute ago``. |
| 254 | * ``17 Feb 2007 16:25:35`` becomes ``4 minutes ago``. |
| 255 | * ``17 Feb 2007 15:30:29`` becomes ``59 minutes ago``. |
| 256 | * ``17 Feb 2007 15:30:01`` becomes ``59 minutes ago``. |
| 257 | * ``17 Feb 2007 15:30:00`` becomes ``an hour ago``. |
| 258 | * ``17 Feb 2007 13:31:29`` becomes ``2 hours ago``. |
| 259 | * ``16 Feb 2007 13:31:29`` becomes ``1 day, 2 hours ago``. |
| 260 | * ``16 Feb 2007 13:30:01`` becomes ``1 day, 2 hours ago``. |
| 261 | * ``16 Feb 2007 13:30:00`` becomes ``1 day, 3 hours ago``. |
| 262 | * ``17 Feb 2007 16:30:30`` becomes ``30 seconds from now``. |
| 263 | * ``17 Feb 2007 16:30:29`` becomes ``29 seconds from now``. |
| 264 | * ``17 Feb 2007 16:31:00`` becomes ``a minute from now``. |
| 265 | * ``17 Feb 2007 16:34:35`` becomes ``4 minutes from now``. |
| 266 | * ``17 Feb 2007 17:30:29`` becomes ``an hour from now``. |
| 267 | * ``17 Feb 2007 18:31:29`` becomes ``2 hours from now``. |
| 268 | * ``18 Feb 2007 16:31:29`` becomes ``1 day from now``. |
| 269 | * ``26 Feb 2007 18:31:29`` becomes ``1 week, 2 days from now``. |
| 270 | """ |
| 271 | time_format = '%d %b %Y %H:%M:%S' |
| 272 | documented_now = datetime.datetime.strptime('17 Feb 2007 16:30:00', |
| 273 | time_format) |
| 274 | |
| 275 | class DocumentedMockDateTime(datetime.datetime): |
| 276 | """Override Class for humanize.datetime """ |
| 277 | @classmethod |
| 278 | def now(cls, tz=None): |
| 279 | if tz is None or tz.utcoffset(documented_now) is None: |
| 280 | return documented_now |
| 281 | else: |
| 282 | return documented_now.replace(tzinfo=tz) + tz.utcoffset(now) |
| 283 | |
| 284 | humanize.datetime = DocumentedMockDateTime |
| 285 | for test_time_string, expected_natural_time in ( |
| 286 | ('17 Feb 2007 16:30:00', 'now', ), |
| 287 | ('17 Feb 2007 16:29:31', '29 seconds ago', ), |
| 288 | ('17 Feb 2007 16:29:00', 'a minute ago', ), |
| 289 | ('17 Feb 2007 16:25:35', '4 minutes ago', ), |
| 290 | ('17 Feb 2007 15:30:29', '59 minutes ago', ), |
| 291 | ('17 Feb 2007 15:30:01', '59 minutes ago', ), |
| 292 | ('17 Feb 2007 15:30:00', 'an hour ago', ), |
| 293 | ('17 Feb 2007 13:31:29', '2 hours ago', ), |
| 294 | ('16 Feb 2007 13:31:29', '1 day, 2 hours ago', ), |
| 295 | ('16 Feb 2007 13:30:01', '1 day, 2 hours ago', ), |
| 296 | ('16 Feb 2007 13:30:00', '1 day, 3 hours ago', ), |
| 297 | ('17 Feb 2007 16:30:30', '30 seconds from now', ), |
| 298 | ('17 Feb 2007 16:30:29', '29 seconds from now', ), |
| 299 | ('17 Feb 2007 16:31:00', 'a minute from now', ), |
| 300 | ('17 Feb 2007 16:34:35', '4 minutes from now', ), |
| 301 | ('17 Feb 2007 17:30:29', 'an hour from now', ), |
| 302 | ('17 Feb 2007 18:31:29', '2 hours from now', ), |
| 303 | ('18 Feb 2007 16:31:29', '1 day from now', ), |
| 304 | ('26 Feb 2007 18:31:29', '1 week, 2 days from now', ), |
| 305 | ): |
| 306 | test_time = datetime.datetime.strptime(test_time_string, time_format) |
| 307 | natural_time = humanize.naturaltime(test_time).replace(u'\xa0', u' ') |
| 308 | self.assertEqual(expected_natural_time, natural_time) |