-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDatify.swift
executable file
·118 lines (103 loc) · 4.07 KB
/
Datify.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
// Originaly Written By:
// timeDateExtensions.swift [https://github.com/schluete/SwiftDateTimeExtensions]
// Created by Axel Schlueter on 4.6.14.
//
// The MIT License (MIT)
// Copyright (c) 2014 Axel Schlueter
// Renamed to Datify [https://github.com/hemangshah/Datify]
// Updated for Swift 3.x on 6th.Jun.17.
// Hemang Shah
// The MIT License (MIT)
// Copyright (c) 2017 Hemang Shah
import Foundation
extension NSDateComponents {
/** returns the current date plus the receiver's interval */
var fromNow: Date {
let cal = NSCalendar.current
return cal.date(byAdding: (self as DateComponents) as DateComponents, to: Date.init() as Date)!
}
/** returns the current date minus the receiver's interval */
var ago: Date {
let cal = NSCalendar.current
return cal.date(byAdding: -self as DateComponents, to: Date.init())!
}
}
/** helper method to DRY the code a little, adds or subtracts two sets of date components */
func combineComponents(left: NSDateComponents,
right: NSDateComponents,
multiplier: Int) -> NSDateComponents
{
let comps = NSDateComponents()
comps.second = ((left.second != NSDateComponentUndefined ? left.second : 0) +
(right.second != NSDateComponentUndefined ? right.second : 0) * multiplier)
comps.minute = ((left.minute != NSDateComponentUndefined ? left.minute : 0) +
(right.minute != NSDateComponentUndefined ? right.minute : 0) * multiplier)
comps.hour = ((left.hour != NSDateComponentUndefined ? left.hour : 0) +
(right.hour != NSDateComponentUndefined ? right.hour : 0) * multiplier)
comps.day = ((left.day != NSDateComponentUndefined ? left.day : 0) +
(right.day != NSDateComponentUndefined ? right.day : 0) * multiplier)
comps.month = ((left.month != NSDateComponentUndefined ? left.month : 0) +
(right.month != NSDateComponentUndefined ? right.month : 0) * multiplier)
comps.year = ((left.year != NSDateComponentUndefined ? left.year : 0) +
(right.year != NSDateComponentUndefined ? right.year : 0) * multiplier)
return comps
}
/** adds the two sets of date components */
func +(left: NSDateComponents, right: NSDateComponents) -> NSDateComponents {
return combineComponents(left: left, right: right, multiplier: 1)
}
/** subtracts the two sets of date components */
func -(left: NSDateComponents, right: NSDateComponents) -> NSDateComponents {
return combineComponents(left: left, right: right, multiplier: -1)
}
/** negates the two sets of date components */
prefix func -(comps: NSDateComponents) -> NSDateComponents {
let result = NSDateComponents()
if(comps.second != NSDateComponentUndefined) { result.second = -comps.second }
if(comps.minute != NSDateComponentUndefined) { result.minute = -comps.minute }
if(comps.hour != NSDateComponentUndefined) { result.hour = -comps.hour }
if(comps.day != NSDateComponentUndefined) { result.day = -comps.day }
if(comps.month != NSDateComponentUndefined) { result.month = -comps.month }
if(comps.year != NSDateComponentUndefined) { result.year = -comps.year }
return result
}
/** functions to convert integers into various time intervals */
extension Int {
var minutes: NSDateComponents {
let comps = NSDateComponents()
comps.minute = self;
return comps
}
var hours: NSDateComponents {
let comps = NSDateComponents()
comps.hour = self;
return comps
}
var days: NSDateComponents {
let comps = NSDateComponents()
comps.day = self;
return comps
}
var weeks: NSDateComponents {
let comps = NSDateComponents()
comps.day = 7 * self;
return comps
}
var months: NSDateComponents {
let comps = NSDateComponents()
comps.month = self;
return comps
}
var years: NSDateComponents {
let comps = NSDateComponents()
comps.year = self;
return comps
}
}
//Mark: Usage Example
func testDatify() -> Void {
print("now: \(Date.init())")
print("8 days later: \(8.days.fromNow)")
print("2 weeks before: \(2.weeks.ago)")
print("5 days, 3 month later: \((5.days + 3.months).fromNow)")
}