Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with midnight_to_midnight(): cascading effects of how lubridate::ymd_hms() handles midnight. #1

Open
etzkorn opened this issue Feb 7, 2024 · 0 comments

Comments

@etzkorn
Copy link

etzkorn commented Feb 7, 2024

Hi Marta,
Please see the following reproducible example highlighting some problems we are experiencing with the package. As the function midnight_to_midnight() produces some unexpected results, the rest of the package (e.x. activity_stats()) seems broken as well.

The error seems to originate with the lubridate function ymd_hms, which fails to parse date-times at midnight, though it does parse character vectors at midnight. A potential solution would be to convert the user input to a character vector before re-converting to date-time. I've also "hacked" another solution in which I add one second to each date time, but I am not yet sure if that works for higher-level arc tools functions like activity_stats().

devtools::install_github("martakarass/arctools")
#install.packages("arctools")
library(arctools)
library(lubridate)

# generate a time vector 6am to 6pm next day
start.time <- ymd_hms("2024-02-07 06:00:00 UTC")
acc_ts <- start.time + minutes(0:1439)
class(acc_ts)
# Result is "POSIXct" "POSIXt" 

acc <- rnorm(length(acc_ts))

ts_mtm  <- midnight_to_midnight(acc = acc_ts, acc_ts = acc_ts)
# Warning message:
# 1 failed to parse.

# check that the length is the desired output
length(acc_ts) #1440
length(ts_mtm) #4320 
# in addition to the parsing error, the mtm function adds 1 extra day (1440 minutes) 
# at the end

# the error appears to happen when midnight_to_midnight calls lubridate::ymd_hms

acc_ts2 <- lubridate::ymd_hms(acc_ts)

# print original time stamps around the problematic one
acc_ts[(which(is.na(acc_ts2))-2):(which(is.na(acc_ts2))+2)]

################################################################################
# a "hack" solution: adding 1 second to date times
acc_ts <- start.time + minutes(0:1439) + seconds(1)
ts_mtm  <- midnight_to_midnight(acc = acc_ts, acc_ts = acc_ts)
# no warning

# check that the length is the desired output
length(acc_ts) #1440
length(ts_mtm) #2880 

Some extra notes on ymd_hms:

The issue has been noted here: tidyverse/lubridate#1124.

Essentially, ymd_hms only "works" for times at midnight which are coded as a character vector, not a date-time. Here is an illustration:

ymd_hms("2024-02-08 00:00:00 UTC")
# "2024-02-08 UTC", works

ymd_hms(ymd_hms("2024-02-08 00:00:00 UTC"))
# fails

ymd_hms("2024-02-08 00:01:00 UTC")
# "2024-02-08 00:01:00 UTC", works

ymd_hms(ymd_hms("2024-02-08 00:01:00 UTC"))
# "2024-02-08 00:01:00 UTC", works
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

No branches or pull requests

1 participant