diff --git a/AFDateHelper.podspec b/AFDateHelper.podspec index bad309f..1d697e6 100644 --- a/AFDateHelper.podspec +++ b/AFDateHelper.podspec @@ -9,7 +9,7 @@ Pod::Spec.new do |s| s.name = "AFDateHelper" - s.version = "3.1.1" + s.version = "3.1.2" s.summary = "NSDate Extension for Swift 2.0" 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 4bb286a..4b67325 100644 --- a/AFDateHelper/AFDateExtension.swift +++ b/AFDateHelper/AFDateExtension.swift @@ -45,6 +45,10 @@ public enum DateFormat { case ISO8601(ISO8601Format?), DotNet, RSS, AltRSS, Custom(String) } +public enum TimeZone { + case Local, UTC +} + public extension NSDate { // MARK: Intervals In Seconds @@ -72,11 +76,12 @@ public extension NSDate { - Parameter fromString Date string i.e. "16 July 1972 6:12:00". - Parameter format The Date Formatter type can be .ISO8601(ISO8601Format?), .DotNet, .RSS, .AltRSS or Custom(String). + - Parameter timeZone: The time zone to interpret fromString can be .Local, .UTC applies to Custom format only - Returns A new date */ - convenience init(fromString string: String, format:DateFormat) + convenience init(fromString string: String, format:DateFormat, timeZone: TimeZone = .Local) { if string.isEmpty { self.init() @@ -85,6 +90,15 @@ public extension NSDate { let string = string as NSString + let zone: NSTimeZone + + switch timeZone { + case .Local: + zone = NSTimeZone.localTimeZone() + case .UTC: + zone = NSTimeZone(forSecondsFromGMT: 0) + } + switch format { case .DotNet: @@ -137,7 +151,7 @@ public extension NSDate { case .Custom(let dateFormat): - let formatter = NSDate.formatter(format: dateFormat) + let formatter = NSDate.formatter(format: dateFormat, timeZone: zone) if let date = formatter.dateFromString(string as String) { self.init(timeInterval:0, sinceDate:date) } else { @@ -730,11 +744,13 @@ public extension NSDate { A string representation based on a format. - Parameter format: The format of date can be .ISO8601(.ISO8601Format?), .DotNet, .RSS, .AltRSS or Custom(FormatString). + - Parameter timeZone: The time zone to interpret the date can be .Local, .UTC applies to Custom format only - Returns The date string representation */ - func toString(format format: DateFormat) -> String + func toString(format format: DateFormat, timeZone: TimeZone = .Local) -> String { var dateFormat: String + let zone: NSTimeZone switch format { case .DotNet: let offset = NSTimeZone.defaultTimeZone().secondsFromGMT / 3600 @@ -742,14 +758,24 @@ public extension NSDate { return "/Date(\(nowMillis)\(offset))/" case .ISO8601(let isoFormat): dateFormat = (isoFormat != nil) ? isoFormat!.rawValue : ISO8601Format.DateTimeMilliSec.rawValue + zone = NSTimeZone.localTimeZone() case .RSS: dateFormat = RSSFormat + zone = NSTimeZone.localTimeZone() case .AltRSS: dateFormat = AltRSSFormat + zone = NSTimeZone.localTimeZone() case .Custom(let string): + switch timeZone { + case .Local: + zone = NSTimeZone.localTimeZone() + case .UTC: + zone = NSTimeZone(forSecondsFromGMT: 0) + } dateFormat = string } - let formatter = NSDate.formatter(format: dateFormat) + + let formatter = NSDate.formatter(format: dateFormat, timeZone: zone) return formatter.stringFromDate(self) }