.NET 是否给了我错误的 2008 年 12 月 29 日的周数? [英] Is .NET giving me the wrong week number for Dec. 29th 2008?

查看:15
本文介绍了.NET 是否给了我错误的 2008 年 12 月 29 日的周数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据官方(公历)日历,2008 年 12 月 29 日的周数是 1,因为在 2008 年的最后一天之后第 52 周(即 28/12)一年中只剩下三天或更少的天数.有点奇怪,但是好的,规则就是规则.

According to the official (gregorian) calendar, the week number for 29/12/2008 is 1, because after the last day of week 52 (i.e. 28/12) there are three or less days left in the year. Kinda weird, but OK, rules are rules.

所以根据这个日历,我们有 2008/2009 年的这些边界值

So according to this calendar, we have these boundary values for 2008/2009

  • 28/12 是第 52 周
  • 29/12 是第 1 周
  • 1/1 是第 1 周
  • 8/1 是第 2 周

C# 提供了一个 GregorianCalendar 类,它有一个函数 GetWeekOfYear(date, rule, firstDayOfWeek).

C# offers a GregorianCalendar class, that has a function GetWeekOfYear(date, rule, firstDayOfWeek).

参数 rule 是一个具有 3 个可能值的枚举:FirstDay、FirstFourWeekDay、FirstFullWeek.根据我的理解,我应该遵循 FirstFourWeekDay 规则,但为了以防万一,我尝试了所有这些规则.

The parameter rule is an enumeration with 3 possible values: FirstDay, FirstFourWeekDay, FirstFullWeek. From what I've understood I should go for the FirstFourWeekDay rule, but I tried all of them just in case.

最后一个参数告知应将哪一天视为一周的第一天,根据该日历,它是星期一,所以是星期一.

The last parameter informs which week day should be considered the first day of the week, according to that calendar it's Monday so Monday it is.

所以我启动了一个快速而肮脏的控制台应用程序来测试这个:

So I fired up a quick and dirty console app to test this:

using System;
using System.Globalization;

namespace CalendarTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var cal = new GregorianCalendar();
            var firstWeekDay = DayOfWeek.Monday;
            var twentyEighth = new DateTime(2008, 12, 28);
            var twentyNinth = new DateTime(2008, 12, 29);
            var firstJan = new DateTime(2009, 1, 1);
            var eightJan = new DateTime(2009, 1, 8);
            PrintWeekDays(cal, twentyEighth, firstWeekDay);
            PrintWeekDays(cal, twentyNinth, firstWeekDay);
            PrintWeekDays(cal, firstJan, firstWeekDay);
            PrintWeekDays(cal, eightJan, firstWeekDay);
            Console.ReadKey();
        }

        private static void PrintWeekDays(Calendar cal, DateTime dt, DayOfWeek firstWeekDay)
        {
            Console.WriteLine("Testing for " + dt.ToShortDateString());
            Console.WriteLine("--------------------------------------------");
            Console.Write(CalendarWeekRule.FirstDay.ToString() + "		");
            Console.WriteLine(cal.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, firstWeekDay));
            Console.Write(CalendarWeekRule.FirstFourDayWeek.ToString() + "	");
            Console.WriteLine(cal.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, firstWeekDay));
            Console.Write(CalendarWeekRule.FirstFullWeek.ToString() + "		");
            Console.WriteLine(cal.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, firstWeekDay));
            Console.WriteLine("--------------------------------------------");
        }
    }
}

...这就是我得到的

Testing for 28.12.2008
--------------------------------------------
FirstDay                52
FirstFourDayWeek        52
FirstFullWeek           51
--------------------------------------------
Testing for 29.12.2008
--------------------------------------------
FirstDay                53
FirstFourDayWeek        53
FirstFullWeek           52
--------------------------------------------
Testing for 01.01.2009
--------------------------------------------
FirstDay                1
FirstFourDayWeek        1
FirstFullWeek           52
--------------------------------------------
Testing for 08.01.2009
--------------------------------------------
FirstDay                2
FirstFourDayWeek        2
FirstFullWeek           1
--------------------------------------------

如我们所见,以上组合均不符合官方日历(如果您赶时间,请注意 29/12 永远不会成为第 1 周).

So as we see, none of the combinations above matches the official calendar (if you are in a hurry, just see that 29/12 never gets week #1).

我在这里做错了什么?也许我缺少一些明显的东西?(现在是周五,比利时的工作时间很晚,请多多包涵;))

What am I getting wrong here? Maybe there's something glaring that I am missing? (it's Friday and late work hours here in Belgium, bear with me ;))

也许我应该解释一下:我需要一个适用于任何年份的函数,返回与我链接的公历相同的结果.所以 2008 年没有特殊的解决方法.

Maybe I should explain: what I need is a function that works for any year, returning the same results as the gregorian calendar I linked. So no special workarounds for 2008.

推荐答案

这个文章 深入研究了问题和可能的解决方法.问题的核心在于 .NET 日历实现似乎并没有忠实地实现 ISO 标准

This article looks deeper into the issue and possible workarounds. The hub of the matter is that the .NET calendar implementation does not seem to faithfully implement the ISO standard

这篇关于.NET 是否给了我错误的 2008 年 12 月 29 日的周数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