From 15daf87d143c927ec0c118504afb499e69abd84e Mon Sep 17 00:00:00 2001 From: Melvin Rivera Date: Tue, 7 Jul 2015 16:25:45 -0400 Subject: [PATCH] Fixed daylight savings issues --- AFDateHelper.podspec | 2 +- AFDateHelper/AFDateExtension.swift | 32 ++++++++++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/AFDateHelper.podspec b/AFDateHelper.podspec index c67c655..92dbb2c 100644 --- a/AFDateHelper.podspec +++ b/AFDateHelper.podspec @@ -9,7 +9,7 @@ Pod::Spec.new do |s| s.name = "AFDateHelper" - s.version = "2.0.0" + s.version = "2.0.1" s.summary = "Extension for NSDate in Swift." s.description = <<-DESC Extension for NSDate in Swift for creating, modifying or comparing dates. diff --git a/AFDateHelper/AFDateExtension.swift b/AFDateHelper/AFDateExtension.swift index 26f7b11..8d44d62 100644 --- a/AFDateHelper/AFDateExtension.swift +++ b/AFDateHelper/AFDateExtension.swift @@ -1,7 +1,7 @@ // // AFDateExtension.swift // -// Version 2.0.0 +// Version 2.0.1 // // Created by Melvin Rivera on 7/15/14. // Copyright (c) 2014. All rights reserved. @@ -236,38 +236,44 @@ extension NSDate { func dateByAddingDays(days: Int) -> NSDate { - let interval: NSTimeInterval = self.timeIntervalSinceReferenceDate + NSDate.dayInSeconds() * Double(days) - return NSDate(timeIntervalSinceReferenceDate: interval) + let dateComp = NSDateComponents() + dateComp.day = days + return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(0))! } func dateBySubtractingDays(days: Int) -> NSDate { - let interval: NSTimeInterval = self.timeIntervalSinceReferenceDate - NSDate.dayInSeconds() * Double(days) - return NSDate(timeIntervalSinceReferenceDate: interval) + let dateComp = NSDateComponents() + dateComp.day = (days * -1) + return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(0))! } func dateByAddingHours(hours: Int) -> NSDate { - let interval: NSTimeInterval = self.timeIntervalSinceReferenceDate + NSDate.hourInSeconds() * Double(hours) - return NSDate(timeIntervalSinceReferenceDate: interval) + let dateComp = NSDateComponents() + dateComp.hour = hours + return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(0))! } func dateBySubtractingHours(hours: Int) -> NSDate { - let interval: NSTimeInterval = self.timeIntervalSinceReferenceDate - NSDate.hourInSeconds() * Double(hours) - return NSDate(timeIntervalSinceReferenceDate: interval) + let dateComp = NSDateComponents() + dateComp.hour = (hours * -1) + return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(0))! } func dateByAddingMinutes(minutes: Int) -> NSDate { - let interval: NSTimeInterval = self.timeIntervalSinceReferenceDate + NSDate.minuteInSeconds() * Double(minutes) - return NSDate(timeIntervalSinceReferenceDate: interval) + let dateComp = NSDateComponents() + dateComp.minute = minutes + return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(0))! } func dateBySubtractingMinutes(minutes: Int) -> NSDate { - let interval: NSTimeInterval = self.timeIntervalSinceReferenceDate - NSDate.minuteInSeconds() * Double(minutes) - return NSDate(timeIntervalSinceReferenceDate: interval) + let dateComp = NSDateComponents() + dateComp.minute = (minutes * -1) + return NSCalendar.currentCalendar().dateByAddingComponents(dateComp, toDate: self, options: NSCalendarOptions(0))! } func dateAtStartOfDay() -> NSDate