├── .gitignore ├── DateIntervalOperators.podspec ├── DateIntervalOperators.swift ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | -------------------------------------------------------------------------------- /DateIntervalOperators.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = 'DateIntervalOperators' 3 | s.version = '1.0.0' 4 | s.license = 'MIT' 5 | s.summary = 'A set of extensions and operators for manipulating dates in Swift' 6 | s.homepage = 'https://github.com/tschmitz/DateIntervalOperators' 7 | s.authors = { 'Tim Schmitz' => 'tim@tapandtonic.net' } 8 | s.source = { :git => 'https://github.com/tschmitz/DateIntervalOperators.git', :tag => "1.0.0" } 9 | 10 | s.requires_arc = true 11 | s.ios.deployment_target = '8.0' 12 | s.osx.deployment_target = '10.9' 13 | 14 | s.source_files = '*.swift' 15 | end -------------------------------------------------------------------------------- /DateIntervalOperators.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DateIntervalOperators.swift 3 | // SyncTime 4 | // 5 | // Created by Tim Schmitz on 2/2/15. 6 | // Copyright (c) 2015 Tap and Tonic. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | public extension Int { 12 | public func day() -> NSDateComponents { 13 | let components = NSDateComponents() 14 | components.day = self 15 | return components 16 | } 17 | public func days() -> NSDateComponents { return self.day() } 18 | 19 | public func week() -> NSDateComponents { 20 | let components = NSDateComponents() 21 | components.weekOfYear = self 22 | return components 23 | } 24 | public func weeks() -> NSDateComponents { return self.week() } 25 | 26 | public func month() -> NSDateComponents { 27 | let components = NSDateComponents() 28 | components.month = self 29 | return components 30 | } 31 | public func months() -> NSDateComponents { return self.month() } 32 | 33 | public func year() -> NSDateComponents { 34 | let components = NSDateComponents() 35 | components.year = self 36 | return components 37 | } 38 | public func years() -> NSDateComponents { return self.year() } 39 | 40 | public func second() -> NSDateComponents { 41 | let components = NSDateComponents() 42 | components.second = self 43 | return components 44 | } 45 | public func seconds() -> NSDateComponents { return self.second() } 46 | 47 | public func minute() -> NSDateComponents { 48 | let components = NSDateComponents() 49 | components.minute = self 50 | return components 51 | } 52 | public func minutes() -> NSDateComponents { return self.minute() } 53 | 54 | public func hour() -> NSDateComponents { 55 | let components = NSDateComponents() 56 | components.hour = self 57 | return components 58 | } 59 | public func hours() -> NSDateComponents { return self.hour() } 60 | } 61 | 62 | public func +(lhs: NSDate, rhs: NSDateComponents) -> NSDate { 63 | return NSCalendar.currentCalendar().dateByAddingComponents(rhs, toDate: lhs, options: nil) ?? lhs 64 | } 65 | 66 | public func -(lhs: NSDate, rhs: NSDateComponents) -> NSDate { 67 | if rhs.era != Int(NSUndefinedDateComponent) { rhs.era *= -1 } 68 | if rhs.year != Int(NSUndefinedDateComponent) { rhs.year *= -1 } 69 | if rhs.month != Int(NSUndefinedDateComponent) { rhs.month *= -1 } 70 | if rhs.day != Int(NSUndefinedDateComponent) { rhs.day *= -1 } 71 | if rhs.hour != Int(NSUndefinedDateComponent) { rhs.hour *= -1 } 72 | if rhs.minute != Int(NSUndefinedDateComponent) { rhs.minute *= -1 } 73 | if rhs.second != Int(NSUndefinedDateComponent) { rhs.second *= -1 } 74 | if rhs.nanosecond != Int(NSUndefinedDateComponent) { rhs.nanosecond *= -1 } 75 | if rhs.weekday != Int(NSUndefinedDateComponent) { rhs.weekday *= -1 } 76 | if rhs.weekdayOrdinal != Int(NSUndefinedDateComponent) { rhs.weekdayOrdinal *= -1 } 77 | if rhs.quarter != Int(NSUndefinedDateComponent) { rhs.quarter *= -1 } 78 | if rhs.weekOfMonth != Int(NSUndefinedDateComponent) { rhs.weekOfMonth *= -1 } 79 | if rhs.weekOfYear != Int(NSUndefinedDateComponent) { rhs.weekOfYear *= -1 } 80 | if rhs.yearForWeekOfYear != Int(NSUndefinedDateComponent) { rhs.yearForWeekOfYear *= -1 } 81 | 82 | return NSCalendar.currentCalendar().dateByAddingComponents(rhs, toDate: lhs, options: nil) ?? lhs 83 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Tim Schmitz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DateIntervalOperators 2 | A set of extensions and operators for date arithmetic in Swift. 3 | 4 | ### Usage 5 | 6 | #### Date Components 7 | Easily can create instances of NSDateComponents initialized to various intervals. 8 | ```swift 9 | 1.year() 10 | // Equivalent to: 11 | // let components = NSDateComponents() 12 | // components.year = 1 13 | 14 | 5.years() 15 | 15.minutes() 16 | 1.month() 17 | ``` 18 | ####Supported intervals: 19 | ```swift 20 | second(s) 21 | minute(s) 22 | hour(s) 23 | day(s) 24 | week(s) 25 | month(s) 26 | year(s) 27 | ``` 28 | 29 | #### Date Arithmetic 30 | Add or subtract date intervals from instances of NSDate. 31 | ```swift 32 | let today = NSDate() 33 | today + 1.year() // 1 year from now 34 | today - 2 month() // 2 months ago 35 | today + 5.minutes() // 5 minutes from now 36 | ``` 37 | 38 | ### Installation 39 | 40 | #### CocoaPods 41 | 42 | You can also install this library using CocoaPods. Just add this line to your Podfile: 43 | 44 | ```ruby 45 | pod 'DateIntervalOperators' 46 | ``` 47 | 48 | Then import library module like so: 49 | 50 | ```swift 51 | import DateIntervalOperators 52 | ``` 53 | 54 | #### Manual 55 | 56 | You can also install this library manually by copying `DateIntervalOperators.swift` to your project. 57 | 58 | ### Contributing 59 | If you have comments, complaints or ideas for improvements, feel free to open an issue or a pull request. 60 | 61 | ### Author and license 62 | 63 | Tim Schmitz 64 | 65 | - [Github](http://github.com/timschmitz) 66 | - [Twitter](http://twitter.com/timschmitz) 67 | - [minutestomidnight.net](http://minutestomidnight.net) 68 | 69 | DateIntervalOperators is available under the MIT license. See the LICENSE file for more info. --------------------------------------------------------------------------------