Fix monthly weekly task appearing as due every day (#1266)#1368
Open
zazer0 wants to merge 1 commit intoHabitRPG:developfrom
Open
Fix monthly weekly task appearing as due every day (#1266)#1368zazer0 wants to merge 1 commit intoHabitRPG:developfrom
zazer0 wants to merge 1 commit intoHabitRPG:developfrom
Conversation
Fixes issue HabitRPG#1266 where daily tasks set to repeat monthly on a specific weekday occurrence (e.g., "3rd Tuesday of every month") were incorrectly appearing as due every day instead of just once per month. Root cause: The code was using Calendar.component(.weekOfMonth) which returns which week of the month a date is in (1-6), then subtracting 1. This produced incorrect values, especially when the month starts on the target weekday (resulting in 1 - 1 = 0, which is invalid). Solution: Use DateFormatter pattern "F" to get the weekday ordinal (1-5) which correctly represents which occurrence of that specific weekday it is in the month. This matches what the server expects in the weeksOfMonth array. Changes: - Added Calendar.weekdayOrdinal(for:) extension method - Fixed line 1063 to use weekdayOrdinal instead of weekOfMonth-1
f331269 to
0f45652
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1266 where daily tasks set to repeat monthly on a specific weekday occurrence (e.g., "3rd Tuesday of every month") were incorrectly appearing as due every day instead of just once per month.
Root Cause
The bug was in
TaskFormView.swiftline 1049 (now line 1063). The code was usingCalendar.component(.weekOfMonth, from: startDate) - 1which:.weekOfMonththat returns which week of the month a date is in (1-6), not which occurrence of a specific weekday1 - 1 = 0, which is invalid)Example of the bug:
weekOfMonth(1) - 1 = 0❌ (invalid, causes server to miscalculate and show task as due every day)weekdayOrdinal = 1✅ (correctly indicates 1st occurrence)Solution
Use DateFormatter pattern "F" to get the weekday ordinal (1-5) which correctly represents which occurrence of that specific weekday it is in the month. This matches what the Habitica server expects in the
weeksOfMontharray.Changes
Calendar.weekdayOrdinal(for:)extension method that uses DateFormatter pattern "F"Calendar.current.weekdayOrdinal(for: startDate)instead of the buggy calculationTesting
TaskRepeatablesSummaryInteractor.swiftwhich already uses DateFormatter pattern "FEEEE" for displayTaskRepeatablesSummaryInteractorTests.swift:163-164expectsweeksOfMonth = [5]for "5 Tuesday", confirming server expects 1-indexed values (1-5)Habitica User ID
@zazy0