Commits

alloy020  committed 9cd5da3

Add `stringFromDate` variant that takes source timezone into account.

  • Participants
  • Parent commits 900c739

Comments (0)

Files changed (6)

File ISO8601DateFormatter.h

 
 - (NSString *) stringFromDate:(NSDate *)date;
 - (NSString *) stringFromDate:(NSDate *)date timeZone:(NSTimeZone *)timeZone;
+- (NSString *) stringFromDate:(NSDate *)date sourceTimeZone:(NSTimeZone *)sourceTimeZone destinationTimeZone:(NSTimeZone *)destinationTimeZone;
+- (NSString *) stringFromDate:(NSDate *)date formatString:(NSString *)dateFormat sourceTimeZone:(NSTimeZone *)sourceTimeZone destinationTimeZone:(NSTimeZone *)destinationTimeZone; // TODO private?
 
 @end

File ISO8601DateFormatter.m

 }
 
 - (NSString *) stringFromDate:(NSDate *)date timeZone:(NSTimeZone *)timeZone {
+  return [self stringFromDate:date sourceTimeZone:nil destinationTimeZone:timeZone];
+}
+
+- (NSString *) stringFromDate:(NSDate *)date sourceTimeZone:(NSTimeZone *)sourceTimeZone destinationTimeZone:(NSTimeZone *)destinationTimeZone {
 	switch (self.format) {
 		case ISO8601DateFormatCalendar:
-			return [self stringFromDate:date formatString:ISO_CALENDAR_DATE_FORMAT timeZone:timeZone];
+       return [self stringFromDate:date formatString:ISO_CALENDAR_DATE_FORMAT sourceTimeZone:sourceTimeZone destinationTimeZone:destinationTimeZone];
 		case ISO8601DateFormatWeek:
-			return [self weekDateStringForDate:date timeZone:timeZone];
+			return [self weekDateStringForDate:date timeZone:destinationTimeZone]; // TODO
 		case ISO8601DateFormatOrdinal:
-			return [self stringFromDate:date formatString:ISO_ORDINAL_DATE_FORMAT timeZone:timeZone];
+			return [self stringFromDate:date formatString:ISO_ORDINAL_DATE_FORMAT sourceTimeZone:sourceTimeZone destinationTimeZone:destinationTimeZone];
 		default:
 			[NSException raise:NSInternalInconsistencyException format:@"self.format was %d, not calendar (%d), week (%d), or ordinal (%d)", self.format, ISO8601DateFormatCalendar, ISO8601DateFormatWeek, ISO8601DateFormatOrdinal];
 			return nil;
 }
 
 - (NSString *) stringFromDate:(NSDate *)date formatString:(NSString *)dateFormat timeZone:(NSTimeZone *)timeZone {
+  return [self stringFromDate:date formatString:dateFormat sourceTimeZone:nil destinationTimeZone:timeZone];
+}
+
+- (NSString *) stringFromDate:(NSDate *)date formatString:(NSString *)dateFormat sourceTimeZone:(NSTimeZone *)sourceTimeZone destinationTimeZone:(NSTimeZone *)destinationTimeZone {
 	if (includeTime)
 		dateFormat = [dateFormat stringByAppendingFormat:@"'T'%@", [self replaceColonsInString:ISO_TIME_FORMAT withTimeSeparator:self.timeSeparator]];
 
-	unparsingCalendar.timeZone = timeZone;
+	unparsingCalendar.timeZone = destinationTimeZone;
 
 	if (dateFormat != lastUsedFormatString) {
 		[unparsingFormatter release];
 		unparsingFormatter = [[NSDateFormatter alloc] init];
 		unparsingFormatter.formatterBehavior = NSDateFormatterBehavior10_4;
 		unparsingFormatter.dateFormat = dateFormat;
+		unparsingFormatter.timeZone = sourceTimeZone;
 		unparsingFormatter.calendar = unparsingCalendar;
 	}
 
 	NSString *str = [unparsingFormatter stringForObjectValue:date];
 
 	if (includeTime) {
-		NSInteger offset = [timeZone secondsFromGMT];
+		NSInteger offset = [destinationTimeZone secondsFromGMT];
 		offset /= 60;  //bring down to minutes
 		if (offset == 0)
 			str = [str stringByAppendingString:ISO_TIMEZONE_UTC_FORMAT];
-CLANG=/Developer/usr/bin/clang
+CLANG=/usr/bin/clang
 CC=$(CLANG)
 CFLAGS+=-std=c99 -g -Werror -Wmissing-field-initializers -Wreturn-type -Wmissing-braces -Wparentheses -Wswitch -Wunused-function -Wunused-label -Wunused-variable -Wunused-value -Wshadow -Wsign-compare -Wnewline-eof -Wshorten-64-to-32 -Wundeclared-selector -Wmissing-prototypes -Wformat -Wunknown-pragmas
 LDFLAGS+=-framework Foundation
 
-all: testparser unparse-weekdate unparse-ordinaldate unparse-date
+all: testparser unparse-weekdate unparse-ordinaldate unparse-date unparse-zones
 test: all parser-test unparser-test
 analysis: ISO8601DateFormatter-analysis.plist
 
 unparse-weekdate: unparse-weekdate.o ISO8601DateFormatter.o ISO8601DateFormatter.o
 unparse-ordinaldate: unparse-ordinaldate.o ISO8601DateFormatter.o ISO8601DateFormatter.o
 unparse-date: unparse-date.o ISO8601DateFormatter.o ISO8601DateFormatter.o
+unparse-zones: unparse-zones.o ISO8601DateFormatter.o ISO8601DateFormatter.o
 
 testunparsewithtime: testunparsewithtime.o ISO8601DateFormatter.o
 

File test_files/testunparser-expected.out

 2008-01-01:	2008-001
 2009-01-01:	2009-001
 2010-01-01:	2010-001
+
+./unparse-zones
+2010-01-01T11:22:33Z

File testunparser.sh

-#!/usr/bin/env zsh -f
+#!/usr/bin/env zsh-f
 echo './unparse-date 199{1,2,3,4,5,6,7,8,9}-01-01 200{0,1,2,3,4,5,6,7,8,9}-01-01 2010-01-01'
 ./unparse-date 199{1,2,3,4,5,6,7,8,9}-01-01 200{0,1,2,3,4,5,6,7,8,9}-01-01 2010-01-01
 echo
 echo
 echo './unparse-ordinaldate 199{1,2,3,4,5,6,7,8,9}-01-01 200{0,1,2,3,4,5,6,7,8,9}-01-01 2010-01-01'
 ./unparse-ordinaldate 199{1,2,3,4,5,6,7,8,9}-01-01 200{0,1,2,3,4,5,6,7,8,9}-01-01 2010-01-01
+echo
+echo './unparse-zones'
+./unparse-zones

File unparse-zones.m

+#import <Foundation/Foundation.h>
+#import "ISO8601DateFormatter.h"
+
+int main(int argc, const char **argv) {
+  NSAutoreleasePool *pool = [NSAutoreleasePool new];
+
+  [NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:+3600]];
+
+  ISO8601DateFormatter *formatter = [[[ISO8601DateFormatter alloc] init] autorelease];
+  formatter.includeTime = YES;
+
+  NSDate *dateInUTC = [formatter dateFromString:@"2010-01-01T11:22:33Z"];
+  NSTimeZone *utc = [NSTimeZone timeZoneForSecondsFromGMT:0];
+  //printf("%s\n", [[formatter stringFromDate:dateInUTC timeZone:utc] UTF8String]);
+  printf("%s\n", [[formatter stringFromDate:dateInUTC sourceTimeZone:utc destinationTimeZone:utc] UTF8String]);
+
+  [pool release];
+  return 0;
+}
+