`weekday` 是否保证在 [1:7] 范围内?
Is `weekday` guaranteed to be in the range [1:7]?
我相信答案是肯定的,但 Apple 的文档暗示答案是否定的。
Weekday units are the numbers 1 through n, where n is the number of days in the week. For example, in the Gregorian calendar, n is 7 and Sunday is represented by 1. weekday
这句话似乎暗示不同的日历系统有不同的周长。在维基百科上做了一些研究后,我找不到 n != 7
的任何现代日历。
我是不是看错了? Foundation 中是否有任何日历的星期为 n != 7
?
这不一定是保证,但以下代码找不到除 1..<8
.
以外没有工作日范围的任何语言环境的日历
let calIds: [Calendar.Identifier] = [ .buddhist, .chinese, .coptic, .ethiopicAmeteAlem, .ethiopicAmeteMihret, .gregorian, .hebrew, .indian, .islamic, .islamicCivil, .islamicTabular, .islamicUmmAlQura, .iso8601, .japanese, .persian, .republicOfChina]
for calId in calIds {
var cal = Calendar(identifier: calId)
for locId in Locale.availableIdentifiers {
let locale = Locale(identifier: locId)
cal.locale = locale
if let weekdayMin = cal.minimumRange(of: .weekday), let weekdayMax = cal.maximumRange(of: .weekday) {
if weekdayMin == weekdayMax {
if weekdayMin.startIndex != 1 || weekdayMin.count != 7 {
print("Calendar \(calId) with locale \(locId) isn't 1..<8: \(weekdayMin)")
}
} else {
print("Calendar \(calId) with locale \(locId) has a different min and max weekday range: \(weekdayMin) - \(weekdayMax)")
}
} else {
print("Calendar \(calId) with locale \(locId) doesn't have both a min and max weekday range")
}
}
}
我相信答案是肯定的,但 Apple 的文档暗示答案是否定的。
Weekday units are the numbers 1 through n, where n is the number of days in the week. For example, in the Gregorian calendar, n is 7 and Sunday is represented by 1. weekday
这句话似乎暗示不同的日历系统有不同的周长。在维基百科上做了一些研究后,我找不到 n != 7
的任何现代日历。
我是不是看错了? Foundation 中是否有任何日历的星期为 n != 7
?
这不一定是保证,但以下代码找不到除 1..<8
.
let calIds: [Calendar.Identifier] = [ .buddhist, .chinese, .coptic, .ethiopicAmeteAlem, .ethiopicAmeteMihret, .gregorian, .hebrew, .indian, .islamic, .islamicCivil, .islamicTabular, .islamicUmmAlQura, .iso8601, .japanese, .persian, .republicOfChina]
for calId in calIds {
var cal = Calendar(identifier: calId)
for locId in Locale.availableIdentifiers {
let locale = Locale(identifier: locId)
cal.locale = locale
if let weekdayMin = cal.minimumRange(of: .weekday), let weekdayMax = cal.maximumRange(of: .weekday) {
if weekdayMin == weekdayMax {
if weekdayMin.startIndex != 1 || weekdayMin.count != 7 {
print("Calendar \(calId) with locale \(locId) isn't 1..<8: \(weekdayMin)")
}
} else {
print("Calendar \(calId) with locale \(locId) has a different min and max weekday range: \(weekdayMin) - \(weekdayMax)")
}
} else {
print("Calendar \(calId) with locale \(locId) doesn't have both a min and max weekday range")
}
}
}