forked from owainhunt/UAGithubEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSString+UAGithubEngineUtilities.m
60 lines (47 loc) · 1.63 KB
/
NSString+UAGithubEngineUtilities.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// NSString+UAGithubEngineUtilities.m
// UAGithubEngine
//
// Created by Owain Hunt on 08/04/2010.
// Copyright 2010 Owain R Hunt. All rights reserved.
//
#import "NSString+UAGithubEngineUtilities.h"
@implementation NSString(UAGithubEngineUtilities)
- (NSDate *)dateFromGithubDateString {
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
NSString *dateString = self;
// Because Github returns three different date string formats throughout the API,
// we need to check how to process the string based on the format used
if ([[self substringWithRange:NSMakeRange(10, 1)] isEqualToString:@"T"])
{
if ([[self substringWithRange:NSMakeRange([self length] - 1, 1)] isEqualToString:@"Z"])
{
// eg 2010-05-23T21:26:03.921Z (UTC with milliseconds)
if([[self substringWithRange:NSMakeRange(19, 1)] isEqualToString:@"."]){
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
// eg 2010-05-23T21:26:03Z (UTC without milliseconds)
} else {
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
}
}
else
// eg 2010-04-07T12:50:15-07:00
{
NSMutableString *newDate = [self mutableCopy];
[newDate deleteCharactersInRange:NSMakeRange(22, 1)];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
[dateString release];
dateString = newDate;
[newDate release];
}
} else {
// eg 2010/07/28 21:21:00 +0100
[df setDateFormat:@"yyyy/MM/dd HH:mm:ss ZZZ"];
}
return [df dateFromString:dateString];
}
- (NSString *)encodedString
{
return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)@";/?:@&=$+{}<>,", kCFStringEncodingUTF8);
}
@end