0

From an API I get the following two date strings. Those dates show the time of a weather observation of a weather station that’s located in some time zone.

dateUTC = "2019-06-14T18:00:00Z" dateLocal = "2019-06-14 20:00:00"

How can I determine the time zone the weather station is in? (Not the time zone of the user!)

I've tried to use the DateFormatter, but unfortunately the local date gets immediately transformed to GMT - and it's then identical with the UTC date.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let dateUTC = dateFormatter.date(from: current.currentObservation[0].timeUTC)!
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateLocal = dateFormatter.date(from: current.currentObservation[0].timeLocal)!

I would like to see as the result of the above date(s): Time zone = GMT+2

Brezentrager
  • 879
  • 1
  • 7
  • 9
  • 1
    Possible duplicate of [iOS: Convert UTC NSDate to local Timezone](https://stackoverflow.com/questions/1268509/ios-convert-utc-nsdate-to-local-timezone) ... the existing answer is in Objective-C, but these questions are more related to API than language. – Joshua Nozzi Jun 14 '19 at 16:05
  • 1
    The two times are the same if the local timezone is UTC+2. The question you should be asking is "how to determine the local timezone?". Once you have the `TimeZone`, getting its offset from UTC is trivial. Either way, the answer is independent of any specific dates you are getting. – rmaddy Jun 14 '19 at 17:17
  • The next question is why do you need to know the local timezone offset? What do you think you need to do with that information? – rmaddy Jun 14 '19 at 17:20
  • The data comes from Private Weather Stations (PWS). For my app I need to determine, in which time zone the weather station is located. The time zone of the PWS might be a different one than the time zone the user is in. So my question might be: How can I determine the time zone of "dateLocal"? – Brezentrager Jun 14 '19 at 18:20
  • @Brezentrager no need to find out the user timezone you just have to parse the dateUTC string and use the resulting date. If you need to display it to the user use DateFormatter dateStyle and timeStyle as needed – Leo Dabus Jun 14 '19 at 19:46
  • @LeoDabus My problem is that I have to display the correct time in the time zone of the weather station. So in the example: 20:00:00. But when converting the string to a date format in Swift, the system automatically assumes that the date is in the user's time zone - which is not necessarily true. One way of handling this would of course be to split the strings into its individual components (year, month, day ...) and do the calculation manually. But I'm hoping there's a more elegant way of doing this. – Brezentrager Jun 14 '19 at 20:02
  • I finally found the solution! Before using the DateFormatter on the localDate, I had to set the DateFormatter.timeZone = TimeZone(abbreviation: "UTC"). This gives me two dates in the same time zone, which I can now compare. Thanks for all your hints! – Brezentrager Jun 14 '19 at 20:11
  • https://stackoverflow.com/questions/28016578/how-to-create-a-date-time-stamp-and-format-as-iso-8601-rfc-3339-utc-time-zone/28016692#28016692 – Leo Dabus Jun 14 '19 at 20:21

1 Answers1

0

The answer was quite simple - still I didn't see it for hours. The trick is to set a timeZone for the DateFormatter.

            let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
        let dateUTC = dateFormatter.date(from: current.currentObservation[0].timeUTC)!
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
        let dateLocal = dateFormatter.date(from: current.currentObservation[0].timeLocal)!

        let difference = Calendar.current.dateComponents([.second], from: dateUTC, to: dateLocal).second!
Brezentrager
  • 879
  • 1
  • 7
  • 9
  • @vadian If you re-post the answer that you had deleted, I‘ll accept it. Your answer provided me with the decisive hint. – Brezentrager Jun 14 '19 at 20:37