malcommac / SwiftDate

🐔 Toolkit to parse, validate, manipulate, compare and display dates, time & timezones in Swift.
MIT License
7.64k stars 770 forks source link

DateInRegion - Preserve 8601 timezone #746

Open markst opened 4 years ago

markst commented 4 years ago

I wish to format a date in it's local timezone, however formatting the date will not preserve the region/timezone:

DateInRegion("2020-08-26T17:00:00+10:00")

Output:

(lldb) po DateInRegion("2020-08-26T17:00:00+10:00")?.region
▿ Optional<Region>
  ▿ some : {calendar='gregorian', timezone='GMT', locale='en_US'}
    ▿ calendar : gregorian (autoupdatingCurrent)
      - identifier : gregorian
      - kind : "autoupdatingCurrent"
      ▿ locale : Optional<Locale>
        ▿ some : en_US (autoupdatingCurrent)
          - identifier : "en_US"
          - kind : "autoupdatingCurrent"
      ▿ timeZone : GMT (fixed)
        - identifier : "GMT"
        - kind : "fixed"
        ▿ abbreviation : Optional<String>
          - some : "GMT"
        - secondsFromGMT : 0
        - isDaylightSavingTime : false
      - firstWeekday : 1
      - minimumDaysInFirstWeek : 1
morgz commented 3 years ago

I think this is the same: https://github.com/malcommac/SwiftDate/issues/775

markst commented 3 years ago

If I set the region to current:

SwiftDate.defaultRegion = .current

The DateInRegion behaves more how I expected:

(lldb) po DateInRegion("2020-08-26T17:00:00+10:00")?.region
▿ Optional<Region>
  ▿ some : {calendar='gregorian', timezone='Australia/Darwin', locale='en_US'}
    ▿ calendar : gregorian (autoupdatingCurrent)
      - identifier : gregorian
      - kind : "autoupdatingCurrent"
      ▿ locale : Optional<Locale>
        ▿ some : en_US (autoupdatingCurrent)
          - identifier : "en_US"
          - kind : "autoupdatingCurrent"
      ▿ timeZone : Australia/Darwin (autoupdatingCurrent)
        - identifier : "Australia/Darwin"
        - kind : "autoupdatingCurrent"
        ▿ abbreviation : Optional<String>
          - some : "GMT+9:30"
        - secondsFromGMT : 34200
        - isDaylightSavingTime : false
      - firstWeekday : 1
      - minimumDaysInFirstWeek : 1
markst commented 3 years ago

Significant when using DateInRegion.dateComponents since wrong timezone component is returned.

markst commented 2 years ago

Just to add; when using toISODate - the region is preserved:

(lldb) po "2020-08-26T17:00:00+10:00".toISODate()?.region
▿ Optional<Region>
  ▿ some : {calendar='gregorian', timezone='GMT+1000', 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+1000 (fixed)
        - identifier : "GMT+1000"
        - kind : "fixed"
        ▿ abbreviation : Optional<String>
          - some : "GMT+10"
        - secondsFromGMT : 36000
        - isDaylightSavingTime : false
      - firstWeekday : 1
      - minimumDaysInFirstWeek : 1