0

I am making an app which among other things, it determines the sunset and sunrise. Nevertheless I've found some difficulties to get them using the user's local time. Therefore, I would like to know: how can I get the user's time zone in Swift?

The coordinates of my test location (Puerto Vallarta, Mex) that I load in the iPhone simulator are these:

Latitude: 20.61 Longitude: -106.23

The next is part of my code to determine the sunrise and the sunset of any location in the world:

//First we get the sunrise time.
let sunriseDate = Date(timeIntervalSince1970: sunriseValue.doubleValue)
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .medium
formatter.timeZone = NSTimeZone(name: "UTC")! as TimeZone
formatter.dateFormat = "HH:mm:ss"

let sunrise = formatter.string(from: sunriseDate)
print(sunrise)

//Second, we get the sunset time
let sunsetDate = Date(timeIntervalSince1970: sunsetValue.doubleValue)

let sunset = formatter.string(from: sunsetDate)
print(sunset)

And this is what I get from the console:

Sunrise: 13:08:12 Sunset: 00:21:22

This is more than evident that the printed sunrise and sunset correspond to the UTC, nevertheless I haven't found any direct information that could help me to get the user's local time, wherever that person is.

Basically the sunrise and sunset that you see in the code above I got it from an API (openweather) from which I got their values in unix time (which I converted to date and time using this link . Now I see that I effectively got the correct sunset and sunrise but in UTC format, now I'm missing one step to go from UTC to local time.

So, in summary, I got:

from API: time in unix time (e.g. sunrise = 1541855294; sunset = 1541895681;), extract from it the time using DateFormatter() (see code above) and I got the UTC time (formatter.dateFormat = "HH:mm:ss").

I hope that with this modification the question is more clear now.

That's why I wanted to ask you if you cand lend me a hand with this, please :)

Thanks in advance and I apologize for any misunderstanding!

  • 1
    Did you try *not setting* formatter.timeZone? Then the timezone from the user's settings should be used. – Martin R Nov 10 '18 at 17:25
  • BTW - If you ever do use a time zone, use `TimeZone`, not `NSTimeZone`. – rmaddy Nov 10 '18 at 17:42
  • Do you want to show the times in the user's local time or in the local time of the coordinate? For the latter you would need to know the timezone of the coordinate. – rmaddy Nov 10 '18 at 17:44
  • Hello rmaddy. I want to keep it simple so I just want to show the user's time (only the time HH:mm:ss) @M – J_A_F_Casillas Nov 10 '18 at 18:15
  • @JorgeAlbertoFuentesCasillas Then simply remove the line where you set the formatter's timeZone property and your are done, just like Martin stated in the 1st comment. – rmaddy Nov 10 '18 at 18:58
  • @rmaddy already done that but the sunrise and sunset still say 14:08:20 and 01:21:18 respectively (still in UTC format). Thanks for replying. – J_A_F_Casillas Nov 10 '18 at 22:15
  • If you print `sunriseDate` you will see 13:08:14 UTC time. But if you print `sunrise` you will get the user's local time assuming you don't set the date formatter's timeZone. I live in the UTC-5 timezone and when I run your code (with no time zone set), I see `06:18:14`. So the code you posted is fine if you don't set the time zone of the formatter. Don't be confused by the output of printing a `Date` instance. – rmaddy Nov 10 '18 at 22:24
  • @rmaddy thanks for replying. I did run in Playground the code w/o the timeZone (basically the code is the same but I deleted the line formatter.timeZone = NSTimeZone(name: "UTC")! as TimeZone and still get the same values as I stated before :/ I changed the city to Tokyo and now I get Sunrise date: 2018-11-10 21:12:16 +0000 Sunrise: 22:12:16 Sunset date: 2018-11-11 07:37:13 +0000 Sunset: 08:37:13 I repeat: the code is the same, except that I deleted formatter.timeZone = NSTimeZone(name: "UTC")! as TimeZone Thx again for your patience. – J_A_F_Casillas Nov 11 '18 at 00:32
  • Since you appear to live in the UTC+1 timezone, the output you see is correct. Your code is working fine since you stated you want to show the times in the current user's timezone regardless of where the coordinate is. – rmaddy Nov 11 '18 at 00:37
  • Aaalright then! Thank you all for your support! Especially you @rmaddy for being patient! So we can conclude that the code above is correct and the only thing to do is remove the .timezone line. This question has been answered! – J_A_F_Casillas Nov 11 '18 at 09:24

1 Answers1

-1

Did you try to use the Date method description(with locale: Locale?) to get user's localized time ? This will give you the time of the device

let date = Date().description(with: .current)
    print(date) //Saturday, November 10, 2018 at 11:30:58 AM Central Standard Time

A string representation of the Date, using the given locale, or if the locale argument is nil, in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from UTC (for example, “2001-03-24 10:45:32 +0600”).

After that you can give it any formatt that you want...

Gabo MC
  • 75
  • 8
  • This should only be used for debugging, not for display to a user. And the OP only wants the time, not the date. – rmaddy Nov 10 '18 at 17:54
  • as i wrote at the end of my comment..."you can give the the format that you want..." (HH:MM:SS) and work with that values to compare with sunrise or sunset values.. – Gabo MC Nov 10 '18 at 18:51
  • You can't set a format with the `description(with:)` method and `date` is a `String`, not a `Date`. Please clarify how using the `description(with:)` method of `Date` gives the results desired by the OP. – rmaddy Nov 10 '18 at 18:57