Skip to content

bug in week means first week only has 6 days? #2611

@MichaelChirico

Description

@MichaelChirico
dates = structure(0:7, class = 'Date')
dates
# [1] "1970-01-01" "1970-01-02" "1970-01-03"
# [4] "1970-01-04" "1970-01-05" "1970-01-06"
# [7] "1970-01-07" "1970-01-08"

I think we'd expect week(dates) to be 1L for the first seven days, but this is not the case:

week(dates)
# [1] 1 1 1 1 1 1 2 2

The fix is trivial -- the problem is yday returns 1-based days, but relying on %/% needs 0-based days:

week = function(x) (yday(x) - 1L) %/% 7L + 1L
# -or better, given what yday does- 
week = function(x) as.POSIXlt(x)$yday %/% 7L + 1L

Just want to make sure this is not expected behavior? And if this is expected, we should document.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions