diff options
author | Derick Rethans <derick@php.net> | 2008-05-03 10:04:37 +0000 |
---|---|---|
committer | Derick Rethans <derick@php.net> | 2008-05-03 10:04:37 +0000 |
commit | 76708f3c2e39058a4856b3004cafe22bfe1a1a36 (patch) | |
tree | 99832b7eaeafaf3f1cec38751b9630c73f545d76 /ext/date | |
parent | a578b0cbcc1a8705e665e559219beea769ce4007 (diff) | |
download | php-git-76708f3c2e39058a4856b3004cafe22bfe1a1a36.tar.gz |
- MFH: Fixed weekdays adding/subtracting algorithm.
Diffstat (limited to 'ext/date')
-rw-r--r-- | ext/date/lib/tm2unixtime.c | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/ext/date/lib/tm2unixtime.c b/ext/date/lib/tm2unixtime.c index c56a1bdce9..750918ae4f 100644 --- a/ext/date/lib/tm2unixtime.c +++ b/ext/date/lib/tm2unixtime.c @@ -197,35 +197,55 @@ static void do_adjust_relative(timelib_time* time) static void do_adjust_special_weekday(timelib_time* time) { - timelib_sll current_dow, this_weekday = 0, count; + timelib_sll current_dow, count; - current_dow = timelib_day_of_week(time->y, time->m, time->d); count = time->relative.special.amount; + + current_dow = timelib_day_of_week(time->y, time->m, time->d); if (count == 0) { + // skip over saturday and sunday if (current_dow == 6) { - this_weekday = 2; + time->d += 2; } + // skip over sunday if (current_dow == 0) { - this_weekday = 1; + time->d += 1; } - time->d += this_weekday; - return; } else if (count > 0) { + // skip over saturday and sunday if (current_dow == 5) { - this_weekday = 2; + time->d += 2; } + // skip over sunday if (current_dow == 6) { - this_weekday = 1; + time->d += 1; } - } else if (count < 0) { - if (current_dow == 0) { - this_weekday = -1; + // add increments of 5 weekdays as a week + time->d += (count / 5) * 7; + // if current DOW plus the remainder > 5, add two days + current_dow = timelib_day_of_week(time->y, time->m, time->d); + time->d += (count % 5); + if ((count % 5) + current_dow > 5) { + time->d += 2; } + } else if (count < 0) { + // skip over sunday and saturday if (current_dow == 1) { - this_weekday = -2; + time->d -= 2; + } + // skip over satruday + if (current_dow == 0 ) { + time->d -= 1; + } + // subtract increments of 5 weekdays as a week + time->d += (count / 5) * 7; + // if current DOW minus the remainder < 0, subtract two days + current_dow = timelib_day_of_week(time->y, time->m, time->d); + time->d += (count % 5); + if ((count % 5) + current_dow < 1) { + time->d -= 2; } } - time->d += this_weekday + ((count / 5) * 7) + (count % 5); } static void do_adjust_special(timelib_time* time) |