-
Notifications
You must be signed in to change notification settings - Fork 2
/
time.swift
363 lines (302 loc) · 10.2 KB
/
time.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// This class holds a buffer to C's tm (CTime) struct. The tm struct is
// needed for retrieving data from C functions such as localtime_r and gmtime_r.
// This is a low level class that still exposes many of the C level interfaces.
public struct TimeBuffer: CustomStringConvertible {
public var buffer: CTime
public var _utc = false
public init(secondsSinceEpoch: Int) {
var b = Sys.timeBuffer()
Sys.localtime_r(secondsSinceEpoch: secondsSinceEpoch, buffer: &b)
buffer = b
}
public init() {
self.init(secondsSinceEpoch: Sys.time())
}
public init(buffer: CTime, utc: Bool = false) {
self.buffer = buffer
_utc = utc
}
public var isUtc: Bool { return _utc }
public var isDst: Bool { return buffer.tm_isdst == 1 }
public var yearday: Int32 { return buffer.tm_yday }
// Low level. C-based. Weekdays start from 0 with Sunday.
public var weekday: Int32 { return buffer.tm_wday }
// Low level. C-based. Years start from 1900.
// Current year would have a value of current_year - 1900.
// E.g. 2016 - 1900 // Which would be 116.
public var year: Int32 { return buffer.tm_year }
// Low level. C-based. Months go from 0 to 11 here.
public var month: Int32 { return buffer.tm_mon }
public var day: Int32 { return buffer.tm_mday }
public var hour: Int32 { return buffer.tm_hour }
public var minute: Int32 { return buffer.tm_min }
public var second: Int32 { return buffer.tm_sec }
public var secondsSinceEpoch: Int {
var r = Int(second)
r += Int(minute) * 60
r += Int(hour) * 3600
r += Int(yearday) * 86400
let y = Int(year)
r += (y - 70) * 31536000
r += ((y - 69) / 4) * 86400
return r
}
public var description: String {
return "TimeBuffer(year: \(year), month: \(month), day: \(day), " +
"hour: \(hour), minute: \(minute), second: \(second), " +
"weekday: \(weekday), yearday: \(yearday), isDst: \(isDst), " +
"isUtc: \(_utc))"
}
static func utc() -> TimeBuffer {
var b = Sys.timeBuffer()
Sys.gmtime_r(secondsSinceEpoch: Sys.time(), buffer: &b)
return TimeBuffer(buffer: b, utc: true)
}
static func utc(secondsSinceEpoch: Int) -> TimeBuffer {
var b = Sys.timeBuffer()
Sys.gmtime_r(secondsSinceEpoch: secondsSinceEpoch, buffer: &b)
return TimeBuffer(buffer: b, utc: true)
}
}
// Time is by default set on local time. It also supports time in UTC format
// and can convert between the two of them with methods such as toUtc() and
// toLocalTime().
//
// Time math is supported on the second, minute, hour and day properties and
// a new time buffer value will be computed to present the changes with.
//
// Time can also be compared with the comparison operators: < == > !=
//
// An output format can be obtained by using the strftime function similar to C.
// E.g.
// var t = Time() // Creates a new Time object set on local time.
// var u = Time.utc() // Creates a new Time object set on UTC time.
// print(Time().strftime("%Y-%m-%d %H:%M:%S")) //> 2015-12-29 05:28:20
// print(Time() == Time()) // Prints true
// print(Time().secondsSinceEpoch) // Prints 1451377821
// var k = Time(year: 2016, month: 12, day: 30) // Creates a new Time object.
// // As does this:
// Time.utc(year: 2016, month: 12, day: 30, hour: 5, minute: 3, second: 1)
// var r = Time()
// r.minute -= 30 // Goes back in time half an hour.
// r.day += 7 // Goes forward a week.
public struct Time: CustomStringConvertible {
var _buffer: TimeBuffer
var _secondsSinceEpoch = 0
// This property is still under consideration. It may be useful when using
// the strftime command to include milliseconds, which nanoseconds are then
// converted to. It may also help to store all the time data that is
// available under Linux.
public var nanoseconds = 0
public init() {
self.init(secondsSinceEpoch: Sys.time())
}
// Assume secondsSinceEpoch is coming from UTC.
public init(secondsSinceEpoch: Int, nanoseconds: Int = 0) {
_secondsSinceEpoch = secondsSinceEpoch
self.nanoseconds = nanoseconds
_buffer = TimeBuffer(secondsSinceEpoch: secondsSinceEpoch)
}
public init(buffer: CTime) {
_buffer = TimeBuffer(buffer: buffer)
_secondsSinceEpoch = _buffer.secondsSinceEpoch
}
public init(buffer: TimeBuffer) {
_buffer = buffer
_secondsSinceEpoch = buffer.secondsSinceEpoch
}
public init(year: Int, month: Int, day: Int, hour: Int = 0, minute: Int = 0,
second: Int = 0) {
self.init(secondsSinceEpoch: Time.convertToSecondsSinceEpoch(year: year,
month: month, day: day, hour: hour, minute: minute, second: second) -
Time.findLocalTimeDifference())
}
public var isUtc: Bool { return _buffer.isUtc }
public var isDst: Bool { return _buffer.isDst }
public var yearday: Int { return Int(_buffer.yearday) }
// Make it range from Monday to Sunday and from 1 to 7.
public var weekday: Int {
let n = Int(_buffer.weekday)
return n == 0 ? 7 : n
}
public var year: Int { return 1900 + Int(_buffer.year) }
public var month: Int { return 1 + Int(_buffer.month) }
public var day: Int {
get { return Int(_buffer.day) }
set {
secondsSinceEpoch += (newValue - Int(_buffer.day)) * 86400
}
}
public var hour: Int {
get { return Int(_buffer.hour) }
set {
secondsSinceEpoch += (newValue - Int(_buffer.hour)) * 3600
}
}
public var minute: Int {
get { return Int(_buffer.minute) }
set {
secondsSinceEpoch += (newValue - Int(_buffer.minute)) * 60
}
}
public var second: Int {
get { return Int(_buffer.second) }
set {
secondsSinceEpoch += newValue - Int(_buffer.second)
}
}
public var secondsSinceEpoch: Int {
get { return _secondsSinceEpoch }
set {
_secondsSinceEpoch = newValue
_buffer = TimeBuffer(secondsSinceEpoch: newValue)
}
}
// Returns a new instance of Time.
// If self is set on UTC already, just copy it over. Otherwise, convert it
// from local time to UTC.
public func toUtc() -> Time {
if isUtc {
return self
} else {
return Time.utc(secondsSinceEpoch: secondsSinceEpoch,
nanoseconds: nanoseconds)
}
}
// Returns a new instance of Time.
// If self is set on local time already, just copy it over. Otherwise,
// convert it from UTC to local time.
public func toLocalTime() -> Time {
if !isUtc {
return self
} else {
return Time(secondsSinceEpoch: secondsSinceEpoch,
nanoseconds: nanoseconds)
}
}
public func strftime(mask: String) -> String {
return Time.locale.strftime(time: self, mask: mask)
}
public var description: String {
return "Time(year: \(year), month: \(month), day: \(day), " +
"hour: \(hour), minute: \(minute), second: \(second), " +
"nanoseconds: \(nanoseconds), weekday: \(weekday), " +
"yearday: \(yearday), isDst: \(isDst), isUtc: \(isUtc))"
}
static public func isLeapYear(year: Int) -> Bool {
if year % 4 != 0 {
return false
} else if year % 100 != 0 {
return true
} else if year % 400 != 0 {
return false
} else {
return true
}
}
static public func countYeardays(year: Int, month: Int, day: Int) -> Int {
var r = day - 1
switch month {
case 12: // Dec
fallthrough
case 11: // Nov
r += 30
fallthrough
case 10: // Oct
r += 31
fallthrough
case 9: // Sep
r += 30
fallthrough
case 8: // Aug
r += 31
fallthrough
case 7: // Jul
r += 31
fallthrough
case 6: // Jun
r += 30
fallthrough
case 5: // May
r += 31
fallthrough
case 4: // Apr
r += 30
fallthrough
case 3: // Mar
r += 31
fallthrough
case 2: // Feb
r += isLeapYear(year: year) ? 29 : 28
fallthrough
case 1: // Jan
r += 31
fallthrough
default:
() // Ignore.
}
return r;
}
static public func convertToSecondsSinceEpoch(year: Int, month: Int,
day: Int, hour: Int, minute: Int, second: Int) -> Int {
var r = second
r += minute * 60
r += hour * 3600
r += countYeardays(year: year, month: month, day: day) * 86400
let y = year - 1900
r += (y - 70) * 31536000
r += ((y - 69) / 4) * 86400
return r
}
static public func findLocalTimeDifference() -> Int {
let n = TimeBuffer().secondsSinceEpoch
let tbUtc = TimeBuffer.utc()
var nu = tbUtc.secondsSinceEpoch
// TODO Joao: confirm that this is needed. The idea is from this SO thread:
// http://stackoverflow.com/questions/9076494/how-to-convert-from-utc-to-local-time-in-c
if tbUtc.isDst {
nu -= 3600
}
return n - nu
}
static public func utc() -> Time {
return Time(buffer: TimeBuffer.utc())
}
static public func utc(year: Int, month: Int, day: Int, hour: Int = 0,
minute: Int = 0, second: Int = 0) -> Time {
return Time(secondsSinceEpoch: convertToSecondsSinceEpoch(year: year,
month: month, day: day, hour: hour, minute: minute, second: second) -
findLocalTimeDifference())
}
static public func utc(secondsSinceEpoch secs: Int, nanoseconds: Int = 0)
-> Time {
var t = Time(buffer: TimeBuffer.utc(secondsSinceEpoch: secs))
t.nanoseconds = nanoseconds
return t
}
static public var locale = Locale.one
}
public func <(lhs: Time, rhs: Time) -> Bool {
return lhs.secondsSinceEpoch < rhs.secondsSinceEpoch ||
(lhs.secondsSinceEpoch == rhs.secondsSinceEpoch &&
lhs.nanoseconds < rhs.nanoseconds)
}
public func ==(lhs: Time, rhs: Time) -> Bool {
return lhs.secondsSinceEpoch == rhs.secondsSinceEpoch &&
lhs.nanoseconds == rhs.nanoseconds
}
public func !=(lhs: Time, rhs: Time) -> Bool {
return lhs.secondsSinceEpoch != rhs.secondsSinceEpoch ||
lhs.nanoseconds != rhs.nanoseconds
}
public func >(lhs: Time, rhs: Time) -> Bool {
return lhs.secondsSinceEpoch > rhs.secondsSinceEpoch ||
(lhs.secondsSinceEpoch == rhs.secondsSinceEpoch &&
lhs.nanoseconds > rhs.nanoseconds)
}
public func <=(lhs: Time, rhs: Time) -> Bool {
return lhs < rhs || lhs == rhs
}
public func >=(lhs: Time, rhs: Time) -> Bool {
return lhs > rhs || lhs == rhs
}