en_us locale and en_uk,de_de的weekofday不同

发布于 2025-01-26 08:24:43 字数 1969 浏览 3 评论 0原文

我在应用程序中显示一周中的一周,用户可以选择一周的第一天以及德语或英语本地化。我注意到选择英语的一周有很奇怪的差异。

有人可以解释出什么问题吗?我检查了我们是否有某种不同的编号,但是唯一的区别是,一周的第一天是星期天,而不是星期一...

import UIKit

func getCalForId(_ identifier: String, andLocale locale: String) -> Calendar {
    var cal = Calendar(identifier: .gregorian)
    cal.timeZone = TimeZone(identifier: identifier)!
    cal.locale = Locale(identifier: locale)
    
    return cal
}

func GetDateByString(_ value: String) -> Date {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd MMM yyyy HH:mm:ss Z"
    
    return dateFormatter.date(from: value)!
}

let calDE_Berlin = getCalForId("Europe/Berlin", andLocale: "de_DE")
var calUS_Chicago = getCalForId("America/Chicago", andLocale: "en_US")
var calUS_Berlin = getCalForId("Europe/Berlin", andLocale: "en_US")
let calUK_Berlin = getCalForId("Europe/Berlin", andLocale: "en_UK")
        
let date = GetDateByString("5 5 2022 18:45:32 +0200")

let weekDE_Berlin = calDE_Berlin.dateComponents([.weekOfYear], from: date).weekOfYear!
let weekUS_Chicago = calUS_Chicago.dateComponents([.weekOfYear], from: date).weekOfYear!
let weekUS_Berlin = calUS_Berlin.dateComponents([.weekOfYear], from: date).weekOfYear!
let weekUK_Berlin = calUK_Berlin.dateComponents([.weekOfYear], from: date).weekOfYear!

print(calDE_Berlin.firstWeekday)            // 2
print(calDE_Berlin.minimumDaysInFirstWeek)  // 4
print(calUS_Chicago.firstWeekday)           // 1
print(calUS_Chicago.minimumDaysInFirstWeek) // 1
print(calUS_Berlin.firstWeekday)            // 1
print(calUS_Berlin.minimumDaysInFirstWeek)  // 1
print(calUK_Berlin.firstWeekday)            // 2
print(calUK_Berlin.minimumDaysInFirstWeek)  // 4

print(weekDE_Berlin)  // 18
print(weekUS_Chicago) // 19
print(weekUS_Berlin)  // 19
print(weekUK_Berlin)  // 18

谢谢您的预先

问候

Edit 添加了minimumDaysInfirstweek的输出。对于德国来说,似乎只有不同。但这没有解释,为什么基于EN_US的基于EN_US的日历正在输出第19年的一周。

I am showing week of the year in my app and users can choose first day of week as well as German or English localisation. I noticed a weird difference in the week of year when English is chosen.

Can someone explain what's going wrong? I checked if US has different numbering of some sort, but the only difference is, that the first day of the week is Sunday for US instead of Monday...

import UIKit

func getCalForId(_ identifier: String, andLocale locale: String) -> Calendar {
    var cal = Calendar(identifier: .gregorian)
    cal.timeZone = TimeZone(identifier: identifier)!
    cal.locale = Locale(identifier: locale)
    
    return cal
}

func GetDateByString(_ value: String) -> Date {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd MMM yyyy HH:mm:ss Z"
    
    return dateFormatter.date(from: value)!
}

let calDE_Berlin = getCalForId("Europe/Berlin", andLocale: "de_DE")
var calUS_Chicago = getCalForId("America/Chicago", andLocale: "en_US")
var calUS_Berlin = getCalForId("Europe/Berlin", andLocale: "en_US")
let calUK_Berlin = getCalForId("Europe/Berlin", andLocale: "en_UK")
        
let date = GetDateByString("5 5 2022 18:45:32 +0200")

let weekDE_Berlin = calDE_Berlin.dateComponents([.weekOfYear], from: date).weekOfYear!
let weekUS_Chicago = calUS_Chicago.dateComponents([.weekOfYear], from: date).weekOfYear!
let weekUS_Berlin = calUS_Berlin.dateComponents([.weekOfYear], from: date).weekOfYear!
let weekUK_Berlin = calUK_Berlin.dateComponents([.weekOfYear], from: date).weekOfYear!

print(calDE_Berlin.firstWeekday)            // 2
print(calDE_Berlin.minimumDaysInFirstWeek)  // 4
print(calUS_Chicago.firstWeekday)           // 1
print(calUS_Chicago.minimumDaysInFirstWeek) // 1
print(calUS_Berlin.firstWeekday)            // 1
print(calUS_Berlin.minimumDaysInFirstWeek)  // 1
print(calUK_Berlin.firstWeekday)            // 2
print(calUK_Berlin.minimumDaysInFirstWeek)  // 4

print(weekDE_Berlin)  // 18
print(weekUS_Chicago) // 19
print(weekUS_Berlin)  // 19
print(weekUK_Berlin)  // 18

Thanks advance

Regards

EDIT
Added the output for minimumDaysInFirstWeek. Seems only to be different for Germany. But it doesn't explain, why the en_US locale based calendar is outputting week of year 19.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看春风乍起 2025-02-02 08:24:43

多亏了iłyabursov,我现在了解了“问题”。
我发现这仍然非常令人困惑,大多数网站实际上是为我们展示第18周的第18周(通过格式,语言环境:en_us)。另一方面,如果我们没有真正使用它们,那也是可以理解的,每页都不重要 /不同:D

作为“解决方案”,这篇文章有助于理解:
https://devblogs.microsoft.s.microsoft.com/oldnewthing/20151112-2-00/? p = 92121

Thanks to Iłya Bursov I understand the "issue" now.
I find it still very confusing, that most websites are showing week 18 for US where in fact its 19 (by format, locale: en_US). On the other hand, if US is not really using them, its also understandable, that it doesn't matter too much / is different per page :D

As a "solution" this post helped understanding:
https://devblogs.microsoft.com/oldnewthing/20151112-00/?p=92121

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文