Created
August 25, 2013 18:22
-
-
Save pseudomuto/6335411 to your computer and use it in GitHub Desktop.
Blog Code: NSData from OData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSDate (OData) | |
- (id)initWithODataString:(NSString *)dateString; | |
- (NSString *)toODataQueryFormat; | |
+ (NSDate *)dateFromString:(NSString *)dateString; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSDate+OData.h" | |
@implementation NSDate (OData) | |
- (id)initWithODataString:(NSString *)dateString | |
{ | |
return [[NSDate dateFromString:dateString] retain]; | |
} | |
- (NSString *)toODataQueryFormat | |
{ | |
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; | |
NSString *formattedDate = [formatter stringFromDate:self]; | |
[formatter release]; | |
return formattedDate; | |
} | |
+ (NSDate *)dateFromString:(NSString *)dateString | |
{ | |
NSInteger startIndex = [dateString rangeOfString:@"("].location; | |
NSInteger endIndex = [dateString rangeOfString:@")"].location - startIndex; | |
long long ticks = [[dateString substringWithRange:NSMakeRange(startIndex + 1, endIndex)] longLongValue]; | |
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(ticks / 1000)]; | |
return date; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment