Search code examples
objective-cmacoscocoansdateformattermacos-high-sierra

NSDateFormatter doesRelativeDateFormatting returns unexpected relative day value


Using NSDateFormatter on macOS 10.13.3, I'm getting incorrect values when using doesRelativeDateFormatting set to YES. I've seen that there may be an issue with relative dates when using a custom format on the formatter but I am using standard dateStyle & timeStyle settings.

As an example, comparing the current date in New York City to the date in Sydney, Australia using appropriately configured date formatters without using doesRelativeDateFormatting, the string output from the date formatters correctly shows the day in Sydney being +1 from the day in NYC. When I enable doesRelativeDateFormatting on the same formatters, the relative date for Sydney returns incorrectly as same day ('Today'). Code that demonstrates these results:

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSDate *now = [NSDate date];
        NSDateFormatter *localDateFormatter = [NSDateFormatter new];
        localDateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"America/New_York"];
        localDateFormatter.dateStyle = NSDateFormatterFullStyle;
        localDateFormatter.timeStyle = NSDateFormatterFullStyle;

        NSDateFormatter *remoteDateFormatter = [NSDateFormatter new];
        remoteDateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"Australia/Sydney"];
        remoteDateFormatter.dateStyle = NSDateFormatterFullStyle;
        remoteDateFormatter.timeStyle = NSDateFormatterFullStyle;

        NSLog(@"            Now in NYC: %@", [localDateFormatter stringFromDate:now]);
        NSLog(@"         Now in Sydney: %@", [remoteDateFormatter stringFromDate:now]);

        localDateFormatter.doesRelativeDateFormatting = YES;
        remoteDateFormatter.doesRelativeDateFormatting = YES;

        NSLog(@"   Relative now in NYC: %@", [localDateFormatter stringFromDate:now]);
        NSLog(@"Relative now in Sydney: %@", [remoteDateFormatter stringFromDate:now]);
    }
}

Output:

2018-01-26 14:42:28.478 Untitled[40694:1821879]             Now in NYC: Friday, January 26, 2018 at 2:42:28 PM Eastern Standard Time
2018-01-26 14:42:28.479 Untitled[40694:1821879]          Now in Sydney: Saturday, January 27, 2018 at 6:42:28 AM Australian Eastern Daylight Time
2018-01-26 14:42:28.479 Untitled[40694:1821879]    Relative now in NYC: Today at 2:42:28 PM Eastern Standard Time
2018-01-26 14:42:28.479 Untitled[40694:1821879] Relative now in Sydney: Today at 6:42:28 AM Australian Eastern Daylight Time

Is this a bug in NSDateFormatter or am I doing something wrong in my configuration of the formatter? Thanks.


Solution

  • Your output is correct. Call anyone on the phone anywhere in the world and ask them what the date is and they will all say "today". Just because it's a different day of the week in two parts of the world doesn't mean it isn't "today" locally everywhere.

    You are letting yourself get confused by comparing the output of two different timezones that happen to be in two different days when the code is run.

    The idea of "relative" date formatting is that the output is a string relative to "now" in the given timezone. It's not relative to any other timezone. Whichever one is set on the date formatter.