Skip to content

Fix concurrency safety, panic prevention, and weekday range bug#7

Merged
pnguyen215 merged 7 commits into
masterfrom
copilot/audit-go-code-idiomatic-patterns
Apr 18, 2026
Merged

Fix concurrency safety, panic prevention, and weekday range bug#7
pnguyen215 merged 7 commits into
masterfrom
copilot/audit-go-code-idiomatic-patterns

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 16, 2026

Comprehensive audit identified data races, panic-prone APIs, and incorrect weekday filtering logic.

Concurrency Safety

  • Added GetWeekStartDay() / SetWeekStartDay() with sync.RWMutex protection
  • Deprecated direct access to WeekStartDay global (retained for backward compatibility)

Panic Prevention

  • Added *Safe variants for all weekday methods (MondaySafe, TuesdaySafe, etc.) that return errors instead of panicking
  • Extracted weekdaySafe() helper to reduce duplication

Nil Safety

  • Added getWeekStartDay(), getTimeFormats(), getTimeLocation() helpers for nil-safe Rule access in Timex

Bug Fix

  • Fixed GetWeekdaysInRange() which had broken leap year logic that skipped valid weekdays
// Before: weekdays incorrectly filtered in non-leap years
func GetWeekdaysInRange(start, end time.Time) []time.Time {
    // ... flawed IsLeapYear check that skipped days
}

// After: simple weekday check
func GetWeekdaysInRange(start, end time.Time) []time.Time {
    for current := start; current.Before(end) || current.Equal(end); current = current.AddDate(0, 0, 1) {
        if d := current.Weekday(); d != time.Sunday && d != time.Saturday {
            weekdays = append(weekdays, current)
        }
    }
}

Usage

// Thread-safe week start configuration
timefy.SetWeekStartDay(time.Monday)
day := timefy.GetWeekStartDay()

// Error-returning weekday methods
monday, err := tmx.MondaySafe("2023-10-25")
if err != nil {
    // handle parse error
}

Copilot AI and others added 3 commits April 16, 2026 15:26
…ckage

- Add thread-safe Get/SetWeekStartDay() functions to fix data race on global WeekStartDay
- Add helper methods (getWeekStartDay, getTimeFormats, getTimeLocation) for nil-safe access
- Update BeginningOfWeek to use nil-safe helper methods
- Fix GetWeekdaysInRange incorrect logic that skipped weekdays in leap/non-leap years
- Add safe weekday methods (*Safe variants) that return errors instead of panicking
- Improve error messages with package prefix and proper quoting
- Add comprehensive tests for all fixes including concurrency tests

Agent-Logs-Url: https://github.com/sivaosorg/timefy/sessions/0772df36-2c23-49e6-a98c-f8ff5a636e5c

Co-authored-by: pnguyen215 <39581473+pnguyen215@users.noreply.github.com>
- Extract weekdaySafe helper function to reduce code duplication in *Safe methods
- Improve documentation for deprecated WeekStartDay variable clarifying race condition
- Fix test assertions to match actual weekday calculation behavior
- Add comprehensive validation of month and day in test cases

Agent-Logs-Url: https://github.com/sivaosorg/timefy/sessions/0772df36-2c23-49e6-a98c-f8ff5a636e5c

Co-authored-by: pnguyen215 <39581473+pnguyen215@users.noreply.github.com>
- Add guidance to error message for missing time formats
- Update SundaySafe documentation to accurately describe upcoming Sunday behavior

Agent-Logs-Url: https://github.com/sivaosorg/timefy/sessions/0772df36-2c23-49e6-a98c-f8ff5a636e5c

Co-authored-by: pnguyen215 <39581473+pnguyen215@users.noreply.github.com>
Copilot AI changed the title [WIP] Perform comprehensive Go code review for timefy Fix concurrency safety, panic prevention, and weekday range bug Apr 16, 2026
Copilot AI requested a review from pnguyen215 April 16, 2026 15:32
@pnguyen215 pnguyen215 merged commit bcf5541 into master Apr 18, 2026
8 checks passed
@pnguyen215 pnguyen215 deleted the copilot/audit-go-code-idiomatic-patterns branch April 18, 2026 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[timefy] - Comprehensive Go code review focusing on idiomatic patterns, concurrency safety, security, and cross-platform compatibility.

2 participants