Является ли .NET неправильным номером недели для декабря?29-е 2008 года?

StackOverflow https://stackoverflow.com/questions/428957

Вопрос

В соответствии с официальный (григорианский) календарь, номер недели для 29/12/2008 равен 1, потому что после последнего дня 52-й недели (т.е.28/12) в году осталось три или меньше дня.Немного странно, но ладно, правила есть правила.

Итак, согласно этому календарю, у нас есть эти граничные значения на 2008/2009 год

  • 28/12 - это 52-я неделя
  • 29/12 - неделя 1
  • 1/1 - это неделя 1
  • 8/1 - это неделя 2

C # предлагает класс GregorianCalendar, который имеет функцию GetWeekOfYear(date, rule, firstDayOfWeek).

Параметр rule является перечислением с 3 возможными значениями: FirstDay, FirstFourWeekDay, FirstFullWeek.Из того, что я понял, я должен пойти на FirstFourWeekDay правило, но я перепробовал их все на всякий случай.

Последний параметр сообщает, какой день недели следует считать первым днем недели, согласно этому календарю это понедельник, значит, это понедельник.

Поэтому я запустил быстрое и грязное консольное приложение, чтобы протестировать это:

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("--------------------------------------------");
        }
    }
}

...и это то, что я получаю

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).

Что я здесь понимаю не так?Может быть, есть что-то вопиющее, чего мне не хватает?(сегодня пятница и здесь, в Бельгии, рабочее время позднее, потерпите меня ;))

Редактировать:Может быть, мне следует объяснить:что мне нужно, так это функция, которая работает для любого года, возвращая те же результаты, что и связанный мной григорианский календарь.Так что никаких специальных обходных путей для 2008 года нет.

Это было полезно?

Решение

Это Статья изучает глубже проблему и возможные обходные пути.Суть вопроса заключается в том, что реализация .NET calendar, похоже, не полностью реализует стандарт ISO

Другие советы

@Conrad прав..NET-реализация DateTime и GregorianCalendar не реализуют / не соответствуют полной спецификации ISO 8601.При этом их спецификация чрезвычайно детализирована и нетривиальна для полной реализации, по крайней мере, для синтаксического анализа.

Некоторая дополнительная информация доступна на следующих сайтах:

Проще говоря:

Неделя определяется по ее номеру в данном году и начинается с понедельника.Первая неделя года - это та, которая включает в себя первый четверг, или, что эквивалентно, та, которая включает в себя 4 января.

Вот часть кода, который я использую для правильной обработки дат ISO 8601:

    #region FirstWeekOfYear
    /// <summary>
    /// Gets the first week of the year.
    /// </summary>
    /// <param name="year">The year to retrieve the first week of.</param>
    /// <returns>A <see cref="DateTime"/>representing the start of the first
    /// week of the year.</returns>
    /// <remarks>
    /// Week 01 of a year is per definition the first week that has the Thursday 
    /// in this year, which is equivalent to the week that contains the fourth
    /// day of January. In other words, the first week of a new year is the week
    /// that has the majority of its days in the new year. Week 01 might also 
    /// contain days from the previous year and the week before week 01 of a year
    /// is the last week (52 or 53) of the previous year even if it contains days 
    /// from the new year.
    /// A week starts with Monday (day 1) and ends with Sunday (day 7). 
    /// </remarks>
    private static DateTime FirstWeekOfYear(int year)
    {
        int dayNumber;

        // Get the date that represents the fourth day of January for the given year.
        DateTime date = new DateTime(year, 1, 4, 0, 0, 0, DateTimeKind.Utc);

        // A week starts with Monday (day 1) and ends with Sunday (day 7).
        // Since DayOfWeek.Sunday = 0, translate it to 7. All of the other values
        // are correct since DayOfWeek.Monday = 1.
        if (date.DayOfWeek == DayOfWeek.Sunday)
        {
            dayNumber = 7;
        }
        else
        {
            dayNumber = (int)date.DayOfWeek;
        }

        // Since the week starts with Monday, figure out what day that 
        // Monday falls on.
        return date.AddDays(1 - dayNumber);
    }

    #endregion

    #region GetIsoDate
    /// <summary>
    /// Gets the ISO date for the specified <see cref="DateTime"/>.
    /// </summary>
    /// <param name="date">The <see cref="DateTime"/> for which the ISO date
    /// should be calculated.</param>
    /// <returns>An <see cref="Int32"/> representing the ISO date.</returns>
    private static int GetIsoDate(DateTime date)
    {
        DateTime firstWeek;
        int year = date.Year;

        // If we are near the end of the year, then we need to calculate
        // what next year's first week should be.
        if (date >= new DateTime(year, 12, 29))
        {
            if (date == DateTime.MaxValue)
            {
                firstWeek = FirstWeekOfYear(year);
            }
            else
            {
                firstWeek = FirstWeekOfYear(year + 1);
            }

            // If the current date is less than next years first week, then
            // we are still in the last month of the current year; otherwise
            // change to next year.
            if (date < firstWeek)
            {
                firstWeek = FirstWeekOfYear(year);
            }
            else
            {
                year++;
            }
        }
        else
        {
            // We aren't near the end of the year, so make sure
            // we're not near the beginning.
            firstWeek = FirstWeekOfYear(year);

            // If the current date is less than the current years
            // first week, then we are in the last month of the
            // previous year.
            if (date < firstWeek)
            {
                if (date == DateTime.MinValue)
                {
                    firstWeek = FirstWeekOfYear(year);
                }
                else
                {
                    firstWeek = FirstWeekOfYear(--year);
                }
            }
        }

        // return the ISO date as a numeric value, so it makes it
        // easier to get the year and the week.
        return (year * 100) + ((date - firstWeek).Days / 7 + 1);
    }

    #endregion

    #region Week
    /// <summary>
    /// Gets the week component of the date represented by this instance.
    /// </summary>
    /// <value>The week, between 1 and 53.</value>
    public int Week
    {
        get
        {
            return this.isoDate % 100;
        }
    }
    #endregion

    #region Year
    /// <summary>
    /// Gets the year component of the date represented by this instance.
    /// </summary>
    /// <value>The year, between 1 and 9999.</value>
    public int Year
    {
        get
        {
            return this.isoDate / 100;
        }
    }
    #endregion

Номера недель отличаются от страны к стране и должны зависеть от вашего региона / региональных настроек, если я не совсем ошибаюсь.

Редактировать:Википедия подтверждает мое смутное воспоминание о том, что эти цифры различаются в зависимости от страны: http://en.wikipedia.org/wiki/Week_number#Week_number

Я бы ожидал, что респектабельный фреймворк будет подчиняться СТРАНЕ, выбранной в вашей локальной среде выполнения.

По моему опыту, продемонстрированное поведение является типичным поведением, относящимся к частично завершающей неделе как к неделе 53.Возможно, это связано с тем, что все значительные воздействия, с которыми я сталкивался при работе с номерами недель, были связаны с учетом конца календарного года для целей отчетности, а IRS (или налоговое агентство по вашему выбору) считает, что календарный год заканчивается 31 декабря, а не последней полной неделей года.

Я знаю, что это старый пост, но в любом случае noda time, похоже, дает правильный результат..

В качестве обходного маневра вы могли бы сказать, что номер недели - WeekNumber mod 52 .Я полагаю, что это сработало бы в тех случаях, которые вы описываете.

В качестве обходного пути, почему бы не использовать FirstFourDayWeek но добавьте:

  if ( weekNumber > 52 )
    weekNumber = 1;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top