def naturaltime(dt):
    # https://github.com/jmoiron/humanize/issues/46#issuecomment-311973356
    if dt.tzinfo is None:
        return humanize_time.naturaltime(dt)

    with mock.patch.object(humanize_time, '_now', side_effect=_now):
        return humanize_time.naturaltime(dt)
Esempio n. 2
0
def humanize_naturaltime(eval_ctx,
                         value,
                         future=False,
                         months=True,
                         minimum_unit="seconds",
                         when=None):
    return naturaltime(value,
                       future=future,
                       months=months,
                       minimum_unit=minimum_unit,
                       when=when)
Esempio n. 3
0
def naturaltime(*args, **kwargs):
    """ Wrap `humanize.naturaltime` into django_language(). """

    with django_language():
        return humanize_time.naturaltime(*args, **kwargs)
Esempio n. 4
0
 def test_naturaltime_nomonths(self):
     now = datetime.now()
     test_list = [
         now,
         now - timedelta(seconds=1),
         now - timedelta(seconds=30),
         now - timedelta(minutes=1, seconds=30),
         now - timedelta(minutes=2),
         now - timedelta(hours=1, minutes=30, seconds=30),
         now - timedelta(hours=23, minutes=50, seconds=50),
         now - timedelta(days=1),
         now - timedelta(days=17),
         now - timedelta(days=47),
         now - timedelta(days=500),
         now - timedelta(days=365*2 + 35),
         now + timedelta(seconds=1),
         now + timedelta(seconds=30),
         now + timedelta(minutes=1, seconds=30),
         now + timedelta(minutes=2),
         now + timedelta(hours=1, minutes=30, seconds=30),
         now + timedelta(hours=23, minutes=50, seconds=50),
         now + timedelta(days=1),
         now + timedelta(days=500),
         now + timedelta(days=365*2 + 35),
         # regression tests for bugs in post-release humanize
         now + timedelta(days=10000),
         now - timedelta(days=365+35),
         30,
         now - timedelta(days=365*2 + 65),
         now - timedelta(days=365 + 4),
         "NaN",
     ]
     result_list = [
         'now',
         'a second ago',
         '30 seconds ago',
         'a minute ago',
         '2 minutes ago',
         'an hour ago',
         '23 hours ago',
         'a day ago',
         '17 days ago',
         '47 days ago',
         '1 year, 135 days ago',
         '2 years ago',
         'a second from now',
         '30 seconds from now',
         'a minute from now',
         '2 minutes from now',
         'an hour from now',
         '23 hours from now',
         'a day from now',
         '1 year, 135 days from now',
         '2 years from now',
         '27 years from now',
         '1 year, 35 days ago',
         '30 seconds ago',
         '2 years ago',
         '1 year, 4 days ago',
         "NaN",
     ]
     with patch('humanize.time._now') as mocked:
         mocked.return_value = now
         nt_nomonths = lambda d: time.naturaltime(d, months=False)
         self.assertManyResults(nt_nomonths, test_list, result_list)
Esempio n. 5
0
 def test_naturaltime_nomonths(self):
     now = datetime.now()
     test_list = [
         now,
         now - timedelta(seconds=1),
         now - timedelta(seconds=30),
         now - timedelta(minutes=1, seconds=30),
         now - timedelta(minutes=2),
         now - timedelta(hours=1, minutes=30, seconds=30),
         now - timedelta(hours=23, minutes=50, seconds=50),
         now - timedelta(days=1),
         now - timedelta(days=17),
         now - timedelta(days=47),
         now - timedelta(days=500),
         now - timedelta(days=365 * 2 + 35),
         now + timedelta(seconds=1),
         now + timedelta(seconds=30),
         now + timedelta(minutes=1, seconds=30),
         now + timedelta(minutes=2),
         now + timedelta(hours=1, minutes=30, seconds=30),
         now + timedelta(hours=23, minutes=50, seconds=50),
         now + timedelta(days=1),
         now + timedelta(days=500),
         now + timedelta(days=365 * 2 + 35),
         # regression tests for bugs in post-release humanize
         now + timedelta(days=10000),
         now - timedelta(days=365 + 35),
         30,
         now - timedelta(days=365 * 2 + 65),
         now - timedelta(days=365 + 4),
         "NaN",
     ]
     result_list = [
         'now',
         'a second ago',
         '30 seconds ago',
         'a minute ago',
         '2 minutes ago',
         'an hour ago',
         '23 hours ago',
         'a day ago',
         '17 days ago',
         '47 days ago',
         '1 year, 135 days ago',
         '2 years ago',
         'a second from now',
         '30 seconds from now',
         'a minute from now',
         '2 minutes from now',
         'an hour from now',
         '23 hours from now',
         'a day from now',
         '1 year, 135 days from now',
         '2 years from now',
         '27 years from now',
         '1 year, 35 days ago',
         '30 seconds ago',
         '2 years ago',
         '1 year, 4 days ago',
         "NaN",
     ]
     with patch('humanize.time._now') as mocked:
         mocked.return_value = now
         nt_nomonths = lambda d: time.naturaltime(d, months=False)
         self.assertManyResults(nt_nomonths, test_list, result_list)
Esempio n. 6
0
def naturaltime(*args, **kwargs):
    with django_language():
        return humanize_time.naturaltime(*args, **kwargs)
Esempio n. 7
0
 def nt_nomonths(d):
     return time.naturaltime(d, months=False)
Esempio n. 8
0
def naturaltime(*args, **kwargs):
    """ Wrap `humanize.naturaltime` into django_language(). """

    with django_language():
        return humanize_time.naturaltime(*args, **kwargs)