janzzon/difftimeOffice

Days between start and end not counted properly

Closed this issue · 5 comments

Hi,

When running difftime_office_hours( as.POSIXct("2020-01-06 14:17:00"), as.POSIXct("2016-01-08 07:21:00"), working_hours = c(7, 19)) i noticed it doesn't count the day in between (2020-01-07). This is due to the internal function dates_span_fun, it returns NA while it should return "2020-01-07". The problem is in the if statement, see below. The if statement compares the two dates including the times, while it should only compare the date part. This could be easily fixed by using as.Date(day_before_ended) and as.Date(day_after_started). After changing this the function returned the "2020-01-07" correctly.

dates_span_fun <- function(started, ended)	{
    day_after_started <- started + lubridate::days(1) # "2020-01-07 14:17:00"
    day_before_ended <- ended - lubridate::days(1) # "2016-01-07 07:21:00"
    if(day_before_ended < day_after_started) {return(as.POSIXct(rep(NA, 1)))}
    return(
        seq(
            lubridate::floor_date(day_after_started, unit = "day"),
            lubridate::floor_date(day_before_ended, unit = "day"), 
            by = "days"
        )
    )
}

Hi @TimvanSchynel !

Thanks for this bug report and your research of the issue. Since you seem to have found a fix, would you want to submit a PR to fix it? If not, no worries, I can.

This project is also in need of some tests if you'd like to contribute any. The example you gave would be a good one.

I'm quite new to github so I'm not quite sure how to do that yet, you can fix it if it's ok. As for the tests, i would love to but I'm really swamped with school projects & exams right now. I will be using this package a bit more so i will post any issues i find on here.

Thanks TimvanSchynel,

I am fixing this and will shortly push a commit!

This has been fixed in #9
After @janzzon merges, he can close this issue!

closed,
Thanks @TimvanSchynel for notice of the problem and open an issue.
Thanks @johncassil for fix and PR!