Swift time zones converting issue

In my playground i create a simple code example to convert time from my local zone to "America/Sao_Paulo".

let gmtDf: NSDateFormatter = NSDateFormatter()
gmtDf.timeZone = NSTimeZone.systemTimeZone();
gmtDf.dateFormat = "HH:mm:ss"
let gmtDate: NSDate = NSDate.new()

print(gmtDate)

let estDf: NSDateFormatter = NSDateFormatter()
estDf.timeZone = NSTimeZone(name: "America/Sao_Paulo")
estDf.dateFormat = "HH:mm:ss"
let estDate: NSDate = estDf.dateFromString(gmtDf.stringFromDate(gmtDate))!

print(estDate)

All code is working fine, but i have a question that is connected with code output.Why printed output show time 2 hours before my local time. And converted time is in 2000 year.

Answers


  1. NSTimeZone.systemTimeZone will return GMT time if it cannot determine the system time zone, I'm guessing that's what's happening here since you're using a Playground.

  2. You're only using the hour, minutes and seconds to convert from date to string dateFormat = "HH:mm:ss" so that String doesn't have any information about the year, that's why when you're using that to convert back to a NSDate it's showing 2000, since it doesn't have the proper info to know what year it is.


Need Your Help

A Question about using jython when run a receving socket in python

python sockets jython nonblocking

I have not a lot of knowledge of python and network programming. Currently I am trying to implement a simple application which can receive a text message sent by the user, fetch some information fr...

Adding XML Files to the Build

c# visual-studio

I'm using Visual Studio C# Express and I'm wondering how I would go about adding some XML files and being able to reference them in my code. I added the XML files in a folder under the project, bu...