[lldb] Always round down in NSDate's formatter to match NSDate's builtin format

Summary:

When printing an NSDate (for example with `NSLog` or `po`) the seconds value is
always rounded down. LLDB's own formatter however isn't following that behaviour
which leads to situations where the formatted result is sometimes one second
off. For example:

```
(lldb) p [NSDate dateWithTimeIntervalSince1970:0.1]
(__NSTaggedDate *) $1 = [...] 1970-01-01 00:00:01 UTC
(lldb) po [NSDate dateWithTimeIntervalSince1970:0.1]
1970-01-01 00:00:00 +0000

(lldb) p [NSDate dateWithTimeIntervalSince1970:0.6]
(__NSTaggedDate *) $4 =[...] 1970-01-01 00:00:01 UTC
(lldb) po [NSDate dateWithTimeIntervalSince1970:0.6]
1970-01-01 00:00:00 +0000
```

This patch just always rounds down the seconds value we get from the NSDate
object.

Fixes rdar://65084800

Reviewers: mib, davide

Reviewed By: mib

Subscribers: JDevlieghere

Differential Revision: https://reviews.llvm.org/D83221
This commit is contained in:
Raphael Isemann 2020-07-06 16:31:56 +02:00
parent 146dad0077
commit 5814255e1a
3 changed files with 18 additions and 1 deletions

View File

@ -867,7 +867,7 @@ bool lldb_private::formatters::NSDateSummaryProvider(
// is generally true and POSIXly happy, but might break if a library vendor
// decides to get creative
time_t epoch = GetOSXEpoch();
epoch = epoch + (time_t)date_value;
epoch = epoch + static_cast<time_t>(std::floor(date_value));
tm *tm_date = gmtime(&epoch);
if (!tm_date)
return false;

View File

@ -43,6 +43,15 @@ class ObjCDataFormatterNSDate(ObjCDataFormatterTestCase):
self.expect('frame variable date4_abs', substrs=['1970'])
self.expect('frame variable date5_abs', substrs=[now_year])
# Check that LLDB always follow's NSDate's rounding behavior (which
# is always rounding down).
self.expect_expr("date_1970_minus_06", result_summary="1969-12-31 23:59:59 UTC")
self.expect_expr("date_1970_minus_05", result_summary="1969-12-31 23:59:59 UTC")
self.expect_expr("date_1970_minus_04", result_summary="1969-12-31 23:59:59 UTC")
self.expect_expr("date_1970_plus_06", result_summary="1970-01-01 00:00:00 UTC")
self.expect_expr("date_1970_plus_05", result_summary="1970-01-01 00:00:00 UTC")
self.expect_expr("date_1970_plus_04", result_summary="1970-01-01 00:00:00 UTC")
self.expect('frame variable cupertino home europe',
substrs=['@"America/Los_Angeles"',
'@"Europe/Rome"',

View File

@ -655,6 +655,14 @@ int main(int argc, const char *argv[]) {
[NSDate dateWithTimeIntervalSinceReferenceDate:
floor([[NSDate date] timeIntervalSinceReferenceDate])];
NSDate *date_1970_minus_06 = [NSDate dateWithTimeIntervalSince1970:-0.6];
NSDate *date_1970_minus_05 = [NSDate dateWithTimeIntervalSince1970:-0.5];
NSDate *date_1970_minus_04 = [NSDate dateWithTimeIntervalSince1970:-0.4];
NSDate *date_1970_plus_06 = [NSDate dateWithTimeIntervalSince1970:0.6];
NSDate *date_1970_plus_05 = [NSDate dateWithTimeIntervalSince1970:0.5];
NSDate *date_1970_plus_04 = [NSDate dateWithTimeIntervalSince1970:0.4];
CFAbsoluteTime date1_abs = CFDateGetAbsoluteTime(date1);
CFAbsoluteTime date2_abs = CFDateGetAbsoluteTime(date2);
CFAbsoluteTime date3_abs = CFDateGetAbsoluteTime(date3);