diff --git a/FCUtilities/NSDate+FCUtilities.h b/FCUtilities/NSDate+FCUtilities.h new file mode 100644 index 0000000..84cc89a --- /dev/null +++ b/FCUtilities/NSDate+FCUtilities.h @@ -0,0 +1,14 @@ +// +// NSDate+FCUtilities.h +// Part of FCUtilities by Marco Arment (Added NSDate Luke Durrant). See included LICENSE file for BSD license. +// + + +#import +// Also requires libz to be linked, but "@import libz;" doesn't work, presumably because it's not a full-fledged framework + +@interface NSDate (FCUtilities) + ++(NSDate *)fc_dateWithDotNetJSONString:(NSString *)string; +-(NSString*)fc_DateString; +@end diff --git a/FCUtilities/NSDate+FCUtilities.m b/FCUtilities/NSDate+FCUtilities.m new file mode 100644 index 0000000..b530387 --- /dev/null +++ b/FCUtilities/NSDate+FCUtilities.m @@ -0,0 +1,46 @@ +// +// NSDate+FCUtilities.h.m +// Part of FCUtilities by Marco Arment (Added NSDate Luke Durrant). See included LICENSE file for BSD license. +// + +#import "NSData+FCUtilities.h" + +@implementation NSDate (FCUtilities) + ++(NSDate *)fc_dateWithDotNetJSONString:(NSString *)string { + static NSRegularExpression *dateRegEx = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + dateRegEx = [[NSRegularExpression alloc] initWithPattern:@"^\\/date\\((-?\\d++)(?:([+-])(\\d{2})(\\d{2}))?\\)\\/$" options:NSRegularExpressionCaseInsensitive error:nil]; + }); + NSTextCheckingResult *regexResult = [dateRegEx firstMatchInString:string options:0 range:NSMakeRange(0, [string length])]; + + if (regexResult) { + // milliseconds + NSTimeInterval seconds = [[string substringWithRange:[regexResult rangeAtIndex:1]] doubleValue] / 1000.0; + // timezone offset + if ([regexResult rangeAtIndex:2].location != NSNotFound) { + NSString *sign = [string substringWithRange:[regexResult rangeAtIndex:2]]; + // hours + seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:3]]] doubleValue] * 60.0 * 60.0; + // minutes + seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:4]]] doubleValue] * 60.0; + } + + return [NSDate dateWithTimeIntervalSince1970:seconds]; + } + return nil; +} + +-(NSString*)fc_DateString +{ + + NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; + [dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"]; + + NSString *dateStr = [dateFormat stringFromDate:self]; + + return dateStr; +} + +@end diff --git a/FCUtilities/NSString+FCUtilities.h b/FCUtilities/NSString+FCUtilities.h index 16e7e8c..f9bb054 100644 --- a/FCUtilities/NSString+FCUtilities.h +++ b/FCUtilities/NSString+FCUtilities.h @@ -23,5 +23,6 @@ - (NSString *)fc_HTMLEncodedString; - (NSString *)fc_hexString; - (NSString *)fc_MD5Digest; ++ (NSString *)fc_NewGuidString; @end diff --git a/FCUtilities/NSString+FCUtilities.m b/FCUtilities/NSString+FCUtilities.m index 7a2d917..009ec44 100644 --- a/FCUtilities/NSString+FCUtilities.m +++ b/FCUtilities/NSString+FCUtilities.m @@ -115,4 +115,12 @@ - (NSString *)fc_MD5Digest return stringBuffer; } ++ (NSString*)fc_NewGuidString +{ + NSUUID *UUID = [NSUUID UUID]; + NSString* stringUUID = [UUID UUIDString]; + + return [stringUUID lowercaseString]; +} + @end