swiftlang / swift-corelibs-foundation

The Foundation Project, providing core utilities, internationalization, and OS independence
swift.org
Apache License 2.0
5.27k stars 1.13k forks source link

[SR-11775] DateFormatter returns incorrect year in Pashto #3375

Open swift-ci opened 4 years ago

swift-ci commented 4 years ago
Previous ID SR-11775
Radar rdar://problem/57176798
Original Reporter kubista.cameloit (JIRA User)
Type Bug
Environment Tested in Xcode 11.1 with Swift 5
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Foundation | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: d7faf79af167c9fe80a07f4bf22e38c9

Issue Description:

Using a simple code snippet, I can demonstrate that the year returned from DateFormatter when using Pashto locale is incorrect.

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ps")
dateFormatter.setLocalizedDateFormatFromTemplate("y")
let yearString = dateFormatter.string(from: Date())

The value of yearString is:

"AP ۱۳۹۸"

Expected output (as seen for example here: https://nsdateformatter.com/):

"۲۰۱۹"

beccadax commented 4 years ago

@swift-ci create

spevans commented 4 years ago

If the format is set via the dateFormat property then it works correctly, however when using the template 'y' the date format used is 'G y' where G is the era 'AD' or 'BC' etc - see http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

Note the .dateFormat property is how https://nsdateformatter.com/ works.

$ cat 11775.swift
import Foundation

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ps")

dateFormatter.dateFormat = "y"
print("Using 'y':", dateFormatter.string(from: Date()))

dateFormatter.setLocalizedDateFormatFromTemplate("y")
let format = dateFormatter.dateFormat!
print("Template 'y' give dateFormat: \(format)")
print("Using '\(format)':", dateFormatter.string(from: Date())) 

$ swift 11775.swift
Using 'y': ۱۳۹۸
Template 'y' give dateFormat: G y
Using 'G y': AP ۱۳۹۸

So the era is probably still incorrect although this is handled by the underlying ICU library

swift-ci commented 4 years ago

Comment by Petr Kubista (JIRA)

I believe I originally discovered this when using the following:

let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium