Skip to content

Commit b460f5c

Browse files
author
wgwebster
committed
- Moved manipulation methods in Comparators to new Manipulations file
- Wrote "start of" methods - Added component enums for use with start of methods - Added component mutations in Components file
1 parent 272bcb1 commit b460f5c

File tree

5 files changed

+128
-45
lines changed

5 files changed

+128
-45
lines changed

Diff for: DateTools/Date+Comparators.swift

-45
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,6 @@ import Foundation
1010

1111
public extension Date {
1212

13-
// MARK: - Addition / Subtractions
14-
15-
func add(_ timeChunk: TimeChunk) -> Date {
16-
let calendar = Calendar.autoupdatingCurrent
17-
var components = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self)
18-
components.year! += timeChunk.years
19-
components.month! += timeChunk.months
20-
components.day! += timeChunk.days
21-
components.hour! += timeChunk.hours
22-
components.minute! += timeChunk.minutes
23-
components.second! += timeChunk.seconds
24-
return calendar.date(from: components)!
25-
}
26-
27-
func subtract(_ timeChunk: TimeChunk) -> Date {
28-
let calendar = Calendar.autoupdatingCurrent
29-
var components = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self)
30-
components.year! -= timeChunk.years
31-
components.month! -= timeChunk.months
32-
components.day! -= timeChunk.days
33-
components.hour! -= timeChunk.hours
34-
components.minute! -= timeChunk.minutes
35-
components.second! -= timeChunk.seconds
36-
return calendar.date(from: components)!
37-
}
38-
39-
4013
// MARK: - Chunk between
4114

4215
func chunkBetween(date: Date) -> TimeChunk {
@@ -253,22 +226,4 @@ public extension Date {
253226
return self.compare(date) == .orderedAscending
254227
}
255228

256-
257-
// MARK: - Operator Overloads
258-
259-
static func +(leftAddend: Date, rightAddend: TimeChunk) -> Date {
260-
return leftAddend.add(rightAddend)
261-
}
262-
263-
static func -(minuend: Date, subtrahend: TimeChunk) -> Date {
264-
return minuend.subtract(subtrahend)
265-
}
266-
267-
static func +(leftAddend: Date, rightAddend: Int) -> Date {
268-
return leftAddend.addingTimeInterval((TimeInterval(rightAddend)))
269-
}
270-
271-
static func -(minuend: Date, subtrahend: Int) -> Date {
272-
return minuend.addingTimeInterval(-(TimeInterval(subtrahend)))
273-
}
274229
}

Diff for: DateTools/Date+Components.swift

+27
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,33 @@ public extension Date {
144144
}
145145

146146

147+
// MARK: - Set Components
148+
149+
mutating func year(_ year: Int) {
150+
self = Date.init(year: year, month: self.month, day: self.day, hour: self.hour, minute: self.minute, second: self.second)
151+
}
152+
153+
mutating func month(_ month: Int) {
154+
self = Date.init(year: self.year, month: month, day: self.day, hour: self.hour, minute: self.minute, second: self.second)
155+
}
156+
157+
mutating func day(_ day: Int) {
158+
self = Date.init(year: self.year, month: self.month, day: day, hour: self.hour, minute: self.minute, second: self.second)
159+
}
160+
161+
mutating func hour(_ hour: Int) {
162+
self = Date.init(year: self.year, month: self.month, day: self.day, hour: hour, minute: self.minute, second: self.second)
163+
}
164+
165+
mutating func minute(_ minute: Int) {
166+
self = Date.init(year: self.year, month: self.month, day: self.day, hour: self.hour, minute: minute, second: self.second)
167+
}
168+
169+
mutating func second(_ second: Int) {
170+
self = Date.init(year: self.year, month: self.month, day: self.day, hour: self.hour, minute: self.minute, second: second)
171+
}
172+
173+
147174
// MARK: - Bools
148175

149176
var isInLeapYear: Bool {

Diff for: DateTools/Enums.swift

+9
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,12 @@ public enum Anchor {
5151
case center
5252
case end
5353
}
54+
55+
public enum Component {
56+
case year
57+
case month
58+
case day
59+
case hour
60+
case minute
61+
case second
62+
}

Diff for: Tests/DateToolsTests/DateToolsTests.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
5645DF161D9C65CC004FE250 /* Date+Manipulations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5645DF151D9C65CC004FE250 /* Date+Manipulations.swift */; };
1011
5658E3801D6B559900D1465A /* TimePeriodTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5658E35B1D67782D00D1465A /* TimePeriodTests.swift */; };
1112
5658E3811D6B55C800D1465A /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 5658E37A1D6B556B00D1465A /* Info.plist */; };
1213
56890C711D771BA8004E8959 /* TimePeriodCollectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56890C701D771BA8004E8959 /* TimePeriodCollectionTests.swift */; };
@@ -54,6 +55,7 @@
5455
/* End PBXContainerItemProxy section */
5556

