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

[SR-3298] Initializing TimeZone with Identifier results in wrong type/kind. #4437

Open
swift-ci opened this issue Nov 30, 2016 · 9 comments
Open

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-3298
Radar None
Original Reporter aaroncrespo (JIRA User)
Type Bug
Environment

Swift 3.0.1 Xcode Version 8.2

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, SDKOverlay
Assignee None
Priority Medium

md5: b1c50b35a172a505428c7f4d999bdc1a

Issue Description:

Initializing a TimeZone by identifier with an identifier matching the user's current timezone results in an unexpected TimeZone kind.

input:

// hardware set to America/New_York
var timezone = TimeZone.current
var atimezone = TimeZone.autoupdatingCurrent
var ftimezone = TimeZone(identifier: "America/New_York")!
dump(timezone)
dump(atimezone)
dump(ftimezone)

output:

America/New_York (current)
  - identifier: "America/New_York"
  - kind: "current"abbreviation: Optional("EST")
    - some: "EST"
  - secondsFromGMT: -18000
  - isDaylightSavingTime: falseAmerica/New_York (autoupdatingCurrent)
  - identifier: "America/New_York"
  - kind: "autoupdatingCurrent"abbreviation: Optional("EST")
    - some: "EST"
  - secondsFromGMT: -18000
  - isDaylightSavingTime: falseAmerica/New_York (current)
  - identifier: "America/New_York"
  - kind: "*current*"abbreviation: Optional("EST")
    - some: "EST"
  - secondsFromGMT: -18000
  - isDaylightSavingTime: false

expected output:

America/New_York (current)
  - identifier: "America/New_York"
  - kind: "current"abbreviation: Optional("EST")
    - some: "EST"
  - secondsFromGMT: -18000
  - isDaylightSavingTime: falseAmerica/New_York (autoupdatingCurrent)
  - identifier: "America/New_York"
  - kind: "autoupdatingCurrent"abbreviation: Optional("EST")
    - some: "EST"
  - secondsFromGMT: -18000
  - isDaylightSavingTime: falseAmerica/New_York (current)
  - identifier: "America/New_York"
  - kind: "*fixed*"abbreviation: Optional("EST")
    - some: "EST"
  - secondsFromGMT: -18000
  - isDaylightSavingTime: false
@swift-ci
Copy link
Contributor Author

Comment by aaron crespo (JIRA)

Additionally: this behavior is different from Calendar fixed and Calendar current and Locale fixed and Locale current. which is why I thought it was unexpected.

@swift-ci
Copy link
Contributor Author

Comment by aaron crespo (JIRA)

After looking at the source code it appears this might be intentional, however I think the difference in behavior between Locale, Calendar, Timezone, and equality is not documented.

Locale from identifier is never equal to Locale current, same as Calendar from identifier and Calendar current, but sometimes Timezone from identifier is equal to Timezone current.

It makes sense to me that a captured current Locale can be equal to a specific instance of a Locale. In which case Timezone is correct but Locale, and Calendar have the different/unexpected behavior,

@swift-ci
Copy link
Contributor Author

Comment by aaron crespo (JIRA)

@parkera thoughts?

@parkera
Copy link
Member

parkera commented Dec 15, 2016

The above output does make sense to me, because what it's saying is that ftimezone == the current time zone. I think the output of the debug description could use some improvement however.

I wanted the equality and updating behavior of these three types to be the same. It sounds to me like a bug if the current Calendar can never be equal to a Calendar from an identifier (if other properties are equal). Same for Locale.

@parkera
Copy link
Member

parkera commented Dec 15, 2016

I do also want to note that the autoupdating version of these three can only ever be equal to another autoupdating instance of the same type.

@swift-ci
Copy link
Contributor Author

Comment by aaron crespo (JIRA)

Yeah after digging into the code I could see it being a difference in behavior between calendar/locale and timezone. Since timezone is the odd man out I thought it was culprit.

> Calendar can never be equal to a Calendar from an identifier (if other properties are equal). Same for Locale.

I just verified that this is the case let me know if I missed anything

import Foundation
let ccurrent = Calendar.current
dump(ccurrent)
let acurrent = Calendar.autoupdatingCurrent
dump(acurrent)
let fcurrent = Calendar(identifier: .gregorian)
dump(fcurrent)

print(ccurrent == acurrent)
print(ccurrent == fcurrent)
//output:gregorian (current)
  - identifier: Foundation.Calendar.Identifier.gregorian
  - kind: "current"locale: Optional(en_US (current))
    ▿ some: en_US (current)
      - identifier: "en_US"
      - kind: "current"timeZone: America/New_York (current)
    - identifier: "America/New_York"
    - kind: "current"abbreviation: Optional("EST")
      - some: "EST"
    - secondsFromGMT: -18000
    - isDaylightSavingTime: false
  - firstWeekday: 1
  - minimumDaysInFirstWeek: 1gregorian (autoupdatingCurrent)
  - identifier: Foundation.Calendar.Identifier.gregorian
  - kind: "autoupdatingCurrent"locale: Optional(en_US (current))
    ▿ some: en_US (current)
      - identifier: "en_US"
      - kind: "current"timeZone: America/New_York (current)
    - identifier: "America/New_York"
    - kind: "current"abbreviation: Optional("EST")
      - some: "EST"
    - secondsFromGMT: -18000
    - isDaylightSavingTime: false
  - firstWeekday: 1
  - minimumDaysInFirstWeek: 1gregorian (fixed)
  - identifier: Foundation.Calendar.Identifier.gregorian
  - kind: "fixed"locale: Optional( (fixed))
    ▿ some:  (fixed)
      - identifier: ""
      - kind: "fixed"timeZone: America/New_York (current)
    - identifier: "America/New_York"
    - kind: "current"abbreviation: Optional("EST")
      - some: "EST"
    - secondsFromGMT: -18000
    - isDaylightSavingTime: false
  - firstWeekday: 1
  - minimumDaysInFirstWeek: 1

false

false

Commenting out

            //    lhs.locale == rhs.locale 

Produces the expected result in Calendar but should be fixed on Locale. Shall I edit this ticket or close/open a new one? looks like the issue is Locale's Equality since it propagates Calendar and not Timezone.

@parkera
Copy link
Member

parkera commented Dec 16, 2016

We can use this ticket, since it's got the history in it.

@swift-ci
Copy link
Contributor Author

Comment by aaron crespo (JIRA)

@parkera so I don't think there is an issue here Calendar.current gets the system Locale but Calendar(identifier🙂 gets no defined locale. Locale should be taken into account for equality because two same calendars will have different behaviors depending on Locale (first day of the week ETC).

For reference:

        let ccurrent = Calendar.current
        var fcurrent = Calendar(identifier: Calendar.current.identifier)
        fcurrent.locale = ccurrent.locale
        
        XCTAssertEqual(ccurrent, fcurrent) // passes

Behaves as expected.

@parkera
Copy link
Member

parkera commented Dec 19, 2016

Yes I agree that Calendar locale should be part of equality.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants