Anonymous avatar Anonymous committed 8364d6f

Added support for a custom time separator in the unparser.

Comments (0)

Files changed (2)

NSCalendarDate+ISO8601Unparsing.h

 /*This addition unparses dates to ISO 8601 strings. A good introduction to ISO 8601: <http://www.cl.cam.ac.uk/~mgk25/iso-time.html>
  */
 
+//The default separator for time values. Currently, this is ':'.
+extern unichar ISO8601UnparserDefaultTimeSeparatorCharacter;
+
 @interface NSCalendarDate(ISO8601Unparsing)
 
+- (NSString *)ISO8601DateStringWithTime:(BOOL)includeTime timeSeparator:(unichar)timeSep;
+- (NSString *)ISO8601WeekDateStringWithTime:(BOOL)includeTime timeSeparator:(unichar)timeSep;
+- (NSString *)ISO8601OrdinalDateStringWithTime:(BOOL)includeTime timeSeparator:(unichar)timeSep;
+
 - (NSString *)ISO8601DateStringWithTime:(BOOL)includeTime;
 - (NSString *)ISO8601WeekDateStringWithTime:(BOOL)includeTime;
 - (NSString *)ISO8601OrdinalDateStringWithTime:(BOOL)includeTime;
 
 //includeTime: YES.
+- (NSString *)ISO8601DateStringWithTimeSeparator:(unichar)timeSep;
+- (NSString *)ISO8601WeekDateStringWithTimeSeparator:(unichar)timeSep;
+- (NSString *)ISO8601OrdinalDateStringWithTimeSeparator:(unichar)timeSep;
+
+//includeTime: YES.
 - (NSString *)ISO8601DateString;
 - (NSString *)ISO8601WeekDateString;
 - (NSString *)ISO8601OrdinalDateString;

NSCalendarDate+ISO8601Unparsing.m

 
 #import <Foundation/Foundation.h>
 
+#ifndef DEFAULT_TIME_SEPARATOR
+#	define DEFAULT_TIME_SEPARATOR ':'
+#endif
+unichar ISO8601UnparserDefaultTimeSeparatorCharacter = DEFAULT_TIME_SEPARATOR;
+
 static BOOL is_leap_year(unsigned year) {
 	return \
 	    ((year %   4U) == 0U)
 	||  ((year % 400U) == 0U));
 }
 
+@interface NSString(ISO8601Unparsing)
+
+//Replace all occurrences of ':' with timeSep.
+- (NSString *)prepareDateFormatWithTimeSeparator:(unichar)timeSep;
+
+@end
+
 @implementation NSCalendarDate(ISO8601Unparsing)
 
 #pragma mark Public methods
 
