Skip to content

Commit

Permalink
Merge pull request #26 from abuobaida/master
Browse files Browse the repository at this point in the history
Added support for time zone for custom date format
  • Loading branch information
melvitax committed Feb 25, 2016
2 parents 78c6e8b + 42ee04c commit 16a27e6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion AFDateHelper.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 30 additions & 4 deletions AFDateHelper/AFDateExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -730,26 +744,38 @@ 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
let nowMillis = 1000 * self.timeIntervalSince1970
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)
}

Expand Down

0 comments on commit 16a27e6

Please sign in to comment.