Added option to return ISO week-numbering instead of default calendar-numbering.

This commit is contained in:
Vincent Richard 2009-12-01 13:44:34 +00:00
parent be8daf3f8e
commit ee2fa4feaf
3 changed files with 10 additions and 7 deletions

View File

@ -256,7 +256,7 @@ int datetimeUtils::getDayOfWeek(const int year, const int month, const int day)
}
int datetimeUtils::getWeekOfYear(const int year, const int month, const int day)
int datetimeUtils::getWeekOfYear(const int year, const int month, const int day, const bool iso)
{
// Algorithm from http://personal.ecu.edu/mccartyr/ISOwdALG.txt
@ -322,7 +322,7 @@ int datetimeUtils::getWeekOfYear(const int year, const int month, const int day)
WeekNumber -= 1;
}
if (WeekNumber == 1 && month == 12)
if (!iso && (WeekNumber == 1 && month == 12))
WeekNumber = 53;
return WeekNumber;

View File

@ -143,10 +143,11 @@ VMIME_TEST_SUITE_BEGIN
{
VASSERT_EQ("1.1", 52, datetimeUtils::getWeekOfYear(2003, 12, 27));
VASSERT_EQ("1.2", 52, datetimeUtils::getWeekOfYear(2003, 12, 28));
VASSERT_EQ("1.3", 1, datetimeUtils::getWeekOfYear(2003, 12, 29));
VASSERT_EQ("1.4", 1, datetimeUtils::getWeekOfYear(2004, 1, 4));
VASSERT_EQ("1.5", 2, datetimeUtils::getWeekOfYear(2004, 1, 5));
VASSERT_EQ("1.6", 2, datetimeUtils::getWeekOfYear(2004, 1, 11));
VASSERT_EQ("1.3", 1, datetimeUtils::getWeekOfYear(2003, 12, 29, true));
VASSERT_EQ("1.4", 53, datetimeUtils::getWeekOfYear(2003, 12, 29, false));
VASSERT_EQ("1.5", 1, datetimeUtils::getWeekOfYear(2004, 1, 4));
VASSERT_EQ("1.6", 2, datetimeUtils::getWeekOfYear(2004, 1, 5));
VASSERT_EQ("1.7", 2, datetimeUtils::getWeekOfYear(2004, 1, 11));
VASSERT_EQ("2.1", 52, datetimeUtils::getWeekOfYear(2004, 12, 26));
VASSERT_EQ("2.2", 53, datetimeUtils::getWeekOfYear(2004, 12, 27));

View File

@ -84,9 +84,11 @@ public:
* @param year year in 4-digit format
* @param month month (1-12), January is 1, December is 12 (see datetime::Months enum)
* @param day month day (1-31)
* @param iso if TRUE, use ISO week-numbering year (default is to use calendar year).
* For more information, read here: http://en.wikipedia.org/wiki/ISO_8601#Week_dates
* @return the week number (1 is the first week of the year)
*/
static int getWeekOfYear(const int year, const int month, const int day);
static int getWeekOfYear(const int year, const int month, const int day, const bool iso = false);
};