Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Brayden Kness
Brayden Kness
12,492 Points

Weather App Current Time

Is there a way to change the currentTime property to match different timezones?

1 Answer

According to https://developer.forecast.io/docs/v2#forecast_call the API will return the current time at the location for which you requested the weather information. I have changed the coordinates to my position and it does correctly return the local time.

Under normal circumstances, the user is in the same timezone as the location for which he is requesting the current weather. If this is not the case and you know the difference in hours between the users current timezone and the timezone of the weather location, you could just add/subtract it before formatting the time string, e.g.

 func dateStringFromUnixtime(unixTime: Int) -> String {
       // adding one hour to the local time because of different timezone
        let localTime = unixTime + 3600
        let timeInSeconds = NSTimeInterval(localTime)
        let weatherDate = NSDate(timeIntervalSince1970: timeInSeconds)

I guess you could also get the local time zone of the user programmatically, compare it to the "timezone" element returned by the forecast.io API and then calculate the time difference. NSTimezone looks like a good starting point (see docs), but I haven't tried this myself so unfortunately cannot give you a solution out of the box for this approach

Brayden Kness
Brayden Kness
12,492 Points

Ok I asked because when I tried using different locations in different timezones the time was the same in each one