-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathDate+ext.swift
More file actions
50 lines (38 loc) · 1.08 KB
/
Copy pathDate+ext.swift
File metadata and controls
50 lines (38 loc) · 1.08 KB
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
//
// Date+ext.swift
// Hummingbird
//
// Created by Sven A. Schmidt on 11/05/2019.
// Copyright © 2019 finestructure. All rights reserved.
//
import Foundation
enum DateMask {
case day
var dateComponents: Set<Calendar.Component> {
switch self {
case .day:
return [.year, .month, .day]
}
}
}
func truncate(date: Date, to mask: DateMask = .day) -> DateComponents {
return Calendar.current.dateComponents(mask.dateComponents, from: date)
}
extension Date {
static var now: Date { return Current.date() }
func truncated(to mask: DateMask = .day) -> DateComponents {
return truncate(date: self, to: mask)
}
}
extension Date: Defaultable {
static var defaultValue: Any {
return Current.date()
}
init?(forKey key: DefaultsKeys, defaults: UserDefaults) {
guard let value = defaults.object(forKey: key.rawValue) as? Date else { return nil }
self = value
}
func save(forKey key: DefaultsKeys, defaults: UserDefaults) throws {
defaults.set(self, forKey: key.rawValue)
}
}