forked from asuc-octo/berkeley-mobile-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateRange.swift
53 lines (45 loc) · 1.34 KB
/
DateRange.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
import Foundation
/**
* Specifies a range of time with start and end dates.
* The ending `Date` must be later than the starting one.
*/
struct DateRange
{
let start: Date
let end: Date
/**
* Initialize range from specified `start` and `end`.
* Returns `nil` if either are `nil` or `end` is earlier than `start`.
*/
init?(start: Date?, end: Date?)
{
guard start.notNil && end.notNil && start! < end! else {
return nil
}
self.start = start!
self.end = end!
}
/// Inidicates whether the current time is within range.
var isActive: Bool
{
return Date().isBetween(start, end)
}
/// Inidicates whether the current time has past the `end`.
var hasExpired: Bool
{
return end <= Date()
}
/// Indiciates whether the given `date` is within the range.
func contains(_ date: Date) -> Bool
{
return date.isBetween(start, end)
}
/**
* Returns description of format `"<start> <separator> <end>"`
* where `start` and `end` are converted using the given `DateFormatter`.
*/
func description(withFormatter formatter: DateFormatter, separator: String = "~") -> String
{
return formatter.string(from: start) + " \(separator) " + formatter.string(from: end)
}
}