5657
/* Begin PBXFileReference section */
58+
5645DF151D9C65CC004FE250 /* Date+Manipulations.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Date+Manipulations.swift"; sourceTree = "<group>"; };
5759
5658E35B1D67782D00D1465A /* TimePeriodTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimePeriodTests.swift; sourceTree = "<group>"; };
5860
5658E3701D6B53B000D1465A /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Platforms/iPhoneOS.platform/Developer/Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
5961
5658E3761D6B556B00D1465A /* DateToolsTestsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DateToolsTestsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -139,6 +141,7 @@
139141
children = (
140142
F0885D091D708AD0002843B5 /* Date+Components.swift */,
141143
F0885D031D708A07002843B5 /* Date+Comparators.swift */,
144+
5645DF151D9C65CC004FE250 /* Date+Manipulations.swift */,
142145
56D193961D675ADE001BD246 /* Date+Inits.swift */,
143146
F0E41E851D6CE82B00DF0AAB /* Date+Format.swift */,
144147
F0E41E891D6CE89400DF0AAB /* Date+TimeAgo.swift */,
@@ -341,6 +344,7 @@
341344
files = (
342345
56D193A21D675ADE001BD246 /* Integer+DateTools.swift in Sources */,
343346
56D193A61D675ADE001BD246 /* TimePeriodGroup.swift in Sources */,
347+
5645DF161D9C65CC004FE250 /* Date+Manipulations.swift in Sources */,
344348
F0E41E861D6CE82B00DF0AAB /* Date+Format.swift in Sources */,
345349
56D193BD1D675CF2001BD246 /* TimeChunk.swift in Sources */,
346350
56D193A31D675ADE001BD246 /* TimePeriod.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// Date+Manipulations.swift
3+
// DateToolsTests
4+
//
5+
// Created by Grayson Webster on 9/28/16.
6+
// Copyright © 2016 Matthew York. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public extension Date {
12+
13+
// MARK: - StartOf
14+
15+
mutating func start(of component: Component) {
16+
if component == .second {
17+
self.second(self.second)
18+
}
19+
else if component == .minute {
20+
self.second(0)
21+
} else if component == .hour {
22+
self.second(0)
23+
self.minute(0)
24+
} else if component == .day {
25+
self.second(0)
26+
self.minute(0)
27+
self.hour(0)
28+
} else if component == .month {
29+
self.second(0)
30+
self.minute(0)
31+
self.hour(0)
32+
self.day(0)
33+
} else if component == .year {
34+
self.second(0)
35+
self.minute(0)
36+
self.hour(0)
37+
self.day(0)
38+
self.month(0)
39+
}
40+
}
41+
42+
43+
// MARK: - Addition / Subtractions
44+
45+
func add(_ timeChunk: TimeChunk) -> Date {
46+
let calendar = Calendar.autoupdatingCurrent
47+
var components = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self)
48+
components.year! += timeChunk.years
49+
components.month! += timeChunk.months
50+
components.day! += timeChunk.days
51+
components.hour! += timeChunk.hours
52+
components.minute! += timeChunk.minutes
53+
components.second! += timeChunk.seconds
54+
return calendar.date(from: components)!
55+
}
56+
57+
func subtract(_ timeChunk: TimeChunk) -> Date {
58+
let calendar = Calendar.autoupdatingCurrent
59+
var components = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self)
60+
components.year! -= timeChunk.years
61+
components.month! -= timeChunk.months
62+
components.day! -= timeChunk.days
63+
components.hour! -= timeChunk.hours
64+
components.minute! -= timeChunk.minutes
65+
components.second! -= timeChunk.seconds
66+
return calendar.date(from: components)!
67+
}
68+
69+
70+
// MARK: - Operator Overloads
71+
72+
static func +(leftAddend: Date, rightAddend: TimeChunk) -> Date {
73+
return leftAddend.add(rightAddend)
74+
}
75+
76+
static func -(minuend: Date, subtrahend: TimeChunk) -> Date {
77+
return minuend.subtract(subtrahend)
78+
}
79+
80+
static func +(leftAddend: Date, rightAddend: Int) -> Date {
81+
return leftAddend.addingTimeInterval((TimeInterval(rightAddend)))
82+
}
83+
84+
static func -(minuend: Date, subtrahend: Int) -> Date {
85+
return minuend.addingTimeInterval(-(TimeInterval(subtrahend)))
86+
}
87+
88+
}

0 commit comments

Comments
 (0)