| 1 |
import datetime |
| 2 |
from decimal import Decimal |
| 3 |
|
| 4 |
from django.contrib.humanize.templatetags import humanize |
| 5 |
from django.template import Context, Template, defaultfilters |
| 6 |
from django.test import SimpleTestCase, modify_settings, override_settings |
| 7 |
from django.utils import translation |
| 8 |
from django.utils.html import escape |
| 9 |
from django.utils.timezone import get_fixed_timezone |
| 10 |
from django.utils.translation import gettext as _ |
| 11 |
|
| 12 |
# Mock out datetime in some tests so they don't fail occasionally when they |
| 13 |
# run too slow. Use a fixed datetime for datetime.now(). DST change in |
| 14 |
# America/Chicago (the default time zone) happened on March 11th in 2012. |
| 15 |
|
| 16 |
now = datetime.datetime(2012, 3, 9, 22, 30) |
| 17 |
|
| 18 |
|
| 19 |
class MockDateTime(datetime.datetime): |
| 20 |
@classmethod |
| 21 |
def now(cls, tz=None): |
| 22 |
if tz is None or tz.utcoffset(now) is None: |
| 23 |
return now |
| 24 |
else: |
| 25 |
# equals now.replace(tzinfo=utc) |
| 26 |
return now.replace(tzinfo=tz) + tz.utcoffset(now) |
| 27 |
|
| 28 |
|
| 29 |
@modify_settings(INSTALLED_APPS={"append": "django.contrib.humanize"}) |
| 30 |
class HumanizeTests(SimpleTestCase): |
| 31 |
def humanize_tester( |
| 32 |
self, test_list, result_list, method, normalize_result_func=escape |
| 33 |
): |
| 34 |
for test_content, result in zip(test_list, result_list): |
| 35 |
with self.subTest(test_content): |
| 36 |
t = Template("{%% load humanize %%}{{ test_content|%s }}" % method) |
| 37 |
rendered = t.render(Context(locals())).strip() |
| 38 |
self.assertEqual( |
| 39 |
rendered, |
| 40 |
normalize_result_func(result), |
| 41 |
msg="%s test failed, produced '%s', should've produced '%s'" |
| 42 |
% (method, rendered, result), |
| 43 |
) |
| 44 |
|
| 45 |
def test_ordinal(self): |
| 46 |
test_list = ( |
| 47 |
"1", |
| 48 |
"2", |
| 49 |
"3", |
| 50 |
"4", |
| 51 |
"11", |
| 52 |
"12", |
| 53 |
"13", |
| 54 |
"101", |
| 55 |
"102", |
| 56 |
"103", |
| 57 |
"111", |
| 58 |
"-0", |
| 59 |
"-1", |
| 60 |
"-105", |
| 61 |
"something else", |
| 62 |
None, |
| 63 |
) |
| 64 |
result_list = ( |
| 65 |
"1st", |
| 66 |
"2nd", |
| 67 |
"3rd", |
| 68 |
"4th", |
| 69 |
"11th", |
| 70 |
"12th", |
| 71 |
"13th", |
| 72 |
"101st", |
| 73 |
"102nd", |
| 74 |
"103rd", |
| 75 |
"111th", |
| 76 |
"0th", |
| 77 |
"-1", |
| 78 |
"-105", |
| 79 |
"something else", |
| 80 |
None, |
| 81 |
) |
| 82 |
|
| 83 |
with translation.override("en"): |
| 84 |
self.humanize_tester(test_list, result_list, "ordinal") |
| 85 |
|
| 86 |
def test_i18n_html_ordinal(self): |
| 87 |
"""Allow html in output on i18n strings""" |
| 88 |
test_list = ( |
| 89 |
"1", |
| 90 |
"2", |
| 91 |
"3", |
| 92 |
"4", |
| 93 |
"11", |
| 94 |
"12", |
| 95 |
"13", |
| 96 |
"101", |
| 97 |
"102", |
| 98 |
"103", |
| 99 |
"111", |
| 100 |
"something else", |
| 101 |
None, |
| 102 |
) |
| 103 |
result_list = ( |
| 104 |
"1<sup>er</sup>", |
| 105 |
"2<sup>e</sup>", |
| 106 |
"3<sup>e</sup>", |
| 107 |
"4<sup>e</sup>", |
| 108 |
"11<sup>e</sup>", |
| 109 |
"12<sup>e</sup>", |
| 110 |
"13<sup>e</sup>", |
| 111 |
"101<sup>er</sup>", |
| 112 |
"102<sup>e</sup>", |
| 113 |
"103<sup>e</sup>", |
| 114 |
"111<sup>e</sup>", |
| 115 |
"something else", |
| 116 |
"None", |
| 117 |
) |
| 118 |
|
| 119 |
with translation.override("fr-fr"): |
| 120 |
self.humanize_tester(test_list, result_list, "ordinal", lambda x: x) |
| 121 |
|
| 122 |
def test_intcomma(self): |
| 123 |
test_list = ( |
| 124 |
100, |
| 125 |
-100, |
| 126 |
1000, |
| 127 |
-1000, |
| 128 |
10123, |
| 129 |
-10123, |
| 130 |
10311, |
| 131 |
-10311, |
| 132 |
1000000, |
| 133 |
-1000000, |
| 134 |
1234567.25, |
| 135 |
-1234567.25, |
| 136 |
"100", |
| 137 |
"-100", |
| 138 |
"100.1", |
| 139 |
"-100.1", |
| 140 |
"100.13", |
| 141 |
"-100.13", |
| 142 |
"1000", |
| 143 |
"-1000", |
| 144 |
"10123", |
| 145 |
"-10123", |
| 146 |
"10311", |
| 147 |
"-10311", |
| 148 |
"100000.13", |
| 149 |
"-100000.13", |
| 150 |
"1000000", |
| 151 |
"-1000000", |
| 152 |
"1234567.1234567", |
| 153 |
"-1234567.1234567", |
| 154 |
Decimal("1234567.1234567"), |
| 155 |
Decimal("-1234567.1234567"), |
| 156 |
None, |
| 157 |
"1234567", |
| 158 |
"-1234567", |
| 159 |
"1234567.12", |
| 160 |
"-1234567.12", |
| 161 |
"the quick brown fox jumped over the lazy dog", |
| 162 |
) |
| 163 |
result_list = ( |
| 164 |
"100", |
| 165 |
"-100", |
| 166 |
"1,000", |
| 167 |
"-1,000", |
| 168 |
"10,123", |
| 169 |
"-10,123", |
| 170 |
"10,311", |
| 171 |
"-10,311", |
| 172 |
"1,000,000", |
| 173 |
"-1,000,000", |
| 174 |
"1,234,567.25", |
| 175 |
"-1,234,567.25", |
| 176 |
"100", |
| 177 |
"-100", |
| 178 |
"100.1", |
| 179 |
"-100.1", |
| 180 |
"100.13", |
| 181 |
"-100.13", |
| 182 |
"1,000", |
| 183 |
"-1,000", |
| 184 |
"10,123", |
| 185 |
"-10,123", |
| 186 |
"10,311", |
| 187 |
"-10,311", |
| 188 |
"100,000.13", |
| 189 |
"-100,000.13", |
| 190 |
"1,000,000", |
| 191 |
"-1,000,000", |
| 192 |
"1,234,567.1234567", |
| 193 |
"-1,234,567.1234567", |
| 194 |
"1,234,567.1234567", |
| 195 |
"-1,234,567.1234567", |
| 196 |
None, |
| 197 |
"1,234,567", |
| 198 |
"-1,234,567", |
| 199 |
"1,234,567.12", |
| 200 |
"-1,234,567.12", |
| 201 |
"the quick brown fox jumped over the lazy dog", |
| 202 |
) |
| 203 |
with translation.override("en"): |
| 204 |
self.humanize_tester(test_list, result_list, "intcomma") |
| 205 |
|
| 206 |
def test_l10n_intcomma(self): |
| 207 |
test_list = ( |
| 208 |
100, |
| 209 |
-100, |
| 210 |
1000, |
| 211 |
-1000, |
| 212 |
10123, |
| 213 |
-10123, |
| 214 |
10311, |
| 215 |
-10311, |
| 216 |
1000000, |
| 217 |
-1000000, |
| 218 |
1234567.25, |
| 219 |
-1234567.25, |
| 220 |
"100", |
| 221 |
"-100", |
| 222 |
"1000", |
| 223 |
"-1000", |
| 224 |
"10123", |
| 225 |
"-10123", |
| 226 |
"10311", |
| 227 |
"-10311", |
| 228 |
"1000000", |
| 229 |
"-1000000", |
| 230 |
"1234567.1234567", |
| 231 |
"-1234567.1234567", |
| 232 |
Decimal("1234567.1234567"), |
| 233 |
-Decimal("1234567.1234567"), |
| 234 |
None, |
| 235 |
"1234567", |
| 236 |
"-1234567", |
| 237 |
"1234567.12", |
| 238 |
"-1234567.12", |
| 239 |
"the quick brown fox jumped over the lazy dog", |
| 240 |
) |
| 241 |
result_list = ( |
| 242 |
"100", |
| 243 |
"-100", |
| 244 |
"1,000", |
| 245 |
"-1,000", |
| 246 |
"10,123", |
| 247 |
"-10,123", |
| 248 |
"10,311", |
| 249 |
"-10,311", |
| 250 |
"1,000,000", |
| 251 |
"-1,000,000", |
| 252 |
"1,234,567.25", |
| 253 |
"-1,234,567.25", |
| 254 |
"100", |
| 255 |
"-100", |
| 256 |
"1,000", |
| 257 |
"-1,000", |
| 258 |
"10,123", |
| 259 |
"-10,123", |
| 260 |
"10,311", |
| 261 |
"-10,311", |
| 262 |
"1,000,000", |
| 263 |
"-1,000,000", |
| 264 |
"1,234,567.1234567", |
| 265 |
"-1,234,567.1234567", |
| 266 |
"1,234,567.1234567", |
| 267 |
"-1,234,567.1234567", |
| 268 |
None, |
| 269 |
"1,234,567", |
| 270 |
"-1,234,567", |
| 271 |
"1,234,567.12", |
| 272 |
"-1,234,567.12", |
| 273 |
"the quick brown fox jumped over the lazy dog", |
| 274 |
) |
| 275 |
with self.settings(USE_THOUSAND_SEPARATOR=False): |
| 276 |
with translation.override("en"): |
| 277 |
self.humanize_tester(test_list, result_list, "intcomma") |
| 278 |
|
| 279 |
def test_intcomma_without_number_grouping(self): |
| 280 |
# Regression for #17414 |
| 281 |
with translation.override("ja"): |
| 282 |
self.humanize_tester([100], ["100"], "intcomma") |
| 283 |
|
| 284 |
def test_intword(self): |
| 285 |
# Positive integers. |
| 286 |
test_list_positive = ( |
| 287 |
"100", |
| 288 |
"1000000", |
| 289 |
"1200000", |
| 290 |
"1290000", |
| 291 |
"1000000000", |
| 292 |
"2000000000", |
| 293 |
"6000000000000", |
| 294 |
"1300000000000000", |
| 295 |
"3500000000000000000000", |
| 296 |
"8100000000000000000000000000000000", |
| 297 |
("1" + "0" * 100), |
| 298 |
("1" + "0" * 104), |
| 299 |
) |
| 300 |
result_list_positive = ( |
| 301 |
"100", |
| 302 |
"1.0 million", |
| 303 |
"1.2 million", |
| 304 |
"1.3 million", |
| 305 |
"1.0 billion", |
| 306 |
"2.0 billion", |
| 307 |
"6.0 trillion", |
| 308 |
"1.3 quadrillion", |
| 309 |
"3.5 sextillion", |
| 310 |
"8.1 decillion", |
| 311 |
"1.0 googol", |
| 312 |
("1" + "0" * 104), |
| 313 |
) |
| 314 |
# Negative integers. |
| 315 |
test_list_negative = ("-" + test for test in test_list_positive) |
| 316 |
result_list_negative = ("-" + result for result in result_list_positive) |
| 317 |
with translation.override("en"): |
| 318 |
self.humanize_tester( |
| 319 |
(*test_list_positive, *test_list_negative, None), |
| 320 |
(*result_list_positive, *result_list_negative, None), |
| 321 |
"intword", |
| 322 |
) |
| 323 |
|
| 324 |
def test_i18n_intcomma(self): |
| 325 |
test_list = ( |
| 326 |
100, |
| 327 |
1000, |
| 328 |
10123, |
| 329 |
10311, |
| 330 |
1000000, |
| 331 |
1234567.25, |
| 332 |
"100", |
| 333 |
"1000", |
| 334 |
"10123", |
| 335 |
"10311", |
| 336 |
"1000000", |
| 337 |
None, |
| 338 |
) |
| 339 |
result_list = ( |
| 340 |
"100", |
| 341 |
"1.000", |
| 342 |
"10.123", |
| 343 |
"10.311", |
| 344 |
"1.000.000", |
| 345 |
"1.234.567,25", |
| 346 |
"100", |
| 347 |
"1.000", |
| 348 |
"10.123", |
| 349 |
"10.311", |
| 350 |
"1.000.000", |
| 351 |
None, |
| 352 |
) |
| 353 |
with self.settings(USE_THOUSAND_SEPARATOR=True): |
| 354 |
with translation.override("de"): |
| 355 |
self.humanize_tester(test_list, result_list, "intcomma") |
| 356 |
|
| 357 |
def test_i18n_intword(self): |
| 358 |
# Positive integers. |
| 359 |
test_list_positive = ( |
| 360 |
"100", |
| 361 |
"1000000", |
| 362 |
"1200000", |
| 363 |
"1290000", |
| 364 |
"1000000000", |
| 365 |
"2000000000", |
| 366 |
"6000000000000", |
| 367 |
) |
| 368 |
result_list_positive = ( |
| 369 |
"100", |
| 370 |
"1,0 Million", |
| 371 |
"1,2 Millionen", |
| 372 |
"1,3 Millionen", |
| 373 |
"1,0 Milliarde", |
| 374 |
"2,0 Milliarden", |
| 375 |
"6,0 Billionen", |
| 376 |
) |
| 377 |
# Negative integers. |
| 378 |
test_list_negative = ("-" + test for test in test_list_positive) |
| 379 |
result_list_negative = ("-" + result for result in result_list_positive) |
| 380 |
with self.settings(USE_THOUSAND_SEPARATOR=True): |
| 381 |
with translation.override("de"): |
| 382 |
self.humanize_tester( |
| 383 |
(*test_list_positive, *test_list_negative), |
| 384 |
(*result_list_positive, *result_list_negative), |
| 385 |
"intword", |
| 386 |
) |
| 387 |
|
| 388 |
def test_apnumber(self): |
| 389 |
test_list = [str(x) for x in range(1, 11)] |
| 390 |
test_list.append(None) |
| 391 |
result_list = ( |
| 392 |
"one", |
| 393 |
"two", |
| 394 |
"three", |
| 395 |
"four", |
| 396 |
"five", |
| 397 |
"six", |
| 398 |
"seven", |
| 399 |
"eight", |
| 400 |
"nine", |
| 401 |
"10", |
| 402 |
None, |
| 403 |
) |
| 404 |
with translation.override("en"): |
| 405 |
self.humanize_tester(test_list, result_list, "apnumber") |
| 406 |
|
| 407 |
def test_naturalday(self): |
| 408 |
today = datetime.date.today() |
| 409 |
yesterday = today - datetime.timedelta(days=1) |
| 410 |
tomorrow = today + datetime.timedelta(days=1) |
| 411 |
someday = today - datetime.timedelta(days=10) |
| 412 |
notdate = "I'm not a date value" |
| 413 |
|
| 414 |
test_list = (today, yesterday, tomorrow, someday, notdate, None) |
| 415 |
someday_result = defaultfilters.date(someday) |
| 416 |
result_list = ( |
| 417 |
_("today"), |
| 418 |
_("yesterday"), |
| 419 |
_("tomorrow"), |
| 420 |
someday_result, |
| 421 |
"I'm not a date value", |
| 422 |
None, |
| 423 |
) |
| 424 |
self.humanize_tester(test_list, result_list, "naturalday") |
| 425 |
|
| 426 |
def test_naturalday_tz(self): |
| 427 |
today = datetime.date.today() |
| 428 |
tz_one = get_fixed_timezone(-720) |
| 429 |
tz_two = get_fixed_timezone(720) |
| 430 |
|
| 431 |
# Can be today or yesterday |
| 432 |
date_one = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_one) |
| 433 |
naturalday_one = humanize.naturalday(date_one) |
| 434 |
# Can be today or tomorrow |
| 435 |
date_two = datetime.datetime(today.year, today.month, today.day, tzinfo=tz_two) |
| 436 |
naturalday_two = humanize.naturalday(date_two) |
| 437 |
|
| 438 |
# As 24h of difference they will never be the same |
| 439 |
self.assertNotEqual(naturalday_one, naturalday_two) |
| 440 |
|
| 441 |
def test_naturalday_uses_localtime(self): |
| 442 |
# Regression for #18504 |
| 443 |
# This is 2012-03-08HT19:30:00-06:00 in America/Chicago |
| 444 |
dt = datetime.datetime(2012, 3, 9, 1, 30, tzinfo=datetime.timezone.utc) |
| 445 |
|
| 446 |
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime |
| 447 |
try: |
| 448 |
with override_settings(TIME_ZONE="America/Chicago", USE_TZ=True): |
| 449 |
with translation.override("en"): |
| 450 |
self.humanize_tester([dt], ["yesterday"], "naturalday") |
| 451 |
finally: |
| 452 |
humanize.datetime = orig_humanize_datetime |
| 453 |
|
| 454 |
def test_naturaltime(self): |
| 455 |
class naive(datetime.tzinfo): |
| 456 |
def utcoffset(self, dt): |
| 457 |
return None |
| 458 |
|
| 459 |
test_list = [ |
| 460 |
"test", |
| 461 |
now, |
| 462 |
now - datetime.timedelta(microseconds=1), |
| 463 |
now - datetime.timedelta(seconds=1), |
| 464 |
now - datetime.timedelta(seconds=30), |
| 465 |
now - datetime.timedelta(minutes=1, seconds=30), |
| 466 |
now - datetime.timedelta(minutes=2), |
| 467 |
now - datetime.timedelta(hours=1, minutes=30, seconds=30), |
| 468 |
now - datetime.timedelta(hours=23, minutes=50, seconds=50), |
| 469 |
now - datetime.timedelta(days=1), |
| 470 |
now - datetime.timedelta(days=500), |
| 471 |
now + datetime.timedelta(seconds=1), |
| 472 |
now + datetime.timedelta(seconds=30), |
| 473 |
now + datetime.timedelta(minutes=1, seconds=30), |
| 474 |
now + datetime.timedelta(minutes=2), |
| 475 |
now + datetime.timedelta(hours=1, minutes=30, seconds=30), |
| 476 |
now + datetime.timedelta(hours=23, minutes=50, seconds=50), |
| 477 |
now + datetime.timedelta(days=1), |
| 478 |
now + datetime.timedelta(days=2, hours=6), |
| 479 |
now + datetime.timedelta(days=500), |
| 480 |
now.replace(tzinfo=naive()), |
| 481 |
now.replace(tzinfo=datetime.timezone.utc), |
| 482 |
] |
| 483 |
result_list = [ |
| 484 |
"test", |
| 485 |
"now", |
| 486 |
"now", |
| 487 |
"a second ago", |
| 488 |
"30\xa0seconds ago", |
| 489 |
"a minute ago", |
| 490 |
"2\xa0minutes ago", |
| 491 |
"an hour ago", |
| 492 |
"23\xa0hours ago", |
| 493 |
"1\xa0day ago", |
| 494 |
"1\xa0year, 4\xa0months ago", |
| 495 |
"a second from now", |
| 496 |
"30\xa0seconds from now", |
| 497 |
"a minute from now", |
| 498 |
"2\xa0minutes from now", |
| 499 |
"an hour from now", |
| 500 |
"23\xa0hours from now", |
| 501 |
"1\xa0day from now", |
| 502 |
"2\xa0days, 6\xa0hours from now", |
| 503 |
"1\xa0year, 4\xa0months from now", |
| 504 |
"now", |
| 505 |
"now", |
| 506 |
] |
| 507 |
# Because of the DST change, 2 days and 6 hours after the chosen |
| 508 |
# date in naive arithmetic is only 2 days and 5 hours after in |
| 509 |
# aware arithmetic. |
| 510 |
result_list_with_tz_support = result_list[:] |
| 511 |
assert result_list_with_tz_support[-4] == "2\xa0days, 6\xa0hours from now" |
| 512 |
result_list_with_tz_support[-4] == "2\xa0days, 5\xa0hours from now" |
| 513 |
|
| 514 |
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime |
| 515 |
try: |
| 516 |
with translation.override("en"): |
| 517 |
self.humanize_tester(test_list, result_list, "naturaltime") |
| 518 |
with override_settings(USE_TZ=True): |
| 519 |
self.humanize_tester( |
| 520 |
test_list, result_list_with_tz_support, "naturaltime" |
| 521 |
) |
| 522 |
finally: |
| 523 |
humanize.datetime = orig_humanize_datetime |
| 524 |
|
| 525 |
def test_naturaltime_as_documented(self): |
| 526 |
""" |
| 527 |
#23340 -- Verify the documented behavior of humanize.naturaltime. |
| 528 |
""" |
| 529 |
time_format = "%d %b %Y %H:%M:%S" |
| 530 |
documented_now = datetime.datetime.strptime("17 Feb 2007 16:30:00", time_format) |
| 531 |
|
| 532 |
test_data = ( |
| 533 |
("17 Feb 2007 16:30:00", "now"), |
| 534 |
("17 Feb 2007 16:29:31", "29 seconds ago"), |
| 535 |
("17 Feb 2007 16:29:00", "a minute ago"), |
| 536 |
("17 Feb 2007 16:25:35", "4 minutes ago"), |
| 537 |
("17 Feb 2007 15:30:29", "59 minutes ago"), |
| 538 |
("17 Feb 2007 15:30:01", "59 minutes ago"), |
| 539 |
("17 Feb 2007 15:30:00", "an hour ago"), |
| 540 |
("17 Feb 2007 13:31:29", "2 hours ago"), |
| 541 |
("16 Feb 2007 13:31:29", "1 day, 2 hours ago"), |
| 542 |
("16 Feb 2007 13:30:01", "1 day, 2 hours ago"), |
| 543 |
("16 Feb 2007 13:30:00", "1 day, 3 hours ago"), |
| 544 |
("17 Feb 2007 16:30:30", "30 seconds from now"), |
| 545 |
("17 Feb 2007 16:30:29", "29 seconds from now"), |
| 546 |
("17 Feb 2007 16:31:00", "a minute from now"), |
| 547 |
("17 Feb 2007 16:34:35", "4 minutes from now"), |
| 548 |
("17 Feb 2007 17:30:29", "an hour from now"), |
| 549 |
("17 Feb 2007 18:31:29", "2 hours from now"), |
| 550 |
("18 Feb 2007 16:31:29", "1 day from now"), |
| 551 |
("26 Feb 2007 18:31:29", "1 week, 2 days from now"), |
| 552 |
) |
| 553 |
|
| 554 |
class DocumentedMockDateTime(datetime.datetime): |
| 555 |
@classmethod |
| 556 |
def now(cls, tz=None): |
| 557 |
if tz is None or tz.utcoffset(documented_now) is None: |
| 558 |
return documented_now |
| 559 |
else: |
| 560 |
return documented_now.replace(tzinfo=tz) + tz.utcoffset(now) |
| 561 |
|
| 562 |
orig_humanize_datetime = humanize.datetime |
| 563 |
humanize.datetime = DocumentedMockDateTime |
| 564 |
try: |
| 565 |
for test_time_string, expected_natural_time in test_data: |
| 566 |
with self.subTest(test_time_string): |
| 567 |
test_time = datetime.datetime.strptime( |
| 568 |
test_time_string, time_format |
| 569 |
) |
| 570 |
natural_time = humanize.naturaltime(test_time).replace("\xa0", " ") |
| 571 |
self.assertEqual(expected_natural_time, natural_time) |
| 572 |
finally: |
| 573 |
humanize.datetime = orig_humanize_datetime |
| 574 |
|
| 575 |
def test_inflection_for_timedelta(self): |
| 576 |
""" |
| 577 |
Translation of '%d day'/'%d month'/… may differ depending on the context |
| 578 |
of the string it is inserted in. |
| 579 |
""" |
| 580 |
test_list = [ |
| 581 |
# "%(delta)s ago" translations |
| 582 |
now - datetime.timedelta(days=1), |
| 583 |
now - datetime.timedelta(days=2), |
| 584 |
now - datetime.timedelta(days=30), |
| 585 |
now - datetime.timedelta(days=60), |
| 586 |
now - datetime.timedelta(days=500), |
| 587 |
now - datetime.timedelta(days=865), |
| 588 |
# "%(delta)s from now" translations |
| 589 |
now + datetime.timedelta(days=1), |
| 590 |
now + datetime.timedelta(days=2), |
| 591 |
now + datetime.timedelta(days=31), |
| 592 |
now + datetime.timedelta(days=61), |
| 593 |
now + datetime.timedelta(days=500), |
| 594 |
now + datetime.timedelta(days=865), |
| 595 |
] |
| 596 |
result_list = [ |
| 597 |
"před 1\xa0dnem", |
| 598 |
"před 2\xa0dny", |
| 599 |
"před 1\xa0měsícem", |
| 600 |
"před 2\xa0měsíci", |
| 601 |
"před 1\xa0rokem, 4\xa0měsíci", |
| 602 |
"před 2\xa0lety, 4\xa0měsíci", |
| 603 |
"za 1\xa0den", |
| 604 |
"za 2\xa0dny", |
| 605 |
"za 1\xa0měsíc", |
| 606 |
"za 2\xa0měsíce", |
| 607 |
"za 1\xa0rok, 4\xa0měsíce", |
| 608 |
"za 2\xa0roky, 4\xa0měsíce", |
| 609 |
] |
| 610 |
|
| 611 |
orig_humanize_datetime, humanize.datetime = humanize.datetime, MockDateTime |
| 612 |
try: |
| 613 |
# Choose a language with different |
| 614 |
# naturaltime-past/naturaltime-future translations. |
| 615 |
with translation.override("cs"): |
| 616 |
self.humanize_tester(test_list, result_list, "naturaltime") |
| 617 |
finally: |
| 618 |
humanize.datetime = orig_humanize_datetime |