-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPXUtilities.m
142 lines (116 loc) · 5.25 KB
/
PXUtilities.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// PXUtilities.m
//
// Created by Daniel Blakemore on 5/12/14.
//
// Copyright (c) 2015 Pixio
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "PXUtilities.h"
#import "NSString+MD5.h"
@implementation PXUtilities
+ (NSURL*)convertStringToURL:(NSString*)string
{
if (string == nil)
{
return nil;
}
// find link
static NSDataDetector * detector = nil;
if (!detector) {
detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:NULL];
}
NSArray * matches = [detector matchesInString:string options:kNilOptions range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult * result in matches) {
if ([result resultType] == NSTextCheckingTypeLink) {
return [result URL];
}
}
// no match found
return nil;
}
+ (NSString*)formatDateRelativeToNow:(NSDate*)date
{
return [PXUtilities formatDateRelativeToNow:date shortFormat:FALSE];
}
+ (NSString*)formatDateRelativeToNow:(NSDate*)date shortFormat:(BOOL)sf
{
NSCalendar * calendar = [NSCalendar autoupdatingCurrentCalendar];
// get years
NSInteger yearsBetween = [[calendar components:NSCalendarUnitYear fromDate:date toDate:[NSDate date] options:0] year];
if (yearsBetween > 0) {
// it's been years, say that
if (yearsBetween == 1) {
return sf ? NSLocalizedString(@"1y", nil) : NSLocalizedString(@"1 year ago", nil);
}
return [NSString stringWithFormat:sf ? NSLocalizedString(@"%dy", nil) : NSLocalizedString(@"%d years ago", nil), yearsBetween];
}
// get months
NSInteger monthsBetween = [[calendar components:NSCalendarUnitMonth fromDate:date toDate:[NSDate date] options:0] month];
if (monthsBetween > 0) {
// it's been months, say that
if (monthsBetween == 1) {
return sf ? NSLocalizedString(@"1M", nil) : NSLocalizedString(@"1 month ago", nil);
}
return [NSString stringWithFormat:sf ? NSLocalizedString(@"%dM", nil) : NSLocalizedString(@"%d months ago", nil), monthsBetween];
}
// get weeks
NSInteger daysBetween = [[calendar components:NSCalendarUnitDay fromDate:date toDate:[NSDate date] options:0] day];
if (daysBetween > 7) {
// it's been weeks, say that
if (daysBetween / 7 == 1) {
return sf ? NSLocalizedString(@"1w", nil) : NSLocalizedString(@"1 week ago", nil);
}
return [NSString stringWithFormat:sf ? NSLocalizedString(@"%dw", nil) : NSLocalizedString(@"%d weeks ago", nil), daysBetween / 7];
}
// get days
if (daysBetween > 0) {
// it's been DAYS, say that
if (daysBetween == 1) {
return sf ? NSLocalizedString(@"1d", nil) : NSLocalizedString(@"1 day ago", nil);
}
return [NSString stringWithFormat:sf ? NSLocalizedString(@"%dd", nil) : NSLocalizedString(@"%d days ago", nil), daysBetween];
}
// get hours
NSInteger hoursBetween = [[calendar components:NSCalendarUnitHour fromDate:date toDate:[NSDate date] options:0] hour];
if (hoursBetween > 0) {
// it's been hours, say that
if (hoursBetween == 1) {
return sf ? NSLocalizedString(@"1h", nil) : NSLocalizedString(@"1 hour ago", nil);
}
return [NSString stringWithFormat:sf ? NSLocalizedString(@"%dh", nil) : NSLocalizedString(@"%d hours ago", nil), hoursBetween];
}
// get minutes
NSInteger minutesBetween = [[calendar components:NSCalendarUnitMinute fromDate:date toDate:[NSDate date] options:0] minute];
if (minutesBetween > 0) {
// it's been hours, say that
if (minutesBetween == 1) {
return sf ? NSLocalizedString(@"1m", nil) : NSLocalizedString(@"1 minute ago", nil);
}
return [NSString stringWithFormat:sf ? NSLocalizedString(@"%dm", nil) : NSLocalizedString(@"%d minutes ago", nil), minutesBetween];
}
return sf ? NSLocalizedString(@"now", nil) : NSLocalizedString(@"just now", nil);
}
+ (NSURL*)gravatarURLForEmail:(NSString*)email
{
NSString *gravatarPath = [NSString stringWithFormat:@"http://gravatar.com/avatar/%@?&default=retro", [email md5string]];
return [NSURL URLWithString:gravatarPath];
}
@end