Week-year YYYY used in an NSDateFormatter calendar format
ID |
objectivec.correctness.nsdate_formatter |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Api Misuse |
Language |
Objective-C |
Description
Reports a date format string, set with setDateFormat: or assigned to the dateFormat property, that uses the week-year pattern YYYY without a week number. A format that pairs YYYY with a week field is a genuine week-based date and is not reported.
Rationale
In Unicode date patterns YYYY is the ISO week-numbering year, not the calendar year: it belongs to the week the date falls in, so the last days of December can carry the following year and the first days of January the previous one. The two agree for about 360 days a year, so the mistake passes every test written outside the turn of the year and then misdates receipts, log lines and file names for a few days each December.
The following code illustrates the pattern detected by this rule:
@implementation ReceiptPrinter
- (NSString *)printedDate:(NSDate *)date {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// FLAGGED: Week-year YYYY used in an NSDateFormatter calendar format
[formatter setDateFormat:@"YYYY-MM-dd"];
return [formatter stringFromDate:date];
}
@end