summaryrefslogtreecommitdiff
path: root/core
Side-by-side diff
Diffstat (limited to 'core') (more/less context) (show whitespace changes)
-rw-r--r--core/pim/datebook/datebookweek.cpp74
1 files changed, 48 insertions, 26 deletions
diff --git a/core/pim/datebook/datebookweek.cpp b/core/pim/datebook/datebookweek.cpp
index ce0b10b..8caa827 100644
--- a/core/pim/datebook/datebookweek.cpp
+++ b/core/pim/datebook/datebookweek.cpp
@@ -589,36 +589,52 @@ QDate DateBookWeek::weekDate() const
return d.addDays(-dayoffset);
}
-// this used to only be needed by datebook.cpp, but now we need it inside
-// week view since
-// we need to be able to figure out our total number of weeks on the fly...
-// this is probably the best place to put it..
+// This used to only be needed by datebook.cpp, but now we need it
+// inside week view since we need to be able to figure out our total
+// number of weeks on the fly. This is probably the best place to put
+// it.
// For Weeks that start on Monday... (EASY!)
-// At the moment we will use ISO 8601 method for computing
-// the week. Granted, other countries use other methods,
-// bet we aren't doing any Locale stuff at the moment. So,
-// this should pass. This Algorithim is public domain and
-// available at:
+// At the moment we will use the ISO 8601 method for computing the week.
+// Granted, other countries use other methods, but we aren't doing any
+// Locale stuff at the moment. Without locale info it is too hard to
+// guess what method should be used. So, this should pass. This
+// algorithim is public domain and available at:
// http://personal.ecu.edu/mccartyr/ISOwdALG.txt
-// the week number is return, and the year number is returned in year
+// the week number is returned, and the year number is returned in year
// for Instance 2001/12/31 is actually the first week in 2002.
-// There is a more mathematical definition, but I will implement it when
-// we are pass our deadline.
+// There is a more mathematical definition, it should be implemented.
// For Weeks that start on Sunday... (ahh... home rolled)
-// okay, if Jan 1 is on Friday or Saturday,
-// it will go to the pervious
-// week...
-
+// okay, if Jan 1 is on Friday or Saturday, it will go to the previous
+// week.
+
+/*!
+ * The week number and year number of a date may actually be different
+ * then the date itself as specified by the ISO 8601 standard. For
+ * example if Jan 1 year falls on a Friday, the week number will
+ * be the last week number for the previous year (52 of year - 1). Another
+ * example is that the date 2001-12-31 falls on a Monday, its week number
+ * is 1 and its year is 2002. This function provides stripped down
+ * version of the alogrithm described in
+ * http://personal.ecu.edu/mccartyr/ISOwdALG.txt to provide the correct
+ * week number and year number for a given date.
+ *
+ * \param d The date that you want to know the week number for.
+ * \param week A reference to the variable you want to store the week
+ * number in.
+ * \param year A reference to the variable you want to store the year
+ * number in.
+ * \param startOnMonday Inform the function whether weeks start on
+ * Sunday or Monday. Set to true if your weeks start on Monday.
+ * \return false if the supplied date is invalid, true otherwise.
+ */
bool calcWeek( const QDate &d, int &week, int &year,
bool startOnMonday )
{
- int weekNumber;
- int yearNumber;
+ int weekNumber = -1;
+ int yearNumber = -1;
- // remove a pesky warning, (Optimizations on g++)
- weekNumber = -1;
int jan1WeekDay = QDate(d.year(), 1, 1).dayOfWeek();
int dayOfWeek = d.dayOfWeek();
@@ -629,18 +645,21 @@ bool calcWeek( const QDate &d, int &week, int &year,
// find the Jan1Weekday;
if ( d.dayOfYear() <= ( 8 - jan1WeekDay) && jan1WeekDay > 4 ) {
yearNumber = d.year() - 1;
- if ( jan1WeekDay == 5 || ( jan1WeekDay == 6 && QDate::leapYear(yearNumber) ) )
+ if ( jan1WeekDay == 5
+ || ( jan1WeekDay == 6 && QDate::leapYear(yearNumber) ) )
weekNumber = 53;
else
weekNumber = 52;
} else
yearNumber = d.year();
+
if ( yearNumber == d.year() ) {
int totalDays = 365;
if ( QDate::leapYear(yearNumber) )
totalDays++;
if ( ((totalDays - d.dayOfYear()) < (4 - dayOfWeek) )
- || (jan1WeekDay == 7) && (totalDays - d.dayOfYear()) < 3) {
+ || (jan1WeekDay == 7) && (totalDays - d.dayOfYear()) < 3)
+ {
yearNumber++;
weekNumber = 1;
}
@@ -654,15 +673,18 @@ bool calcWeek( const QDate &d, int &week, int &year,
} else {
// it's better to keep these cases separate...
if ( d.dayOfYear() <= (7 - jan1WeekDay) && jan1WeekDay > 4
- && jan1WeekDay != 7 ) {
+ && jan1WeekDay != 7 )
+ {
yearNumber = d.year() - 1;
if ( jan1WeekDay == 6
- || (jan1WeekDay == 7 && QDate::leapYear(yearNumber) ) ) {
+ || (jan1WeekDay == 7 && QDate::leapYear(yearNumber) ) )
+ {
weekNumber = 53;
}else
weekNumber = 52;
} else
yearNumber = d.year();
+
if ( yearNumber == d.year() ) {
int totalDays = 365;
if ( QDate::leapYear( yearNumber ) )
@@ -675,11 +697,11 @@ bool calcWeek( const QDate &d, int &week, int &year,
if ( yearNumber == d.year() ) {
int j = d.dayOfYear() + (7 - dayOfWeek % 7) + ( jan1WeekDay - 1 );
weekNumber = j / 7;
- if ( jan1WeekDay > 4 ) {
+
+ if ( jan1WeekDay > 4 )
weekNumber--;
}
}
- }
year = yearNumber;
week = weekNumber;
return true;