-- (NSString *)ISO8601DateStringWithTime:(BOOL)includeTime {
-	NSDateFormatter *formatter = [[NSDateFormatter alloc] initWithDateFormat:(includeTime ? @"%Y-%m-%dT%H:%M:%S" : @"%Y-%m-%d") allowNaturalLanguage:NO];
+- (NSString *)ISO8601DateStringWithTime:(BOOL)includeTime timeSeparator:(unichar)timeSep {
+	NSString *dateFormat = [(includeTime ? @"%Y-%m-%dT%H:%M:%S" : @"%Y-%m-%d") prepareDateFormatWithTimeSeparator:timeSep];
+	NSDateFormatter *formatter = [[NSDateFormatter alloc] initWithDateFormat:dateFormat allowNaturalLanguage:NO];
 	NSString *str = [formatter stringForObjectValue:self];
 	[formatter release];
 	if(includeTime) {
  *	Rick McCarty, 1999
  *	http://personal.ecu.edu/mccartyr/ISOwdALG.txt
  */
-- (NSString *)ISO8601WeekDateStringWithTime:(BOOL)includeTime {
+- (NSString *)ISO8601WeekDateStringWithTime:(BOOL)includeTime timeSeparator:(unichar)timeSep {
 	enum {
 		monday, tuesday, wednesday, thursday, friday, saturday, sunday
 	};
 
 	NSString *timeString;
 	if(includeTime) {
-		NSDateFormatter *formatter = [[NSDateFormatter alloc] initWithDateFormat:@"T%H:%M:%S%z" allowNaturalLanguage:NO];
+		NSDateFormatter *formatter = [[NSDateFormatter alloc] initWithDateFormat:[@"T%H:%M:%S%z" prepareDateFormatWithTimeSeparator:timeSep] allowNaturalLanguage:NO];
 		timeString = [formatter stringForObjectValue:self];
 		[formatter release];
 	} else
 
 	return [NSString stringWithFormat:@"%u-W%02u-%02u%@", year, week, dayOfWeek + 1U, timeString];
 }
-- (NSString *)ISO8601OrdinalDateStringWithTime:(BOOL)includeTime {
+- (NSString *)ISO8601OrdinalDateStringWithTime:(BOOL)includeTime timeSeparator:(unichar)timeSep {
 	NSString *timeString;
 	if(includeTime) {
-		NSDateFormatter *formatter = [[NSDateFormatter alloc] initWithDateFormat:@"T%H:%M:%S%z" allowNaturalLanguage:NO];
+		NSDateFormatter *formatter = [[NSDateFormatter alloc] initWithDateFormat:[@"T%H:%M:%S%z" prepareDateFormatWithTimeSeparator:timeSep] allowNaturalLanguage:NO];
 		timeString = [formatter stringForObjectValue:self];
 		[formatter release];
 	} else
 
 #pragma mark -
 
+- (NSString *)ISO8601DateStringWithTime:(BOOL)includeTime {
+	return [self ISO8601DateStringWithTime:includeTime timeSeparator:ISO8601UnparserDefaultTimeSeparatorCharacter];
+}
+- (NSString *)ISO8601WeekDateStringWithTime:(BOOL)includeTime {
+	return [self ISO8601WeekDateStringWithTime:includeTime timeSeparator:ISO8601UnparserDefaultTimeSeparatorCharacter];
+}
+- (NSString *)ISO8601OrdinalDateStringWithTime:(BOOL)includeTime {
+	return [self ISO8601OrdinalDateStringWithTime:includeTime timeSeparator:ISO8601UnparserDefaultTimeSeparatorCharacter];
+}
+
+#pragma mark -
+
+- (NSString *)ISO8601DateStringWithTimeSeparator:(unichar)timeSep {
+	return [self ISO8601DateStringWithTime:YES timeSeparator:timeSep];
+}
+- (NSString *)ISO8601WeekDateStringWithTimeSeparator:(unichar)timeSep {
+	return [self ISO8601WeekDateStringWithTime:YES timeSeparator:timeSep];
+}
+- (NSString *)ISO8601OrdinalDateStringWithTimeSeparator:(unichar)timeSep {
+	return [self ISO8601OrdinalDateStringWithTime:YES timeSeparator:timeSep];
+}
+
+#pragma mark -
+
 - (NSString *)ISO8601DateString {
-	return [self ISO8601DateStringWithTime:YES];
+	return [self ISO8601DateStringWithTime:YES timeSeparator:ISO8601UnparserDefaultTimeSeparatorCharacter];
 }
 - (NSString *)ISO8601WeekDateString {
-	return [self ISO8601WeekDateStringWithTime:YES];
+	return [self ISO8601WeekDateStringWithTime:YES timeSeparator:ISO8601UnparserDefaultTimeSeparatorCharacter];
 }
 - (NSString *)ISO8601OrdinalDateString {
-	return [self ISO8601OrdinalDateStringWithTime:YES];
+	return [self ISO8601OrdinalDateStringWithTime:YES timeSeparator:ISO8601UnparserDefaultTimeSeparatorCharacter];
 }
 
 @end
+
+@implementation NSString(ISO8601Unparsing)
+
+//Replace all occurrences of ':' with timeSep.
+- (NSString *)prepareDateFormatWithTimeSeparator:(unichar)timeSep {
+	NSString *dateFormat = self;
+	if(timeSep != ':') {
+		NSMutableString *dateFormatMutable = [[dateFormat mutableCopy] autorelease];
+		[dateFormatMutable replaceOccurrencesOfString:@":"
+		                               	   withString:[NSString stringWithCharacters:&timeSep length:1U]
+	                                      	  options:NSBackwardsSearch | NSLiteralSearch
+	                                        	range:(NSRange){ 0U, [dateFormat length] }];
+		dateFormat = dateFormatMutable;
+	}
+	return dateFormat;
+}
+
+@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.