janzzon/difftimeOffice

Incorrect time calculation

Opened this issue · 5 comments

First of all a massive thank you for building a package to attempt to handle this function.

Today is the first time I have attempted to use this package, I have ensured that both my start and end variables are in POSIXct format "2021-01-01 03:33:49"

The code in R is thus:
require(difftimeOffice) Turnaround$Office <- difftime_office_hours(Turnaround$Created_Date_Time, Turnaround$Completed_Date_Time, working_hours = c(9, 17))

The output is incorrect for items that fall within the same day. Example a start date of "2021-01-04 11:38:21" against an end date of "2021-01-04 11:40:44", the output from this package gives "26440s (~7.34 hours)" as opposed to the correct 143 seconds actual difference.

On entires with a date range falling across two days it is correct, example a start date of "2021-01-04 14:14:05" against an end date of "2021-01-05 11:43:18" gives 19753s (~5.49 hours) which is correct.

@Saarek84

I feel like there's something unique with your setup or timezone that we are not accounting for. Can you please run this and report the results?

remove.packages('difftimeOffice')
remotes::install_github("janzzon/difftimeOffice")

time_1 <- as.POSIXct('2021-01-04 11:38:21')
time_2 <- as.POSIXct('2021-01-04 11:40:44')

library(difftimeOffice) 

test <- difftime_office_hours(time_1, 
                              time_2, 
                              working_hours = c(9, 17))
test

I get

> test
[1] "143s (~2.38 minutes)"

which is your expected response, so I can't recreate the problem over here!

Apologies for the delayed response. Your test works perfectly, have re-run against the data and have the same issue.

Format for both date variables is POSIXct, format: "2021-01-01 03:33:49" "2021-01-01 03:34:00" "2021-01-01 03:34:10" "2021-01-01 03:34:21"

A created time of 2021-01-04 14:02:25 with a completed time of 2021-01-04 14:36:07 is bringing back 28800s (~8 hours) within the data frame.

But, if I create the times manually as you have done and then run the test it works.

I did get a warning message when I ran against the dataframe: Note: method with signature ‘Duration#ANY’ chosen for function ‘-’,
target signature ‘Duration#Duration’.
"ANY#Duration" would also be valid

Can you please run a dput() on your data and post it here?

Unfortunately I cannot run dput on the whole data frame for you due to confidenital data, but here is is against the data variables being used:

structure(list(Created_Date/Time= structure(c(1609472029, 1609472040, 1609472050, 1609472061, 1609472072, 1609472082), class = c("POSIXct", "POSIXt"), tzone = "UTC"),Completed_Date/Time = structure(c(1611074829, 1611075984, 1611076226, 1611070739, 1611076416, 1611076653), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, 6L), class = "data.frame")

Some entries contain NA values, could that be the cause of the issue?

I am having trouble reproducing on my side, even with this.

Have a look:

library(dplyr)

df <- structure(list(Created_Date_Time = structure(c(1609472029, 1609472040, 1609472050, 1609472061, 1609472072, 1609472082), class = c("POSIXct", "POSIXt"), tzone = "UTC"),Completed_Date_Time = structure(c(1611074829, 1611075984, 1611076226, 1611070739, 1611076416, 1611076653), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, 6L), class = "data.frame")

df[1,1] <- as.POSIXct("2021-01-04 14:02:25", tz = "UTC")
df[1,2] <- as.POSIXct("2021-01-04 14:36:07", tz = "UTC")
df[7,1] <- NA_real_
df[7,2] <- NA_real_

df

    Created_Date_Time Completed_Date_Time
1 2021-01-04 14:02:25 2021-01-04 14:36:07
2 2021-01-01 03:34:00 2021-01-19 17:06:24
3 2021-01-01 03:34:10 2021-01-19 17:10:26
4 2021-01-01 03:34:21 2021-01-19 15:38:59
5 2021-01-01 03:34:32 2021-01-19 17:13:36
6 2021-01-01 03:34:42 2021-01-19 17:17:33
7                <NA>                <NA>


df <- df %>% 
  mutate(diff_time = difftimeOffice::difftime_office_hours(Created_Date_Time, 
                                                           Completed_Date_Time, 
                                                           working_hours = c(9, 17)
  )
  )
df


    Created_Date_Time Completed_Date_Time             diff_time            diff_time2
1 2021-01-04 14:02:25 2021-01-04 14:36:07 2022s (~33.7 minutes) 2022s (~33.7 minutes)
2 2021-01-01 03:34:00 2021-01-19 17:06:24  374400s (~4.33 days)  374400s (~4.33 days)
3 2021-01-01 03:34:10 2021-01-19 17:10:26  374400s (~4.33 days)  374400s (~4.33 days)
4 2021-01-01 03:34:21 2021-01-19 15:38:59  369539s (~4.28 days)  369539s (~4.28 days)
5 2021-01-01 03:34:32 2021-01-19 17:13:36  374400s (~4.33 days)  374400s (~4.33 days)
6 2021-01-01 03:34:42 2021-01-19 17:17:33  374400s (~4.33 days)  374400s (~4.33 days)
7                <NA>                <NA>                  <NA>                  <NA>

I believe everything here is expected!