#import <objc/objc.h> #import <objc/Object.h> #import <Foundation/Foundation.h> @interface TestObj : NSObject { int num; } - (void)foo; @end @implementation TestObj - (NSDate *)parseRFC3339Date:(NSString *)date { NSDate *parsedDate = nil; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSRange zRange = [date rangeOfString:@"Z"]; if (zRange.length > 0 && zRange.location == date.length - 1) { [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; parsedDate = [dateFormatter dateFromString:date]; if (parsedDate == nil) { [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"]; dateFormatter.lenient = YES; parsedDate = [dateFormatter dateFromString:date]; } return parsedDate; } else { [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"]; parsedDate = [dateFormatter dateFromString:date]; if (parsedDate == nil) { [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ"]; dateFormatter.lenient = YES; parsedDate = [dateFormatter dateFromString:date]; } return parsedDate; } } int main() { id obj = [[TestObj alloc] init]; printf([obj parseRFC3339Date:@"2007-05-01T15:43:26.3452+07:00"] == nil ? "Failure\n" : "Success\n");; printf([obj parseRFC3339Date:@"2007-05-01T15:43:26.3452-07:00"] == nil ? "Failure\n" : "Success\n");; return 0; } @end
Standard input is empty
Success Success Success Success Success Success Success Success Success
2016-04-29 09:05:10.819 prog[10946] autorelease called without pool for object (0x9d5bf90) of class GSSetEnumerator in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9df3f88) of class GSDictionaryKeyEnumerator in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9deb1a0) of class GSMutableDictionary in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9df5cc0) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9dc04e8) of class GSCBufferString in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9df3d58) of class GSCInlineString in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9db0650) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9dc4280) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9dc05b0) of class GSCBufferString in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9dc43f0) of class GSDictionaryKeyEnumerator in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9dc44c0) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9dc0280) of class GSCBufferString in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9df3fe8) of class GSCInlineString in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.820 prog[10946] autorelease called without pool for object (0x9dbb330) of class GSCInlineString in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9ded8d0) of class GSMutableString in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9dcfe08) of class GSCInlineString in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9dc0950) of class GSCInlineString in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9dc4210) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9df51c0) of class NSGDate in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9db6c10) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9dc4518) of class NSArrayEnumerator in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9df4060) of class GSMutableDictionary in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9dbb550) of class GSMutableDictionary in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9dc07c0) of class GSDictionaryKeyEnumerator in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9dd09f8) of class GSMutableDictionary in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] WARNING - unable to create shared user defaults! 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9dc0768) of class GSCBufferString in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9dc06b8) of class NSLocale in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9df63f8) of class GSTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.821 prog[10946] autorelease called without pool for object (0x9dc0710) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.823 prog[10946] autorelease called without pool for object (0x9db2130) of class NSGDate in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.823 prog[10946] autorelease called without pool for object (0x9dc06b8) of class NSLocale in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.823 prog[10946] autorelease called without pool for object (0x9df63f8) of class GSTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.823 prog[10946] autorelease called without pool for object (0x9df2880) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.823 prog[10946] autorelease called without pool for object (0x9df44f0) of class NSGDate in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.823 prog[10946] autorelease called without pool for object (0x9dc06b8) of class NSLocale in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.823 prog[10946] autorelease called without pool for object (0x9df63f8) of class GSTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.823 prog[10946] autorelease called without pool for object (0x9dc68c0) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.824 prog[10946] autorelease called without pool for object (0x9dbab40) of class NSGDate in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.824 prog[10946] autorelease called without pool for object (0x9dc06b8) of class NSLocale in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.824 prog[10946] autorelease called without pool for object (0x9df63f8) of class GSTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.824 prog[10946] autorelease called without pool for object (0x9ddafb0) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.824 prog[10946] autorelease called without pool for object (0x9ddbea0) of class NSGDate in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.825 prog[10946] autorelease called without pool for object (0x9dc06b8) of class NSLocale in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.825 prog[10946] autorelease called without pool for object (0x9df63f8) of class GSTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.825 prog[10946] autorelease called without pool for object (0x9de7090) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.825 prog[10946] autorelease called without pool for object (0x9de9fc8) of class NSGDate in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.825 prog[10946] autorelease called without pool for object (0x9dc06b8) of class NSLocale in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.825 prog[10946] autorelease called without pool for object (0x9df63f8) of class GSTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.825 prog[10946] autorelease called without pool for object (0x9df8338) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.826 prog[10946] autorelease called without pool for object (0x9df8168) of class NSGDate in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.826 prog[10946] autorelease called without pool for object (0x9dc06b8) of class NSLocale in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.826 prog[10946] autorelease called without pool for object (0x9df63f8) of class GSTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.826 prog[10946] autorelease called without pool for object (0x9dfb508) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.826 prog[10946] autorelease called without pool for object (0x9df18f0) of class GSAbsTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.826 prog[10946] autorelease called without pool for object (0x9dfe3f0) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.827 prog[10946] autorelease called without pool for object (0x9dfc5a8) of class NSGDate in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.827 prog[10946] autorelease called without pool for object (0x9dc06b8) of class NSLocale in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.827 prog[10946] autorelease called without pool for object (0x9df63f8) of class GSTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.827 prog[10946] autorelease called without pool for object (0x9dfb4d0) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.827 prog[10946] autorelease called without pool for object (0x9df18f0) of class GSAbsTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.827 prog[10946] autorelease called without pool for object (0x9e00de8) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.828 prog[10946] autorelease called without pool for object (0x9dff1b0) of class NSGDate in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.828 prog[10946] autorelease called without pool for object (0x9dc06b8) of class NSLocale in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.828 prog[10946] autorelease called without pool for object (0x9df63f8) of class GSTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.828 prog[10946] autorelease called without pool for object (0x9dfb2e0) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.828 prog[10946] autorelease called without pool for object (0x9df18f0) of class GSAbsTimeZone in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.828 prog[10946] autorelease called without pool for object (0x9dfe158) of class GSAutoreleasedMemory in thread <NSThread: 0x9d62800> 2016-04-29 09:05:10.829 prog[10946] autorelease called without pool for object (0x9e00d50) of class NSGDate in thread <NSThread: 0x9d62800>