Optional warning about using "Week Year" based year instead of "Calendar Year" in date format strings

Issue #53 closed
Former user created an issue

I've now run into this issue couple of times in two different projects. It's not strictly a bug, but in majority of cases it is not what developer actually intented to do.

Instead of this:

    NSDateFormatter *df = [NSDateFormatter new];
    df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    df.dateFormat = @"yyyy-MM-dd'T'HHmmss";

Developer writes this:

    NSDateFormatter *df = [NSDateFormatter new];
    df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    df.dateFormat = @"YYYY-MM-dd'T'HHmmss";

...and it works as expected, most of the time. However, capital Y means week-year (spec). So as today, 31.12.2014, is first week of 2015, "YYYY" produces 2015 while "yyyy" produces 2014. This results in bugs which are time-sensitive and occur around New Year (and holidays...)

Of course there are some legitimate uses of week-year in date formatting, so I wouldn't necessarily enable this rule by default in the FauxPas.

Comments (7)

  1. Former user Account Deleted reporter

    One idea to reduce false positives:

    • If date format string contains specifiers for month (M/L) then week-year is probably not wanted.
    • If date format string contains specifiers for week of year (w) then week-year is probably wanted.
  2. Ali Rantakari repo owner

    Thank you for the suggestion — excellent idea! This currently occupies prime real estate in the todo list.

  3. Log in to comment