2021-03-25 19:43:16 -07:00
|
|
|
import unittest
|
|
|
|
import jc.utils
|
|
|
|
|
|
|
|
|
|
|
|
class MyTests(unittest.TestCase):
|
2021-04-01 11:07:37 -07:00
|
|
|
def test_utils_timestamp(self):
|
2021-03-25 19:43:16 -07:00
|
|
|
|
|
|
|
# naive timestamps created in PDT
|
|
|
|
datetime_map = {
|
|
|
|
# C locale format conversion, or date cli command in C locale with non-UTC tz
|
2021-04-01 11:07:37 -07:00
|
|
|
'Tue Mar 23 16:12:11 2021': {'string': 'Tue Mar 23 16:12:11 2021', 'format': 1000, 'naive': 1616541131, 'utc': None},
|
|
|
|
'Tue Mar 23 16:12:11 IST 2021': {'string': 'Tue Mar 23 16:12:11 IST 2021', 'format': 1000, 'naive': 1616541131, 'utc': None},
|
2021-03-30 11:59:44 -07:00
|
|
|
# en_US.UTF-8 local format (found in who cli output)
|
2021-04-01 11:07:37 -07:00
|
|
|
'2021-03-23 00:14': {'string': '2021-03-23 00:14', 'format': 1500, 'naive': 1616483640, 'utc': None},
|
2021-04-02 12:01:05 -07:00
|
|
|
# Windows english format (found in dir cli output)
|
|
|
|
'12/07/2019 02:09 AM': {'string': '12/07/2019 02:09 AM', 'format': 1600, 'naive': 1575713340, 'utc': None},
|
2021-03-25 19:43:16 -07:00
|
|
|
# en_US.UTF-8 local format (found in upower cli output)
|
2021-04-01 11:07:37 -07:00
|
|
|
'Tue 23 Mar 2021 04:12:11 PM UTC': {'string': 'Tue 23 Mar 2021 04:12:11 PM UTC', 'format': 2000, 'naive': 1616541131, 'utc': 1616515931},
|
2021-03-25 19:43:16 -07:00
|
|
|
# en_US.UTF-8 local format with non-UTC tz (found in upower cli output)
|
2021-04-01 11:07:37 -07:00
|
|
|
'Tue 23 Mar 2021 04:12:11 PM IST': {'string': 'Tue 23 Mar 2021 04:12:11 PM IST', 'format': 3000, 'naive': 1616541131, 'utc': None},
|
2021-03-25 19:43:16 -07:00
|
|
|
# European local format (found in upower cli output)
|
2021-04-01 11:07:37 -07:00
|
|
|
'Tuesday 01 October 2019 12:50:41 PM UTC': {'string': 'Tuesday 01 October 2019 12:50:41 PM UTC', 'format': 4000, 'naive': 1569959441, 'utc': 1569934241},
|
2021-03-25 19:43:16 -07:00
|
|
|
# European local format with non-UTC tz (found in upower cli output)
|
2021-04-01 11:07:37 -07:00
|
|
|
'Tuesday 01 October 2019 12:50:41 PM IST': {'string': 'Tuesday 01 October 2019 12:50:41 PM IST', 'format': 5000, 'naive': 1569959441, 'utc': None},
|
2021-03-25 19:43:16 -07:00
|
|
|
# date cli command in en_US.UTF-8 format
|
2021-04-01 11:07:37 -07:00
|
|
|
'Wed Mar 24 06:16:19 PM UTC 2021': {'string': 'Wed Mar 24 06:16:19 PM UTC 2021', 'format': 6000, 'naive': 1616634979, 'utc': 1616609779},
|
2021-03-25 19:43:16 -07:00
|
|
|
# date cli command in C locale format
|
2021-04-01 11:07:37 -07:00
|
|
|
'Wed Mar 24 11:11:30 UTC 2021': {'string': 'Wed Mar 24 11:11:30 UTC 2021', 'format': 7000, 'naive': 1616609490, 'utc': 1616584290},
|
2021-03-29 14:45:13 -07:00
|
|
|
# C locale format (found in stat cli output - OSX)
|
2021-04-01 11:07:37 -07:00
|
|
|
'Mar 29 11:49:05 2021': {'string': 'Mar 29 11:49:05 2021', 'format': 7100, 'naive': 1617043745, 'utc': None},
|
2021-03-29 14:45:13 -07:00
|
|
|
# C local format (found in stat cli output - linux) non-UTC tz
|
2021-04-01 11:07:37 -07:00
|
|
|
'2019-08-13 18:13:43.555604315 -0400': {'string': '2019-08-13 18:13:43.555604315 -0400', 'format': 7200, 'naive': 1565745223, 'utc': None},
|
2021-03-29 14:45:13 -07:00
|
|
|
# C local format (found in stat cli output - linux) UTC
|
2021-04-01 11:07:37 -07:00
|
|
|
'2019-08-13 18:13:43.555604315 -0000': {'string': '2019-08-13 18:13:43.555604315 -0000', 'format': 7200, 'naive': 1565745223, 'utc': 1565720023},
|
2021-03-29 21:25:19 -07:00
|
|
|
# C locale format (found in timedatectl cli output)
|
2021-04-01 11:07:37 -07:00
|
|
|
'Wed 2020-03-11 00:53:21 UTC': {'string': 'Wed 2020-03-11 00:53:21 UTC', 'format': 7300, 'naive': 1583913201, 'utc': 1583888001},
|
|
|
|
# test with None input
|
|
|
|
None: {'string': None, 'format': None, 'naive': None, 'utc': None}
|
2021-03-25 19:43:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for input_string, expected_output in datetime_map.items():
|
2021-04-01 11:07:37 -07:00
|
|
|
self.assertEqual(jc.utils.timestamp(input_string).__dict__, expected_output)
|