Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GUID String #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions FCUtilities/NSDate+FCUtilities.h
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>
// 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
46 changes: 46 additions & 0 deletions FCUtilities/NSDate+FCUtilities.m
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions FCUtilities/NSString+FCUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
- (NSString *)fc_HTMLEncodedString;
- (NSString *)fc_hexString;
- (NSString *)fc_MD5Digest;
+ (NSString *)fc_NewGuidString;

@end
8 changes: 8 additions & 0 deletions FCUtilities/NSString+FCUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,12 @@ - (NSString *)fc_MD5Digest
return stringBuffer;
}

+ (NSString*)fc_NewGuidString
{
NSUUID *UUID = [NSUUID UUID];
NSString* stringUUID = [UUID UUIDString];

return [stringUUID lowercaseString];
}

@end