Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.toISODate(region: Region.ISO) Not working as expected #775

Open
morgz opened this issue Apr 1, 2021 · 1 comment
Open

.toISODate(region: Region.ISO) Not working as expected #775

morgz opened this issue Apr 1, 2021 · 1 comment

Comments

@morgz
Copy link

morgz commented Apr 1, 2021

The docs state this should return the date in the region specified in the ISO string. This is not the case unless I'm missing something. It converts to GMT.

po "2017-08-05T16:04:03+02:00".toISODate(region: Region.ISO)
▿ Optional<DateInRegion>
  ▿ some : {abs_date='2017-08-05T14:04:03Z', rep_date='2017-08-05T14:04:03Z', region={calendar='gregorian', timezone='GMT', locale='en_US_POSIX'}
    ▿ date : 2017-08-05 14:04:03 +0000
      - timeIntervalSinceReferenceDate : 523634643.0
    ▿ region : {calendar='gregorian', timezone='GMT', locale='en_US_POSIX'}
      ▿ calendar : gregorian (fixed)
        - identifier : gregorian
        - kind : "fixed"
        ▿ locale : Optional<Locale>
          ▿ some : en_US_POSIX (fixed)
            - identifier : "en_US_POSIX"
            - kind : "fixed"
        ▿ timeZone : GMT (fixed)
          - identifier : "GMT"
          - kind : "fixed"
          ▿ abbreviation : Optional<String>
            - some : "GMT"
          - secondsFromGMT : 0
          - isDaylightSavingTime : false
        - firstWeekday : 1
        - minimumDaysInFirstWeek : 1
    - customFormatter : nil
@dagronf
Copy link

dagronf commented Apr 26, 2021

I'm very new to this library so I could be wrong here, but doesn't Region.ISO define GMT as the timezone? From Region.swift:83...

/// ISO Region is defined by the gregorian calendar, gmt timezone and english posix locale
public static var ISO: Region {

By specifying Region.ISO as the region you are telling it to create a DateInRegion object that reflects the specified string in the GMT timezone.

let vdate = "2017-08-05T16:04:03+02:00".toISODate(region: Region.ISO)

Optional({abs_date='2017-08-05T14:04:03Z', rep_date='2017-08-05T14:04:03Z', region={calendar='gregorian', timezone='GMT', locale='en_US_POSIX'})

If you don't specify a Region, the returned DateInRegion reflects the timezone defined in the string (which is what I think you wanted?)

let vdate = "2017-08-05T16:04:03+02:00".toISODate()

Optional({abs_date='2017-08-05T14:04:03Z', rep_date='2017-08-05T16:04:03+02:00', region={calendar='gregorian', timezone='GMT+0200', locale='en_US_POSIX'})

Just to be confusing (which dates are!) the date object (type Date) in the DateInRegion result does not have the concept of a timezone - it's just a point in time. To print it for a particular timezone, create a formatter and set the zone on the formatter eg.

let df = ISO8601DateFormatter()
df.timeZone = vdate!.region.timeZone
Swift.print(df.string(from: vdate!.date))

/// Produces "2017-08-05T16:04:03+02:00"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants