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

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

问题描述

根据官方日历(Gregorian),对于在29/12/2008周数是1,因为经过52周(即28的最后一天/ 12)有留在一年三个​​或更少天。有点不可思议,但OK,规则是规则。

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周

  • 28/12 is week 52
  • 29/12 is week 1
  • 1/1 is week 1
  • 8/1 is week 2

C#提供的GregorianCalendar类,有功能 GetWeekOfYear(日期,规则,Firstdayofweek可)

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

参数规则是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() + "\t\t");
            Console.WriteLine(cal.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, firstWeekDay));
            Console.Write(CalendarWeekRule.FirstFourDayWeek.ToString() + "\t");
            Console.WriteLine(cal.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, firstWeekDay));
            Console.Write(CalendarWeekRule.FirstFullWeek.ToString() + "\t\t");
            Console.WriteLine(cal.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, firstWeekDay));
            Console.WriteLine("--------------------------------------------");
        }
    }
}



...这是什么我得到

... and this what I get

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天全站免登陆