blob: ad11249e76e0e57869d4126d32f660e5d5074f7a [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Unittest for timestr module."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import calendar
12import datetime
13import time
14import unittest
15
16from framework import timestr
17
18
19class TimeStrTest(unittest.TestCase):
20 """Unit tests for timestr routines."""
21
22 def testFormatAbsoluteDate(self):
23 now = datetime.datetime(2008, 1, 1)
24
25 def GetDate(*args):
26 date = datetime.datetime(*args)
27 return timestr.FormatAbsoluteDate(
28 calendar.timegm(date.utctimetuple()), clock=lambda: now)
29
30 self.assertEqual(GetDate(2008, 1, 1), 'Today')
31 self.assertEqual(GetDate(2007, 12, 31), 'Yesterday')
32 self.assertEqual(GetDate(2007, 12, 30), 'Dec 30')
33 self.assertEqual(GetDate(2007, 1, 1), 'Jan 2007')
34 self.assertEqual(GetDate(2007, 1, 2), 'Jan 2007')
35 self.assertEqual(GetDate(2007, 12, 31), 'Yesterday')
36 self.assertEqual(GetDate(2006, 12, 31), 'Dec 2006')
37 self.assertEqual(GetDate(2007, 7, 1), 'Jul 1')
38 self.assertEqual(GetDate(2007, 6, 30), 'Jun 2007')
39 self.assertEqual(GetDate(2008, 1, 3), 'Jan 2008')
40
41 # Leap year fun
42 now = datetime.datetime(2008, 3, 1)
43 self.assertEqual(GetDate(2008, 2, 29), 'Yesterday')
44
45 # Clock skew
46 now = datetime.datetime(2008, 1, 1, 23, 59, 59)
47 self.assertEqual(GetDate(2008, 1, 2), 'Today')
48 now = datetime.datetime(2007, 12, 31, 23, 59, 59)
49 self.assertEqual(GetDate(2008, 1, 1), 'Today')
50 self.assertEqual(GetDate(2008, 1, 2), 'Jan 2008')
51
52 def testFormatRelativeDate(self):
53 now = time.mktime(datetime.datetime(2008, 1, 1).timetuple())
54
55 def TestSecsAgo(secs_ago, expected, expected_days_only):
56 test_time = now - secs_ago
57 actual = timestr.FormatRelativeDate(
58 test_time, clock=lambda: now)
59 self.assertEqual(actual, expected)
60 actual_days_only = timestr.FormatRelativeDate(
61 test_time, clock=lambda: now, days_only=True)
62 self.assertEqual(actual_days_only, expected_days_only)
63
64 TestSecsAgo(10 * 24 * 60 * 60, '', '10 days ago')
65 TestSecsAgo(5 * 24 * 60 * 60 - 1, '4 days ago', '4 days ago')
66 TestSecsAgo(5 * 60 * 60 - 1, '4 hours ago', '')
67 TestSecsAgo(5 * 60 - 1, '4 minutes ago', '')
68 TestSecsAgo(2 * 60 - 1, '1 minute ago', '')
69 TestSecsAgo(60 - 1, 'moments ago', '')
70 TestSecsAgo(0, 'moments ago', '')
71 TestSecsAgo(-10, 'moments ago', '')
72 TestSecsAgo(-100, '', '')
73
74 def testGetHumanScaleDate(self):
75 """Tests GetHumanScaleDate()."""
76 now = time.mktime(datetime.datetime(2008, 4, 10, 20, 50, 30).timetuple())
77
78 def GetDate(*args):
79 date = datetime.datetime(*args)
80 timestamp = time.mktime(date.timetuple())
81 return timestr.GetHumanScaleDate(timestamp, now=now)
82
83 self.assertEqual(GetDate(2008, 4, 10, 15), ('Today', '5 hours ago'))
84 self.assertEqual(GetDate(2008, 4, 10, 19, 55), ('Today', '55 min ago'))
85 self.assertEqual(GetDate(2008, 4, 10, 20, 48, 35), ('Today', '1 min ago'))
86 self.assertEqual(GetDate(2008, 4, 10, 20, 49, 35), ('Today', 'moments ago'))
87 self.assertEqual(GetDate(2008, 4, 10, 20, 50, 55), ('Today', 'moments ago'))
88 self.assertEqual(GetDate(2008, 4, 9, 15), ('Yesterday', '29 hours ago'))
89 self.assertEqual(GetDate(2008, 4, 5, 15), ('Last 7 days', 'Apr 05, 2008'))
90 self.assertEqual(GetDate(2008, 3, 22, 15), ('Last 30 days', 'Mar 22, 2008'))
91 self.assertEqual(
92 GetDate(2008, 1, 2, 15), ('Earlier this year', 'Jan 02, 2008'))
93 self.assertEqual(
94 GetDate(2007, 12, 31, 15), ('Before this year', 'Dec 31, 2007'))
95 self.assertEqual(GetDate(2008, 4, 11, 20, 49, 35), ('Future', 'Later'))