计算给定年份和周数的起始和结束日期/时间

时间:2012-06-17 04:38:44

标签: c++ datetime

如何计算给定年份的开始和结束日期/时间(YYYY-mm-DD HH:MM :: SS)和一年中的ISO周数?

在再次提出这样的问题之前我搜索过了。当然,在SO上从(年,周数)到(日期时间)的转换有一些线索。但是他们通过讲Perl,PHP,JAVA,SQL,C#,.NET,Excel和其他编程语言来解答,除了C / C ++。

3 个答案:

答案 0 :(得分:0)

我担心使用简单的C ++没有简短的解决方案。但是编写解决方案应该不是一件难事。你只需要处理特殊情况(如第一周和上周,二月,闰年)。方法可以是

  1. 从一年的周数开始,获取该年的day numbers(即开始和结束日期编号)。 (在这里照顾第一周和最后一周)
  2. 现在可以通过day numbers谎言找到月份和日期。 (在这里照顾二月)

答案 1 :(得分:0)

您可以从下面的代码开始并稍微调整一下(您对第一周的定义是什么:第一周或第一周的第一周)。你还必须处理上周和上周的特殊情况。

  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;

答案 2 :(得分:0)

为了帮助其他人,我想回答我自己的问题如下:

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;
}