1# encoding: utf-8
2"""
3Test lldb date formatter subsystem.
4"""
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12from ObjCDataFormatterTestCase import ObjCDataFormatterTestCase
13
14import datetime
15
16
17class ObjCDataFormatterNSDate(ObjCDataFormatterTestCase):
18    def test_nsdate_with_run_command(self):
19        """Test formatters for  NSDate."""
20        self.appkit_tester_impl(self.nsdate_data_formatter_commands, False)
21
22    @skipIf(macos_version=[">=", "14.0"])
23    def test_timezone_with_run_command(self):
24        """Test formatters for NSTimeZone and CFTimeZone."""
25        self.appkit_tester_impl(self.timezone_data_formatter_commands, False)
26
27    def nsdate_data_formatter_commands(self):
28        self.expect(
29            "frame variable date1 date2",
30            patterns=["(1985-04-10|1985-04-11)", "(2011-01-01|2010-12-31)"],
31        )
32
33        # this test might fail if we hit the breakpoint late on December 31st of some given year
34        # and midnight comes between hitting the breakpoint and running this line of code
35        # hopefully the output will be revealing enough in that case :-)
36        now_year = "%s-" % str(datetime.datetime.now().year)
37
38        self.expect("frame variable date3", substrs=[now_year])
39        self.expect("frame variable date4", substrs=["1970"])
40        self.expect("frame variable date5", substrs=[now_year])
41
42        self.expect(
43            "frame variable date1_abs date2_abs", substrs=["1985-04", "2011-01"]
44        )
45
46        self.expect("frame variable date3_abs", substrs=[now_year])
47        self.expect("frame variable date4_abs", substrs=["1970"])
48        self.expect("frame variable date5_abs", substrs=[now_year])
49
50        # Check that LLDB always follow's NSDate's rounding behavior (which
51        # is always rounding down).
52        self.expect_expr("date_1970_minus_06", result_summary="1969-12-31 23:59:59 UTC")
53        self.expect_expr("date_1970_minus_05", result_summary="1969-12-31 23:59:59 UTC")
54        self.expect_expr("date_1970_minus_04", result_summary="1969-12-31 23:59:59 UTC")
55        self.expect_expr("date_1970_plus_06", result_summary="1970-01-01 00:00:00 UTC")
56        self.expect_expr("date_1970_plus_05", result_summary="1970-01-01 00:00:00 UTC")
57        self.expect_expr("date_1970_plus_04", result_summary="1970-01-01 00:00:00 UTC")
58
59        self.expect(
60            "frame variable mut_bv",
61            substrs=[
62                "(CFMutableBitVectorRef) mut_bv = ",
63                "1110 0110 1011 0000 1101 1010 1000 1111 0011 0101 1101 0001 00",
64            ],
65        )
66
67        self.expect_expr("distant_past", result_summary="0001-01-01 00:00:00 UTC")
68        self.expect_expr("distant_future", result_summary="4001-01-01 00:00:00 UTC")
69
70    def timezone_data_formatter_commands(self):
71        self.expect(
72            "frame variable cupertino home europe",
73            substrs=['"America/Los_Angeles"', '"Europe/Rome"', '"Europe/Paris"'],
74        )
75
76        self.expect(
77            "frame variable cupertino_ns home_ns europe_ns",
78            substrs=['"America/Los_Angeles"', '"Europe/Rome"', '"Europe/Paris"'],
79        )
80