I am having two problems. Any help with any of the issues will be really appreciated.
First - I am not getting the right NSDate from my device to start with but apparently there is nothing I can do about it.
Second - When I try to convert nsDateAsString to nsDateFromString it never matches.
One thing I noticed was that the daylight-savings is enabled. Will it have any influence on my NSDate? How do I manipulate that?
Code example
func NSDateToTimestamp(nsDate: NSDate) -> Double{
print("------------START-------------")
print("nsDate \(nsDate)")
let formater = NSDateFormatter()
formater.dateFormat = "yyyy-MM-dd HH:mm:ss"
// formater.timeZone = NSTimeZone.systemTimeZone()
// formater.timeZone = NSTimeZone.localTimeZone()
// formater.timeZone = NSTimeZone(forSecondsFromGMT: 3600)
print ("DLS offset \(formater.timeZone.daylightSavingTimeOffset)")
print ("DLS on/off \(formater.timeZone.daylightSavingTime)")
print("-------------------------------")
// string from date
let nsDateAsString = formater.stringFromDate(nsDate)
print("nsDateAsString \(nsDateAsString)")
// get format back to nsdate
let nsDateFromString = formater.dateFromString(nsDateAsString)
print("nsDateFromString \(nsDateFromString!)")
// convert nsdate to timestamp
let nsDateConvertedToTimestamp = nsDateFromString!.timeIntervalSince1970
print("nsDateConvertedToTimestamp \(nsDateConvertedToTimestamp)")
// convert timestamp to NSDate
let timestampConvertedBackToNSDate = NSDate(timeIntervalSince1970: nsDateConvertedToTimestamp)
print("timestampConvertedBackToNSDate \(timestampConvertedBackToNSDate)")
return nsDateConvertedToTimestamp
}
My output ALL DISABLED while it is 4 Feb - 8:11 AM on my phone.
------------START-------------
nsDate 2016-02-03 19:33:22 +0000
DLS offset 3600.0
DLS on/off true
-------------------------------
nsDateAsString 2016-02-04 08:33:22
nsDateFromString 2016-02-03 19:33:22 +0000
nsDateConvertedToTimestamp 1454528002.0
timestampConvertedBackToNSDate 2016-02-03 19:33:22 +0000
My output NSTimeZone.localTimeZone() ENABLED while it is 4 Feb - 8:32 AM on my phone.
------------START-------------
nsDate 2016-02-03 19:32:07 +0000
DLS offset 3600.0
DLS on/off true
-------------------------------
nsDateAsString 2016-02-04 08:32:07
nsDateFromString 2016-02-03 19:32:07 +0000
nsDateConvertedToTimestamp 1454527927.0
timestampConvertedBackToNSDate 2016-02-03 19:32:07 +0000
My output NSTimeZone.systemTimeZone() ENABLED while it is 4 Feb - 8:32 AM on my phone.
------------START-------------
nsDate 2016-02-03 19:32:42 +0000
DLS offset 3600.0
DLS on/off true
-------------------------------
nsDateAsString 2016-02-04 08:32:42
nsDateFromString 2016-02-03 19:32:42 +0000
nsDateConvertedToTimestamp 1454527962.0
timestampConvertedBackToNSDate 2016-02-03 19:32:42 +0000
My output NSTimeZone(forSecondsFromGMT: 3600) ENABLED while it is 4 Feb - 8:31 AM on my phone.
------------START-------------
nsDate 2016-02-03 19:31:16 +0000
DLS offset 0.0
DLS on/off false
-------------------------------
nsDateAsString 2016-02-03 20:31:16
nsDateFromString 2016-02-03 19:31:16 +0000
nsDateConvertedToTimestamp 1454527876.0
timestampConvertedBackToNSDate 2016-02-03 19:31:16 +0000
My output NSTimeZone(forSecondsFromGMT: 0) ENABLED while it is 4 Feb - 8:30 AM on my phone.
------------START-------------
nsDate 2016-02-03 19:30:09 +0000
DLS offset 0.0
DLS on/off false
-------------------------------
nsDateAsString 2016-02-03 19:30:09
nsDateFromString 2016-02-03 19:30:09 +0000
nsDateConvertedToTimestamp 1454527809.0
timestampConvertedBackToNSDate 2016-02-03 19:30:09 +0000