forked from mozilla-mobile/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeConstants.swift
90 lines (69 loc) · 3.63 KB
/
TimeConstants.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
public typealias Timestamp = UInt64
public typealias MicrosecondTimestamp = UInt64
public let ThreeWeeksInSeconds = 3 * 7 * 24 * 60 * 60
public let OneMonthInMilliseconds = 30 * OneDayInMilliseconds
public let OneWeekInMilliseconds = 7 * OneDayInMilliseconds
public let OneDayInMilliseconds = 24 * OneHourInMilliseconds
public let OneHourInMilliseconds = 60 * OneMinuteInMilliseconds
public let OneMinuteInMilliseconds: UInt64 = 60 * 1000
extension NSDate {
public class func now() -> Timestamp {
return UInt64(1000 * NSDate().timeIntervalSince1970)
}
public class func nowNumber() -> NSNumber {
return NSNumber(unsignedLongLong: now())
}
public class func nowMicroseconds() -> MicrosecondTimestamp {
return UInt64(1000000 * NSDate().timeIntervalSince1970)
}
public class func fromTimestamp(timestamp: Timestamp) -> NSDate {
return NSDate(timeIntervalSince1970: Double(timestamp) / 1000)
}
public func toRelativeTimeString() -> String {
let now = NSDate()
let units: NSCalendarUnit = [NSCalendarUnit.Second, NSCalendarUnit.Minute, NSCalendarUnit.Day, NSCalendarUnit.WeekOfYear, NSCalendarUnit.Month, NSCalendarUnit.Year, NSCalendarUnit.Hour]
let components = NSCalendar.currentCalendar().components(units,
fromDate: self,
toDate: now,
options: [])
if components.year > 0 {
return String(format: NSDateFormatter.localizedStringFromDate(self, dateStyle: NSDateFormatterStyle.ShortStyle, timeStyle: NSDateFormatterStyle.ShortStyle))
}
if components.month == 1 {
return String(format: NSLocalizedString("more than a month ago", comment: "Relative date for dates older than a month and less than two months."))
}
if components.month > 1 {
return String(format: NSDateFormatter.localizedStringFromDate(self, dateStyle: NSDateFormatterStyle.ShortStyle, timeStyle: NSDateFormatterStyle.ShortStyle))
}
if components.weekOfYear > 0 {
return String(format: NSLocalizedString("more than a week ago", comment: "Description for a date more than a week ago, but less than a month ago."))
}
if components.day == 1 {
return String(format: NSLocalizedString("yesterday", comment: "Relative date for yesterday."))
}
if components.day > 1 {
return String(format: NSLocalizedString("this week", comment: "Relative date for date in past week."), String(components.day))
}
if components.hour > 0 || components.minute > 0 {
let absoluteTime = NSDateFormatter.localizedStringFromDate(self, dateStyle: NSDateFormatterStyle.NoStyle, timeStyle: NSDateFormatterStyle.ShortStyle)
let format = NSLocalizedString("today at %@", comment: "Relative date for date older than a minute.")
return String(format: format, absoluteTime)
}
return String(format: NSLocalizedString("just now", comment: "Relative time for a tab that was visited within the last few moments."))
}
}
public func decimalSecondsStringToTimestamp(input: String) -> Timestamp? {
var double = 0.0
if NSScanner(string: input).scanDouble(&double) {
return Timestamp(double * 1000)
}
return nil
}
public func millisecondsToDecimalSeconds(input: Timestamp) -> String {
let val: Double = Double(input) / 1000
return String(format: "%.2F", val)
}