| 61 | | @implementation NSDate(WIDateLocalization) |
|---|
| 62 | | |
|---|
| 63 | | - (NSString *)commonDateStringWithSeconds:(BOOL)seconds { |
|---|
| 64 | | return [self commonDateStringWithSeconds:seconds relative:YES capitalized:YES]; |
|---|
| 65 | | } |
|---|
| 66 | | |
|---|
| 67 | | |
|---|
| 68 | | |
|---|
| 69 | | - (NSString *)commonDateStringWithSeconds:(BOOL)seconds relative:(BOOL)relative { |
|---|
| 70 | | return [self commonDateStringWithSeconds:seconds relative:relative capitalized:YES]; |
|---|
| 71 | | } |
|---|
| 72 | | |
|---|
| 73 | | |
|---|
| 74 | | |
|---|
| 75 | | - (NSString *)commonDateStringWithSeconds:(BOOL)seconds relative:(BOOL)relative capitalized:(BOOL)capitalized { |
|---|
| 76 | | NSCalendarDate *calendarDate, *calendarDateToday; |
|---|
| 77 | | NSUserDefaults *defaults; |
|---|
| 78 | | NSMutableString *time; |
|---|
| 79 | | NSString *string, *date, *format; |
|---|
| 80 | | NSRange range; |
|---|
| 81 | | BOOL today = NO, yesterday = NO; |
|---|
| 82 | | |
|---|
| 83 | | defaults = [NSUserDefaults standardUserDefaults]; |
|---|
| 84 | | |
|---|
| 85 | | if(relative) { |
|---|
| 86 | | calendarDate = [self dateWithCalendarFormat:NULL timeZone:NULL]; |
|---|
| 87 | | calendarDateToday = [NSCalendarDate calendarDate]; |
|---|
| 88 | | |
|---|
| 89 | | if([calendarDate dayOfCommonEra] == [calendarDateToday dayOfCommonEra]) |
|---|
| 90 | | today = YES; |
|---|
| 91 | | else if([calendarDate dayOfCommonEra] == [calendarDateToday dayOfCommonEra] - 1) |
|---|
| 92 | | yesterday = YES; |
|---|
| 93 | | } |
|---|
| 94 | | |
|---|
| 95 | | date = [defaults objectForKey:NSDateFormatString]; |
|---|
| 96 | | |
|---|
| 97 | | if(today || yesterday) { |
|---|
| 98 | | if(today) |
|---|
| 99 | | date = [[defaults objectForKey:NSThisDayDesignations] objectAtIndex:0]; |
|---|
| 100 | | else if(yesterday) |
|---|
| 101 | | date = [[defaults objectForKey:NSPriorDayDesignations] objectAtIndex:0]; |
|---|
| 102 | | |
|---|
| 103 | | if(capitalized) |
|---|
| 104 | | date = [date capitalizedString]; |
|---|
| 105 | | } |
|---|
| 106 | | |
|---|
| 107 | | time = [[defaults objectForKey:NSTimeFormatString] mutableCopy]; |
|---|
| 108 | | |
|---|
| 109 | | // --- fix a bug where the date formats would have 12-hour clocks |
|---|
| 110 | | range = [time rangeOfString:@"%H"]; |
|---|
| 111 | | |
|---|
| 112 | | if(range.location != NSNotFound) { |
|---|
| 113 | | // --- time has 24-hour clock, adjust our format accordingly |
|---|
| 114 | | [time replaceOccurrencesOfString:@"%I" |
|---|
| 115 | | withString:@"%H" |
|---|
| 116 | | options:0 |
|---|
| 117 | | range:NSMakeRange(0, [time length])]; |
|---|
| 118 | | } |
|---|
| 119 | | |
|---|
| 120 | | // --- fix a bug where the date formats would have 24-hour clocks |
|---|
| 121 | | range = [time rangeOfString:@"%I"]; |
|---|
| 122 | | |
|---|
| 123 | | if(range.location != NSNotFound) { |
|---|
| 124 | | // --- time has 12-hour clock, adjust our format accordingly |
|---|
| 125 | | [time replaceOccurrencesOfString:@"%H" |
|---|
| 126 | | withString:@"%I" |
|---|
| 127 | | options:0 |
|---|
| 128 | | range:NSMakeRange(0, [time length])]; |
|---|
| 129 | | } |
|---|
| 130 | | |
|---|
| 131 | | // --- kill seconds |
|---|
| 132 | | if(!seconds) { |
|---|
| 133 | | range = [time rangeOfString:@":%S"]; |
|---|
| 134 | | |
|---|
| 135 | | if(range.location != NSNotFound) |
|---|
| 136 | | [time deleteCharactersInRange:range]; |
|---|
| 137 | | |
|---|
| 138 | | range = [time rangeOfString:@".%S"]; |
|---|
| 139 | | |
|---|
| 140 | | if(range.location != NSNotFound) |
|---|
| 141 | | [time deleteCharactersInRange:range]; |
|---|
| 142 | | } |
|---|
| 143 | | |
|---|
| 144 | | // --- get final format |
|---|
| 145 | | format = [NSSWF:@"%@, %@", date, time]; |
|---|
| 146 | | string = [self descriptionWithCalendarFormat:format |
|---|
| 147 | | timeZone:NULL |
|---|
| 148 | | locale:[defaults dictionaryRepresentation]]; |
|---|
| 149 | | |
|---|
| 150 | | [time release]; |
|---|
| 151 | | |
|---|
| 152 | | return string; |
|---|
| 153 | | } |
|---|
| 154 | | |
|---|
| 155 | | |
|---|
| 156 | | |
|---|
| 157 | | - (NSString *)fullDateStringWithSeconds:(BOOL)seconds { |
|---|
| 158 | | NSUserDefaults *defaults; |
|---|
| 159 | | NSMutableString *time; |
|---|
| 160 | | NSString *string, *format, *date; |
|---|
| 161 | | NSRange range; |
|---|
| 162 | | |
|---|
| 163 | | defaults = [NSUserDefaults standardUserDefaults]; |
|---|
| 164 | | date = [defaults objectForKey:NSShortDateFormatString]; |
|---|
| 165 | | time = [[defaults objectForKey:NSTimeFormatString] mutableCopy]; |
|---|
| 166 | | |
|---|
| 167 | | // --- fix a bug where the date formats would have 12-hour clocks |
|---|
| 168 | | range = [time rangeOfString:@"%H"]; |
|---|
| 169 | | |
|---|
| 170 | | if(range.location != NSNotFound) { |
|---|
| 171 | | // --- time has 24-hour clock, adjust our format accordingly |
|---|
| 172 | | [time replaceOccurrencesOfString:@"%I" |
|---|
| 173 | | withString:@"%H" |
|---|
| 174 | | options:0 |
|---|
| 175 | | range:NSMakeRange(0, [time length])]; |
|---|
| 176 | | } |
|---|
| 177 | | |
|---|
| 178 | | // --- fix a bug where the date formats would have 24-hour clocks |
|---|
| 179 | | range = [time rangeOfString:@"%I"]; |
|---|
| 180 | | |
|---|
| 181 | | if(range.location != NSNotFound) { |
|---|
| 182 | | // --- time has 12-hour clock, adjust our format accordingly |
|---|
| 183 | | [time replaceOccurrencesOfString:@"%H" |
|---|
| 184 | | withString:@"%I" |
|---|
| 185 | | options:0 |
|---|
| 186 | | range:NSMakeRange(0, [time length])]; |
|---|
| 187 | | } |
|---|
| 188 | | |
|---|
| 189 | | // --- kill seconds |
|---|
| 190 | | if(!seconds) { |
|---|
| 191 | | range = [time rangeOfString:@":%S"]; |
|---|
| 192 | | |
|---|
| 193 | | if(range.location != NSNotFound) |
|---|
| 194 | | [time deleteCharactersInRange:range]; |
|---|
| 195 | | |
|---|
| 196 | | range = [time rangeOfString:@".%S"]; |
|---|
| 197 | | |
|---|
| 198 | | if(range.location != NSNotFound) |
|---|
| 199 | | [time deleteCharactersInRange:range]; |
|---|
| 200 | | } |
|---|
| 201 | | |
|---|
| 202 | | format = [NSSWF:@"%@, %@", date, time]; |
|---|
| 203 | | string = [self descriptionWithCalendarFormat:format |
|---|
| 204 | | timeZone:NULL |
|---|
| 205 | | locale:[defaults dictionaryRepresentation]]; |
|---|
| 206 | | |
|---|
| 207 | | // --- fix bug where extraneous space characters would be left at the end |
|---|
| 208 | | while([string hasSuffix:@" "]) |
|---|
| 209 | | string = [string substringToIndex:[string length] - 1]; |
|---|
| 210 | | |
|---|
| 211 | | [time release]; |
|---|
| 212 | | |
|---|
| 213 | | return string; |
|---|
| 214 | | } |
|---|
| 215 | | |
|---|
| 216 | | |
|---|
| 217 | | |
|---|
| 218 | | - (NSString *)timeStringWithSeconds:(BOOL)seconds { |
|---|
| 219 | | NSUserDefaults *defaults; |
|---|
| 220 | | NSMutableString *time; |
|---|
| 221 | | NSString *string; |
|---|
| 222 | | NSRange range; |
|---|
| 223 | | |
|---|
| 224 | | defaults = [NSUserDefaults standardUserDefaults]; |
|---|
| 225 | | time = [[defaults objectForKey:NSTimeFormatString] mutableCopy]; |
|---|
| 226 | | |
|---|
| 227 | | // --- fix a bug where the date formats would have 12-hour clocks |
|---|
| 228 | | range = [time rangeOfString:@"%H"]; |
|---|
| 229 | | |
|---|
| 230 | | if(range.location != NSNotFound) { |
|---|
| 231 | | // --- time has 24-hour clock, adjust our format accordingly |
|---|
| 232 | | [time replaceOccurrencesOfString:@"%I" |
|---|
| 233 | | withString:@"%H" |
|---|
| 234 | | options:0 |
|---|
| 235 | | range:NSMakeRange(0, [time length])]; |
|---|
| 236 | | } |
|---|
| 237 | | |
|---|
| 238 | | // --- fix a bug where the date formats would have 24-hour clocks |
|---|
| 239 | | range = [time rangeOfString:@"%I"]; |
|---|
| 240 | | |
|---|
| 241 | | if(range.location != NSNotFound) { |
|---|
| 242 | | // --- time has 12-hour clock, adjust our format accordingly |
|---|
| 243 | | [time replaceOccurrencesOfString:@"%H" |
|---|
| 244 | | withString:@"%I" |
|---|
| 245 | | options:0 |
|---|
| 246 | | range:NSMakeRange(0, [time length])]; |
|---|
| 247 | | } |
|---|
| 248 | | |
|---|
| 249 | | // --- kill seconds |
|---|
| 250 | | if(!seconds) { |
|---|
| 251 | | range = [time rangeOfString:@":%S"]; |
|---|
| 252 | | |
|---|
| 253 | | if(range.location != NSNotFound) |
|---|
| 254 | | [time deleteCharactersInRange:range]; |
|---|
| 255 | | |
|---|
| 256 | | range = [time rangeOfString:@".%S"]; |
|---|
| 257 | | |
|---|
| 258 | | if(range.location != NSNotFound) |
|---|
| 259 | | [time deleteCharactersInRange:range]; |
|---|
| 260 | | } |
|---|
| 261 | | |
|---|
| 262 | | string = [self descriptionWithCalendarFormat:time |
|---|
| 263 | | timeZone:NULL |
|---|
| 264 | | locale:[defaults dictionaryRepresentation]]; |
|---|
| 265 | | |
|---|
| 266 | | // --- fix bug where extraneous space characters would be left at the end |
|---|
| 267 | | while([string hasSuffix:@" "]) |
|---|
| 268 | | string = [string substringToIndex:[string length] - 1]; |
|---|
| 269 | | |
|---|
| 270 | | [time release]; |
|---|
| 271 | | |
|---|
| 272 | | return string; |
|---|
| 273 | | } |
|---|
| 274 | | |
|---|
| 275 | | @end |
|---|
| 276 | | |
|---|
| 277 | | |
|---|
| 278 | | |
|---|