SwiftDate icon indicating copy to clipboard operation
SwiftDate copied to clipboard

DateInRegion - Preserve 8601 timezone

Open markst opened this issue 4 years ago • 4 comments

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

markst avatar Aug 26 '20 08:08 markst

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

morgz avatar Apr 01 '21 20:04 morgz

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 avatar Jun 21 '21 12:06 markst

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

markst avatar Jun 21 '21 12:06 markst

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

markst avatar Apr 25 '22 10:04 markst