Skip to content

Conversation

@richardwooding
Copy link
Contributor

Summary

Fixes #42 - getDayOfWeek() now correctly returns 6 for Sunday instead of -1.

Problem

  • Root cause: PostgreSQL EXTRACT(DOW) returns 0 for Sunday, but simple subtraction (- 1) resulted in -1
  • Expected: CEL uses ISO 8601 weekday numbering (0=Monday, 6=Sunday)
  • Impact: Medium severity - incorrect day-of-week values for Sunday

Solution

Applied modulo arithmetic to correctly map PostgreSQL DOW to CEL ISO 8601:

(EXTRACT(DOW FROM ...) + 6) % 7

Conversion table:

  • Sunday (DOW=0): (0 + 6) % 7 = 6 ✅
  • Monday (DOW=1): (1 + 6) % 7 = 0 ✅
  • Tuesday (DOW=2): (2 + 6) % 7 = 1 ✅
  • Wednesday (DOW=3): (3 + 6) % 7 = 2 ✅
  • Thursday (DOW=4): (4 + 6) % 7 = 3 ✅
  • Friday (DOW=5): (5 + 6) % 7 = 4 ✅
  • Saturday (DOW=6): (6 + 6) % 7 = 5 ✅

Changes

  1. timestamps.go:

    • Wrap EXTRACT(DOW ...) in parentheses for getDayOfWeek
    • Apply (+ 6) % 7 formula instead of simple - 1
    • Use switch statement for better code organization
  2. pg/postgres17_compat_test.go:

    • Added test for Monday (CEL value 0)
    • Added test for Sunday (CEL value 6) - verifies the fix
    • Added test for Thursday (CEL value 3)
    • All tests run against real PostgreSQL 17 database
  3. docs/operators-reference.md:

    • Updated conversion formula documentation

Test Plan

  • All existing tests pass
  • New tests verify Monday, Sunday, and Thursday conversions
  • Integration tests run against PostgreSQL 17
  • Lint checks pass
  • Code formatted with make fmt

Verification

make fmt
make lint
make test

All checks pass ✅

🤖 Generated with Claude Code

fixes #42)

**Problem:** getDayOfWeek() returned -1 for Sunday instead of 6 (CEL ISO 8601 format).
PostgreSQL EXTRACT(DOW) returns 0 for Sunday, but simple subtraction (- 1) gave -1.

**Solution:** Use modulo arithmetic: (EXTRACT(DOW ...) + 6) % 7
- Sunday (DOW=0): (0 + 6) % 7 = 6 ✓
- Monday (DOW=1): (1 + 6) % 7 = 0 ✓
- Saturday (DOW=6): (6 + 6) % 7 = 5 ✓

**Changes:**
- timestamps.go: Wrap EXTRACT in parentheses and apply (+ 6) % 7 for getDayOfWeek
- pg/postgres17_compat_test.go: Added tests for Monday, Sunday, and Thursday
- docs/operators-reference.md: Updated conversion formula in documentation

**Impact:** All days of week now map correctly to CEL ISO 8601 values (0=Monday, 6=Sunday).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@richardwooding richardwooding merged commit 0fda8a4 into main Oct 30, 2025
9 checks passed
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.

getDayOfWeek() Conversion May Be Incorrect for Sunday

1 participant