1// RUN: %check_clang_tidy %s objc-nsdate-formatter %t 2@interface NSObject 3+ (instancetype)alloc; 4- (instancetype)init; 5@end 6 7@interface TestClass : NSObject 8+ (void)testMethod1; 9+ (void)testMethod2; 10+ (void)testMethod3; 11+ (void)testAnotherClass; 12@end 13 14@interface NSString : NSObject 15@end 16 17void NSLog(NSString *format, ...); 18 19@interface NSDate : NSObject 20@end 21 22@interface NSDateFormatter : NSObject 23@property(copy) NSString *dateFormat; 24- (NSString *)stringFromDate:(NSDate *)date; 25@end 26 27@interface AnotherClass : NSObject 28@property(copy) NSString *dateFormat; 29@end 30 31@interface NSDateComponents : NSObject 32@property long year; 33@property long month; 34@property long day; 35@end 36 37@interface NSCalendar : NSObject 38@property(class, readonly, copy) NSCalendar *currentCalendar; 39- (nullable NSDate *)dateFromComponents:(NSDateComponents *)Comps; 40@end 41 42@implementation TestClass 43+ (void)testMethod1 { 44 // Reproducing incorrect behavior from Radar 45 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 46 [formatter setDateFormat:@"YYYY_MM_dd_HH_mm_ss_SSS_ZZZ"]; 47 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 48 NSDateComponents *comps = [[NSDateComponents alloc] init]; 49 [comps setDay:29]; 50 [comps setMonth:12]; 51 [comps setYear:2014]; 52 NSDate *date_radar = [[NSCalendar currentCalendar] dateFromComponents:comps]; 53 NSLog(@"YYYY_MM_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]); 54 55 // Radar correct behavior 56 [formatter setDateFormat:@"yyyy_MM_dd_HH_mm_ss_SSS_ZZZ"]; 57 NSLog(@"yyyy_MM_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]); 58 59 // Radar correct behavior - week year 60 [formatter setDateFormat:@"YYYY_ww_dd_HH_mm_ss_SSS_ZZZ"]; 61 NSLog(@"YYYY_ww_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]); 62 63 // Radar incorrect behavior 64 [formatter setDateFormat:@"yyyy_ww_dd_HH_mm_ss_SSS_ZZZ"]; 65 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? [objc-nsdate-formatter] 66 NSLog(@"yyyy_ww_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]); 67 68 NSLog(@"=========================================="); 69 // Correct 70 [formatter setDateFormat:@"yyyy_MM"]; 71 NSLog(@"yyyy_MM %@", [formatter stringFromDate:date_radar]); 72 73 // Correct 74 [formatter setDateFormat:@"yyyy_dd"]; 75 NSLog(@"yyyy_dd %@", [formatter stringFromDate:date_radar]); 76 77 // Correct 78 [formatter setDateFormat:@"yyyy_DD"]; 79 NSLog(@"yyyy_DD %@", [formatter stringFromDate:date_radar]); 80 81 // Incorrect 82 [formatter setDateFormat:@"yyyy_ww"]; 83 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? [objc-nsdate-formatter] 84 NSLog(@"yyyy_ww %@", [formatter stringFromDate:date_radar]); 85 86 // Incorrect 87 [formatter setDateFormat:@"yyyy_WW"]; 88 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: Week of Month (W) used without the month (M); did you forget M in the format string? [objc-nsdate-formatter] 89 NSLog(@"yyyy_WW %@", [formatter stringFromDate:date_radar]); 90 91 NSLog(@"=========================================="); 92 // Correct 93 [formatter setDateFormat:@"yyyy_MM_dd"]; 94 NSLog(@"yyyy_MM_dd %@", [formatter stringFromDate:date_radar]); 95 96 // Potentially Incorrect 97 [formatter setDateFormat:@"yyyy_MM_DD"]; 98 NSLog(@"yyyy_MM_DD %@", [formatter stringFromDate:date_radar]); 99 100 // Incorrect 101 [formatter setDateFormat:@"yyyy_MM_ww"]; 102 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? [objc-nsdate-formatter] 103 NSLog(@"yyyy_MM_ww %@", [formatter stringFromDate:date_radar]); 104 105 NSLog(@"=======WEEK YEAR=========="); 106 // Incorrect 107 [formatter setDateFormat:@"YYYY_MM"]; 108 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 109 NSLog(@"YYYY_MM %@", [formatter stringFromDate:date_radar]); 110 111 // Correct 112 [formatter setDateFormat:@"YYYY_ww"]; 113 NSLog(@"YYYY_ww %@", [formatter stringFromDate:date_radar]); 114 115 // Incorrect 116 [formatter setDateFormat:@"YYYY_WW"]; 117 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: Week of Month (W) used without the month (M); did you forget M in the format string? [objc-nsdate-formatter] 118 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with week of the month (W); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 119 NSLog(@"YYYY_WW %@", [formatter stringFromDate:date_radar]); 120 121 // Correct 122 [formatter setDateFormat:@"YYYY_dd"]; 123 NSLog(@"YYYY_dd %@", [formatter stringFromDate:date_radar]); 124 125 // Incorrect 126 [formatter setDateFormat:@"YYYY_DD"]; 127 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with day of the year (D); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 128 NSLog(@"YYYY_DD %@", [formatter stringFromDate:date_radar]); 129 130 NSLog(@"=========================================="); 131 // Potentially Incorrect 132 [formatter setDateFormat:@"YYYY_ww_dd"]; 133 NSLog(@"YYYY ww dd %@", [formatter stringFromDate:date_radar]); 134 135 // Incorrect 136 [formatter setDateFormat:@"YYYY_ww_DD"]; 137 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with day of the year (D); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 138 NSLog(@"YYYY_ww_DD %@", [formatter stringFromDate:date_radar]); 139} 140 141+ (void)testMethod2 { 142 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 143 NSDateComponents *comps = [[NSDateComponents alloc] init]; 144 [comps setDay:29]; 145 [comps setMonth:12]; 146 [comps setYear:2014]; 147 NSDate *date_radar = [[NSCalendar currentCalendar] dateFromComponents:comps]; 148 149 // Test 1 : incorrect 150 [formatter setDateFormat:@"yyyy_QQ_MM_ww_dd_EE"]; 151 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? [objc-nsdate-formatter] 152 NSLog(@"yyyy_QQ_MM_ww_dd_EE %@", [formatter stringFromDate:date_radar]); 153 154 // Test 2 : incorrect 155 [formatter setDateFormat:@"yyyy_QQ_MM_ww_dd_ee"]; 156 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? [objc-nsdate-formatter] 157 NSLog(@"yyyy_QQ_MM_ww_dd_ee %@", [formatter stringFromDate:date_radar]); 158 159 // Test 3 : incorrect 160 [formatter setDateFormat:@"yyyy_QQ_MM_ww_DD_EE"]; 161 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? [objc-nsdate-formatter] 162 NSLog(@"yyyy_QQ_MM_ww_DD_EE %@", [formatter stringFromDate:date_radar]); 163 164 // Test 4 : incorrect 165 [formatter setDateFormat:@"yyyy_QQ_MM_ww_DD_ee"]; 166 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? [objc-nsdate-formatter] 167 NSLog(@"yyyy_QQ_MM_ww_DD_ee %@", [formatter stringFromDate:date_radar]); 168 169 // Test 5 : incorrect 170 [formatter setDateFormat:@"yyyy_QQ_MM_ww_F_EE"]; 171 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? [objc-nsdate-formatter] 172 NSLog(@"yyyy_QQ_MM_ww_F_EE %@", [formatter stringFromDate:date_radar]); 173 174 // Test 6 : incorrect 175 [formatter setDateFormat:@"yyyy_QQ_MM_ww_F_ee"]; 176 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? [objc-nsdate-formatter] 177 NSLog(@"yyyy_QQ_MM_ww_F_ee %@", [formatter stringFromDate:date_radar]); 178 179 // Test 7 : correct 180 [formatter setDateFormat:@"yyyy_QQ_MM_WW_dd_EE"]; 181 NSLog(@"yyyy_QQ_MM_WW_dd_EE %@", [formatter stringFromDate:date_radar]); 182 183 // Test 8 : correct 184 [formatter setDateFormat:@"yyyy_QQ_MM_WW_dd_ee"]; 185 NSLog(@"yyyy_QQ_MM_WW_dd_ee %@", [formatter stringFromDate:date_radar]); 186 187 // Test 9 : correct 188 [formatter setDateFormat:@"yyyy_QQ_MM_WW_DD_EE"]; 189 NSLog(@"yyyy_QQ_MM_WW_DD_EE %@", [formatter stringFromDate:date_radar]); 190 191 // Test 10 : correct 192 [formatter setDateFormat:@"yyyy_QQ_MM_WW_DD_ee"]; 193 NSLog(@"yyyy_QQ_MM_WW_DD_ee %@", [formatter stringFromDate:date_radar]); 194 195 // Test 11 : correct 196 [formatter setDateFormat:@"yyyy_QQ_MM_WW_F_EE"]; 197 NSLog(@"yyyy_QQ_MM_WW_F_EE %@", [formatter stringFromDate:date_radar]); 198 199 // Test 12 : correct 200 [formatter setDateFormat:@"yyyy_QQ_MM_WW_F_ee"]; 201 NSLog(@"yyyy_QQ_MM_WW_F_ee %@", [formatter stringFromDate:date_radar]); 202 203 // Test 13 : incorrect 204 [formatter setDateFormat:@"YYYY_QQ_MM_ww_dd_EE"]; 205 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 206 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 207 NSLog(@"YYYY_QQ_MM_ww_dd_EE %@", [formatter stringFromDate:date_radar]); 208 209 // Test 14 : incorrect 210 [formatter setDateFormat:@"YYYY_QQ_MM_ww_dd_ee"]; 211 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 212 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 213 NSLog(@"YYYY_QQ_MM_ww_dd_ee %@", [formatter stringFromDate:date_radar]); 214 215 // Test 15 : incorrect 216 [formatter setDateFormat:@"YYYY_QQ_MM_ww_DD_EE"]; 217 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with day of the year (D); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 218 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 219 // CHECK-MESSAGES: :[[@LINE-3]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 220 NSLog(@"YYYY_QQ_MM_ww_DD_EE %@", [formatter stringFromDate:date_radar]); 221 222 // Test 16 : incorrect 223 [formatter setDateFormat:@"YYYY_QQ_MM_ww_DD_ee"]; 224 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with day of the year (D); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 225 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 226 // CHECK-MESSAGES: :[[@LINE-3]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 227 NSLog(@"YYYY_QQ_MM_ww_DD_ee %@", [formatter stringFromDate:date_radar]); 228 229 // Test 17 : incorrect 230 [formatter setDateFormat:@"YYYY_QQ_MM_ww_F_EE"]; 231 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with day of the week in month (F); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 232 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 233 // CHECK-MESSAGES: :[[@LINE-3]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 234 NSLog(@"YYYY_QQ_MM_ww_F_EE %@", [formatter stringFromDate:date_radar]); 235 236 // Test 18 : incorrect 237 [formatter setDateFormat:@"YYYY_QQ_MM_ww_F_ee"]; 238 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with day of the week in month (F); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 239 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 240 // CHECK-MESSAGES: :[[@LINE-3]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 241 NSLog(@"YYYY_QQ_MM_ww_F_ee %@", [formatter stringFromDate:date_radar]); 242 243 // Test 19 : incorrect 244 [formatter setDateFormat:@"YYYY_QQ_MM_WW_dd_EE"]; 245 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 246 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 247 // CHECK-MESSAGES: :[[@LINE-3]]:28: warning: use of week year (Y) with week of the month (W); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 248 NSLog(@"YYYY_QQ_MM_WW_dd_EE %@", [formatter stringFromDate:date_radar]); 249 250 // Test 20 : incorrect 251 [formatter setDateFormat:@"YYYY_QQ_MM_WW_dd_ee"]; 252 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 253 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 254 // CHECK-MESSAGES: :[[@LINE-3]]:28: warning: use of week year (Y) with week of the month (W); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 255 NSLog(@"YYYY_QQ_MM_WW_dd_ee %@", [formatter stringFromDate:date_radar]); 256 257 // Test 21 : incorrect 258 [formatter setDateFormat:@"YYYY_QQ_MM_WW_DD_EE"]; 259 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with day of the year (D); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 260 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 261 // CHECK-MESSAGES: :[[@LINE-3]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 262 // CHECK-MESSAGES: :[[@LINE-4]]:28: warning: use of week year (Y) with week of the month (W); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 263 NSLog(@"YYYY_QQ_MM_WW_DD_EE %@", [formatter stringFromDate:date_radar]); 264 265 // Test 22 : incorrect 266 [formatter setDateFormat:@"YYYY_QQ_MM_WW_DD_ee"]; 267 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with day of the year (D); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 268 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 269 // CHECK-MESSAGES: :[[@LINE-3]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 270 // CHECK-MESSAGES: :[[@LINE-4]]:28: warning: use of week year (Y) with week of the month (W); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 271 NSLog(@"YYYY_QQ_MM_WW_DD_ee %@", [formatter stringFromDate:date_radar]); 272 273 // Test 23 : incorrect 274 [formatter setDateFormat:@"YYYY_QQ_MM_WW_F_EE"]; 275 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with day of the week in month (F); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 276 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 277 // CHECK-MESSAGES: :[[@LINE-3]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 278 // CHECK-MESSAGES: :[[@LINE-4]]:28: warning: use of week year (Y) with week of the month (W); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 279 NSLog(@"YYYY_QQ_MM_WW_F_EE %@", [formatter stringFromDate:date_radar]); 280 281 // Test 24 : incorrect 282 [formatter setDateFormat:@"YYYY_QQ_MM_WW_F_ee"]; 283 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with day of the week in month (F); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 284 // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 285 // CHECK-MESSAGES: :[[@LINE-3]]:28: warning: use of week year (Y) with quarter number (Q); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 286 // CHECK-MESSAGES: :[[@LINE-4]]:28: warning: use of week year (Y) with week of the month (W); did you mean to use calendar year (y) instead? [objc-nsdate-formatter] 287 NSLog(@"YYYY_QQ_MM_WW_F_ee %@", [formatter stringFromDate:date_radar]); 288} 289 290+ (void)testMethod3 { 291 NSDateFormatter *Formatter = [[NSDateFormatter alloc] init]; 292 NSDateComponents *Comps = [[NSDateComponents alloc] init]; 293 [Comps setDay:29]; 294 [Comps setMonth:12]; 295 [Comps setYear:2014]; 296 NSDate *DateRadar = [[NSCalendar currentCalendar] dateFromComponents:Comps]; 297 298 // Incorrect : has reserved and invalid chars 299 [Formatter setDateFormat:@"Rashmi"]; 300 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: invalid date format specifier [objc-nsdate-formatter] 301 NSLog(@"Rashmi %@", [Formatter stringFromDate:DateRadar]); 302 303 // Correct 304 [Formatter setDateFormat:@"AMy"]; 305 NSLog(@"AMy %@", [Formatter stringFromDate:DateRadar]); 306} 307 308+ (void)testAnotherClass { 309 AnotherClass *Formatter = [[AnotherClass alloc] init]; 310 [Formatter setDateFormat:@"RandomString"]; 311 [Formatter setDateFormat:@"YYYY_QQ_MM_WW_dd_EE"]; 312} 313@end 314