forked from kevinlawler/NSDate-TimeAgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSDate+Extension.swift
177 lines (142 loc) · 5.33 KB
/
NSDate+Extension.swift
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// NSDate+Extension.swift
// Tasty
//
// Created by Vitaliy Kuzmenko on 17/10/14.
// http://github.com/vitkuzmenko
// Copyright (c) 2014 Vitaliy Kuz'menko. All rights reserved.
//
import Foundation
func NSDateTimeAgoLocalizedStrings(key: String) -> String {
let resourcePath: String?
if let frameworkBundle = NSBundle(identifier: "com.kevinlawler.NSDateTimeAgo") {
// Load from Framework
resourcePath = frameworkBundle.resourcePath
} else {
// Load from Main Bundle
resourcePath = NSBundle.mainBundle().resourcePath
}
if resourcePath == nil {
return ""
}
let path = NSURL(fileURLWithPath: resourcePath!).URLByAppendingPathComponent("NSDateTimeAgo.bundle")
guard let bundle = NSBundle(URL: path) else {
return ""
}
return NSLocalizedString(key, tableName: "NSDateTimeAgo", bundle: bundle, comment: "")
}
extension NSDate {
// shows 1 or two letter abbreviation for units.
// does not include 'ago' text ... just {value}{unit-abbreviation}
// does not include interim summary options such as 'Just now'
public var timeAgoSimple: String {
let components = self.dateComponents()
if components.year > 0 {
return stringFromFormat("%%d%@yr", withValue: components.year)
}
if components.month > 0 {
return stringFromFormat("%%d%@mo", withValue: components.month)
}
// TODO: localize for other calanders
if components.day >= 7 {
let value = components.day/7
return stringFromFormat("%%d%@w", withValue: value)
}
if components.day > 0 {
return stringFromFormat("%%d%@d", withValue: components.day)
}
if components.hour > 0 {
return stringFromFormat("%%d%@h", withValue: components.hour)
}
if components.minute > 0 {
return stringFromFormat("%%d%@m", withValue: components.minute)
}
if components.second > 0 {
return stringFromFormat("%%d%@s", withValue: components.second )
}
return ""
}
public var timeAgo: String {
let components = self.dateComponents()
if components.year > 0 {
if components.year < 2 {
return NSDateTimeAgoLocalizedStrings("Last year")
} else {
return stringFromFormat("%%d %@years ago", withValue: components.year)
}
}
if components.month > 0 {
if components.month < 2 {
return NSDateTimeAgoLocalizedStrings("Last month")
} else {
return stringFromFormat("%%d %@months ago", withValue: components.month)
}
}
// TODO: localize for other calanders
if components.day >= 7 {
let week = components.day/7
if week < 2 {
return NSDateTimeAgoLocalizedStrings("Last week")
} else {
return stringFromFormat("%%d %@weeks ago", withValue: week)
}
}
if components.day > 0 {
if components.day < 2 {
return NSDateTimeAgoLocalizedStrings("Yesterday")
} else {
return stringFromFormat("%%d %@days ago", withValue: components.day)
}
}
if components.hour > 0 {
if components.hour < 2 {
return NSDateTimeAgoLocalizedStrings("An hour ago")
} else {
return stringFromFormat("%%d %@hours ago", withValue: components.hour)
}
}
if components.minute > 0 {
if components.minute < 2 {
return NSDateTimeAgoLocalizedStrings("A minute ago")
} else {
return stringFromFormat("%%d %@minutes ago", withValue: components.minute)
}
}
if components.second > 0 {
if components.second < 5 {
return NSDateTimeAgoLocalizedStrings("Just now")
} else {
return stringFromFormat("%%d %@seconds ago", withValue: components.second)
}
}
return ""
}
private func dateComponents() -> NSDateComponents {
let calander = NSCalendar.currentCalendar()
return calander.components([.Second, .Minute, .Hour, .Day, .Month, .Year], fromDate: self, toDate: NSDate(), options: [])
}
private func stringFromFormat(format: String, withValue value: Int) -> String {
let localeFormat = String(format: format, getLocaleFormatUnderscoresWithValue(Double(value)))
return String(format: NSDateTimeAgoLocalizedStrings(localeFormat), value)
}
private func getLocaleFormatUnderscoresWithValue(value: Double) -> String {
guard let localeCode = NSLocale.preferredLanguages().first else {
return ""
}
// Russian (ru) and Ukrainian (uk)
if localeCode == "ru" || localeCode == "uk" {
let XY = Int(floor(value)) % 100
let Y = Int(floor(value)) % 10
if Y == 0 || Y > 4 || (XY > 10 && XY < 15) {
return ""
}
if Y > 1 && Y < 5 && (XY < 10 || XY > 20) {
return "_"
}
if Y == 1 && XY != 11 {
return "__"
}
}
return ""
}
}