Skip to content
Browse files

Added regex to check for unix timestamp during import, and added a me…

…thod to handle found timestamps
  • Loading branch information...
1 parent 477049d commit 3bfa60e4c1b8e7173da4dccb73f6f3d0ff3786c7 @squarefrog committed Feb 18, 2013
View
1 MagicalRecord/Categories/DataImport/MagicalImportFunctions.h
@@ -11,6 +11,7 @@
NSDate * adjustDateForDST(NSDate *date);
NSDate * dateFromString(NSString *value, NSString *format);
+NSDate * dateFromUnixTimestamp(NSInteger value);
NSString * attributeNameFromString(NSString *value);
NSString * primaryKeyNameFromString(NSString *value);
View
6 MagicalRecord/Categories/DataImport/MagicalImportFunctions.m
@@ -43,6 +43,12 @@
return parsedDate;
}
+NSDate * dateFromUnixTimestamp(NSInteger timestamp)
+{
+ NSDate *parsedDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
+ return parsedDate;
+}
+
NSInteger* newColorComponentsFromString(NSString *serializedColor);
NSInteger* newColorComponentsFromString(NSString *serializedColor)
{
View
23 MagicalRecord/Categories/DataImport/NSAttributeDescription+MagicalDataImport.m
@@ -36,8 +36,27 @@ - (id) MR_valueForKeyPath:(NSString *)keyPath fromObjectData:(id)objectData;
{
if (![value isKindOfClass:[NSDate class]])
{
- NSString *dateFormat = [[self userInfo] valueForKey:kMagicalRecordImportCustomDateFormatKey];
- value = dateFromString([value description], dateFormat ?: kMagicalRecordImportDefaultDateFormatString);
+ NSError *error = NULL;
+ NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]{10}"
+ options:NSRegularExpressionCaseInsensitive
+ error:&error];
+
+ NSTextCheckingResult *match = [regex firstMatchInString:[value description]
+ options:0
+ range:NSMakeRange(0, [[value description] length])];
+
+ if (match)
+ {
+ NSString *timestamp = [value substringWithRange: [match range]];
+ value = dateFromUnixTimestamp([timestamp integerValue]);
+ }
+
+ else
+ {
+ NSString *dateFormat = [[self userInfo] valueForKey:kMagicalRecordImportCustomDateFormatKey];
+ value = dateFromString([value description], dateFormat ?: kMagicalRecordImportDefaultDateFormatString);
+ }
+
}
// value = adjustDateForDST(value);
}

0 comments on commit 3bfa60e

Please sign in to comment.
Something went wrong with that request. Please try again.