特定の年の開始日と終了日/時刻 (YYYY-mm-DD HH:MM::SS) とその年の ISO 週番号を計算するにはどうすればよいですか?
この種の質問を再度行う前に、SOを検索しました。確かに、SO での (年、週番号) から (日時) への変換に関するスレッドがいくつかありました。しかし、Perl、PHP、JAVA、SQL、C#、.NET、Excel、および C/C++ を除くその他のプログラミング言語を話すことで回答が得られました。
以下のコードから始めて、少し調整することができます(最初の週の定義は何ですか:最初の丸1週間または最初の部分週)。また、最初と先週の特別なケースを処理する必要があります。
int const year = 2012;
int const week = 24;
boost::gregorian::greg_weekday const firstDayOfWeek = boost::gregorian::Monday;
boost::gregorian::date const jan1st(year, boost::gregorian::Jan, 1);
boost::gregorian::first_day_of_the_week_after const firstDay2ndWeek(firstDayOfWeek);
boost::gregorian::date const begin2ndWeek = firstDay2ndWeek.get_date(jan1st);
boost::gregorian::date const end2ndWeek = begin2ndWeek + boost::gregorian::days(6);
boost::gregorian::date const beginNthWeek = begin2ndWeek + boost::gregorian::weeks(week - 2);
boost::gregorian::date const endNthWeek = end2ndWeek + boost::gregorian::weeks(week - 2);
std::cout << boost::gregorian::to_iso_extended_string(jan1st) << std::endl;
std::cout << boost::gregorian::to_iso_extended_string(begin2ndWeek) << std::endl;
std::cout << boost::gregorian::to_iso_extended_string(end2ndWeek) << std::endl;
std::cout << boost::gregorian::to_iso_extended_string(beginNthWeek) << std::endl;
std::cout << boost::gregorian::to_iso_extended_string(endNthWeek) << std::endl;
他の人を助けるために、私は自分の質問に次のように答えたいと思います。
COleDateTime YearWeekDayToCalendarDate(int nYear, int nWeekNumber, int nWeekDay)
{
// This method requires that one know the weekday of 4 January of the year in question
int nFirstWeekDay = WeekDay(nYear, 1, 4);
// Add 3 to the number of this weekday, giving a correction to be used for dates within this year
int nDaysOffset = nFirstWeekDay + 3;
// Multiply the week number by 7, then add the weekday.
int nOrdinalDayNumber = (nWeekNumber * 7) + nWeekDay;
// From this sum subtract the correction for the year.
nOrdinalDayNumber = nOrdinalDayNumber - nDaysOffset;
// If the ordinal date thus obtained is zero or negative, the date belongs to the previous calendar year;
if (nOrdinalDayNumber <= 0)
nYear--;
int nTotalDaysInTheYear = 365;
if ( LeapYear(nYear) )
nTotalDaysInTheYear++;
// If greater than the number of days in the year, to the following year.
if (nOrdinalDayNumber > nTotalDaysInTheYear)
nYear++;
// The result is the ordinal date, which can be converted into a calendar date using the following function
unsigned int nMonth, nDay;
YearDayToMonthDay(nOrdinalDayNumber, nYear, nDay, nMonth);
COleDateTime dtCalendar(nYear, nMonth, nDay, 0, 0, 0);
return dtCalendar;
}
int WeekDay(int nYear, int nMonth, int nDay)
{
// Find the DayOfYearNumber for the specified nYear, nMonth, and nDay
const int AccumulateDaysToMonth [] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
// Set DayofYear Number for nYear, nMonth, and nDay
int nDayOfYearNumber = nDay + AccumulateDaysToMonth[nMonth - 1];
// Increase of Dayof Year Number by 1, if year is leapyear and month greater than February
//if ( LeapYear(nYear) && (nMonth == 2) )
if ( LeapYear(nYear) && (nMonth > 2) )
nDayOfYearNumber += 1;
// Find the Jan1Weekday for nYear (Monday = 1, Sunday = 7)
int i, j, k, l, nJan1Weekday, nWeekday;
i = (nYear - 1) % 100;
j = (nYear - 1) - i;
k = i + i / 4;
nJan1Weekday = 1 + (((((j / 100) % 4) * 5) + k) % 7);
// Calcuate the WeekDay for the given date
l = nDayOfYearNumber + (nJan1Weekday - 1);
nWeekday = 1 + ((l - 1) % 7);
return nWeekday;
}
void YearDayToMonthDay(unsigned int nYearDay, unsigned int nYear,
unsigned int& nMonthDay, unsigned int& nMonth)
{
// Day is the day between 1 and 366
// Year is the year you wish
unsigned int nMonthTable[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
unsigned int nMonthDays = 0;
if ((nYear % 4 == 0) && ((!(nYear % 100 == 0)) || (nYear % 400 == 0)))
nMonthTable[1] = 29;
else
nMonthTable[1] = 28;
nMonth = 0;
while (nYearDay > nMonthDays)
nMonthDays += nMonthTable[nMonth++];
nMonthDay = nYearDay - nMonthDays + nMonthTable[nMonth - 1];
}
inline bool LeapYear( int nYear )
{
// Find if nYear is LeapYear
if ( (nYear % 4 == 0 && nYear % 100 != 0) || nYear % 400 == 0)
return true;
else
return false;
}
残念ながら、単純な C++ を使用してこの問題を簡単に解決する方法はありません。ただし、ソリューションを作成するのは難しい仕事ではありません。例外的なケース (最初と最後の週、2 月、閏年など) に対処する必要があるだけです。アプローチは
day numbers
(つまり、開始日と終了日の番号) を取得します。(ここで最初と最後の週に注意してください)day numbers
これで、嘘で月と日付を簡単に見つけることができます。(2月分はこちら)