Permalink
Browse files

Adding date transformers (ISO 8601 Timestamp <-> NSDate)

  • Loading branch information...
mattt committed Apr 26, 2013
1 parent 3be814b commit c064303c52eb04e220e539b907477eefeb640035
@@ -10,6 +10,7 @@
F818950F164DBA9900D19548 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F818950E164DBA9900D19548 /* main.m */; };
F818951F164DBB0600D19548 /* NSValueTransformer+TransformerKit.m in Sources */ = {isa = PBXBuildFile; fileRef = F818951B164DBB0600D19548 /* NSValueTransformer+TransformerKit.m */; };
F8189520164DBB0600D19548 /* TTTStringTransformers.m in Sources */ = {isa = PBXBuildFile; fileRef = F818951D164DBB0600D19548 /* TTTStringTransformers.m */; };
+ F83F36DD1729BC4500FB13E3 /* TTTDateTransformers.m in Sources */ = {isa = PBXBuildFile; fileRef = F83F36DC1729BC4500FB13E3 /* TTTDateTransformers.m */; };
F8AC4E11164DBA440083D561 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8AC4E10164DBA440083D561 /* Foundation.framework */; };
F8E5DBCA165578FB002C26DC /* TTTImageTransformers.m in Sources */ = {isa = PBXBuildFile; fileRef = F8E5DBC9165578FB002C26DC /* TTTImageTransformers.m */; };
/* End PBXBuildFile section */
@@ -34,6 +35,8 @@
F818951C164DBB0600D19548 /* TTTStringTransformers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TTTStringTransformers.h; sourceTree = "<group>"; };
F818951D164DBB0600D19548 /* TTTStringTransformers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TTTStringTransformers.m; sourceTree = "<group>"; };
F818951E164DBB0600D19548 /* TransformerKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TransformerKit.h; sourceTree = "<group>"; };
+ F83F36DB1729BC4500FB13E3 /* TTTDateTransformers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TTTDateTransformers.h; sourceTree = "<group>"; };
+ F83F36DC1729BC4500FB13E3 /* TTTDateTransformers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TTTDateTransformers.m; sourceTree = "<group>"; };
F8AC4E0C164DBA440083D561 /* TransformerKit Example */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "TransformerKit Example"; sourceTree = BUILT_PRODUCTS_DIR; };
F8AC4E10164DBA440083D561 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
F8E5DBC8165578FB002C26DC /* TTTImageTransformers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TTTImageTransformers.h; sourceTree = "<group>"; };
@@ -64,10 +67,12 @@
isa = PBXGroup;
children = (
F818951E164DBB0600D19548 /* TransformerKit.h */,
- F818951C164DBB0600D19548 /* TTTStringTransformers.h */,
- F818951D164DBB0600D19548 /* TTTStringTransformers.m */,
+ F83F36DB1729BC4500FB13E3 /* TTTDateTransformers.h */,
+ F83F36DC1729BC4500FB13E3 /* TTTDateTransformers.m */,
F8E5DBC8165578FB002C26DC /* TTTImageTransformers.h */,
F8E5DBC9165578FB002C26DC /* TTTImageTransformers.m */,
+ F818951C164DBB0600D19548 /* TTTStringTransformers.h */,
+ F818951D164DBB0600D19548 /* TTTStringTransformers.m */,
F818951A164DBB0600D19548 /* NSValueTransformer+TransformerKit.h */,
F818951B164DBB0600D19548 /* NSValueTransformer+TransformerKit.m */,
);
@@ -173,6 +178,7 @@
F818951F164DBB0600D19548 /* NSValueTransformer+TransformerKit.m in Sources */,
F8189520164DBB0600D19548 /* TTTStringTransformers.m in Sources */,
F8E5DBCA165578FB002C26DC /* TTTImageTransformers.m in Sources */,
+ F83F36DD1729BC4500FB13E3 /* TTTDateTransformers.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -1,10 +1,23 @@
+// main.m
+// Copyright (c) 2012 Mattt Thompson (http://mattt.me)
//
-// main.m
-// TransformerKit Example
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
//
-// Created by Mattt Thompson on 2012/11/09.
-// Copyright (c) 2012年 Mattt Thompson. All rights reserved.
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import <Foundation/Foundation.h>
@@ -13,21 +26,27 @@
int main(int argc, const char * argv[]) {
@autoreleasepool {
[@{
- @"Capitalized String" : TTTCapitalizedStringTransformerName,
- @"Uppercase String" : TTTUppercaseStringTransformerName,
- @"Lowercase String" : TTTLowercaseStringTransformerName,
- @"Camel Case String" : TTTCamelCaseStringTransformerName,
- @"Llama Case String" : TTTLlamaCaseStringTransformerName,
- @"Snake Case String" : TTTSnakeCaseStringTransformerName,
- @"Train Case String" : TTTTrainCaseStringTransformerName,
- @"Reversed String" : TTTReverseStringTransformerName,
- @"Rémövê Dîaçritics" : TTTRemoveDiacriticStringTransformerName,
- @"ट्रांस्लितेराते स्ट्रिंग" : TTTTransliterateStringToLatinTransformerName,
- } enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
- NSLog(@"%@: %@", key, [[NSValueTransformer valueTransformerForName:obj] transformedValue:key]);
+ @"Capitalized String" : TTTCapitalizedStringTransformerName,
+ @"Uppercase String" : TTTUppercaseStringTransformerName,
+ @"Lowercase String" : TTTLowercaseStringTransformerName,
+ @"Camel Case String" : TTTCamelCaseStringTransformerName,
+ @"Llama Case String" : TTTLlamaCaseStringTransformerName,
+ @"Snake Case String" : TTTSnakeCaseStringTransformerName,
+ @"Train Case String" : TTTTrainCaseStringTransformerName,
+ @"Reversed String" : TTTReverseStringTransformerName,
+ @"Rémövê Dîaçritics" : TTTRemoveDiacriticStringTransformerName,
+ @"ट्रांस्लितेराते स्ट्रिंग" : TTTTransliterateStringToLatinTransformerName,
+ } enumerateKeysAndObjectsUsingBlock:^(id value, id name, BOOL *stop) {
+ NSLog(@"%@: %@", value, [[NSValueTransformer valueTransformerForName:name] transformedValue:value]);
+ }];
+
+ [@[TTTISO8601DateTransformerName] enumerateObjectsUsingBlock:^(id name, NSUInteger idx, BOOL *stop) {
+ NSValueTransformer *transformer = [NSValueTransformer valueTransformerForName:name];
+ NSLog(@"%@ (Timestamp): %@", name, [transformer transformedValue:[NSDate date]]);
+ NSLog(@"%@ (Date): %@", name, [transformer reverseTransformedValue:[transformer transformedValue:[NSDate date]]]);
}];
}
-
+
return 0;
}
@@ -0,0 +1,32 @@
+// TTTDateTransformers.h
+//
+// Copyright (c) 2013 Mattt Thompson
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#import <Foundation/Foundation.h>
+
+/**
+
+ */
+extern NSString * const TTTISO8601DateTransformerName;
+
+@interface TTTDateTransformers : NSObject
+
+@end
@@ -0,0 +1,87 @@
+// TTTDateTransformers.m
+//
+// Copyright (c) 2013 Mattt Thompson
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#import "TTTDateTransformers.h"
+
+#import "NSValueTransformer+TransformerKit.h"
+
+#include <time.h>
+#include <xlocale.h>
+
+NSString * const TTTISO8601DateTransformerName = @"TTTISO8601DateTransformerName";
+
+static NSString * TTTISO8601TimestampFromDate(NSDate *date) {
+ static NSDateFormatter *_iso8601DateFormatter = nil;
+ static dispatch_once_t onceToken;
+ dispatch_once(&onceToken, ^{
+ _iso8601DateFormatter = [[NSDateFormatter alloc] initWithDateFormat:@"%Y-%m-%dT%H:%M:%S%z" allowNaturalLanguage:NO];
+ [_iso8601DateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
+ });
+
+ return [_iso8601DateFormatter stringFromDate:date];
+}
+
+static NSDate * TTTDateFromISO8601Timestamp(NSString *timestamp) {
+ static int const ISO_8601_MAX_LENGTH = 25;
+
+ const char *source = [timestamp cStringUsingEncoding:NSUTF8StringEncoding];
+ char destination[ISO_8601_MAX_LENGTH];
+ size_t length = strlen(source);
+
+ if (length == 0) {
+ return nil;
+ }
+
+ if (length == 20 && source[length - 1] == 'Z') {
+ memcpy(destination, source, length - 1);
+ strncpy(destination + length - 1, "+0000\0", 6);
+ } else if (length == 25 && source[22] == ':') {
+ memcpy(destination, source, 22);
+ memcpy(destination + 22, source + 23, 2);
+ } else {
+ memcpy(destination, source, MIN(length, ISO_8601_MAX_LENGTH - 1));
+ }
+
+ destination[sizeof(destination) - 1] = NULL;
+
+ struct tm time = {
+ .tm_isdst = -1,
+ };
+
+ strptime_l(source, "%FT%T%z", &time, NULL);
+
+ return [NSDate dateWithTimeIntervalSince1970:mktime(&time)];
+}
+
+@implementation TTTDateTransformers
+
++ (void)load {
+ @autoreleasepool {
+ [NSValueTransformer registerValueTransformerWithName:TTTISO8601DateTransformerName transformedValueClass:[NSDate class] returningTransformedValueWithBlock:^id(id value) {
+ return TTTISO8601TimestampFromDate(value);
+ } allowingReverseTransformationWithBlock:^id(id value) {
+ return TTTDateFromISO8601Timestamp(value);
+ }];
+ }
+}
+
+@end
@@ -23,5 +23,7 @@
#ifndef _TRANSFORMER_KIT_
#define _TRANSFORMER_KIT_
+ #import "TTTDateTransformers.h"
+ #import "TTTImageTransformers.h"
#import "TTTStringTransformers.h"
#endif

0 comments on commit c064303

Please sign in to comment.