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