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

[BUG] Datetime being parsed incorrectly #85

Closed
dhersz opened this issue Feb 18, 2021 · 9 comments
Closed

[BUG] Datetime being parsed incorrectly #85

dhersz opened this issue Feb 18, 2021 · 9 comments
Labels
bug Something isn't working

Comments

@dhersz
Copy link
Contributor

dhersz commented Feb 18, 2021

Describe the bug
I'm trying to calculate some routes departing from the following datetime: format(as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S". However, otp_plan() only returns itineraries that start very early in the morning. I tried playing a bit with the timezone argument but nothing worked. Then I checked how datetimes were parsed inside the function and realized that "14:00:00" is being parsed as "02:00", without am/pm indicator.

To Reproduce

date_time <- as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S")
timezone <- "America/Sao_Paulo"

date <- format(date_time, "%m-%d-%Y", tz = timezone)
time <- tolower(format(date_time, "%I:%M%p", tz = timezone))

date
#> [1] "05-13-2019"
time
#> [1] "02:00"

Expected behavior
time should be 02:00pm

System

R version 4.0.4 (2021-02-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=pt_BR.UTF-8       LC_NUMERIC=C               LC_TIME=pt_BR.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=pt_BR.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=pt_BR.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.13.6     opentripplanner_0.3.1

loaded via a namespace (and not attached):
 [1] rstudioapi_0.13   knitr_1.31        magrittr_2.0.1    R6_2.5.0         
 [5] rlang_0.4.10      pbapply_1.4-3     highr_0.8         tools_4.0.4      
 [9] parallel_4.0.4    geodist_0.0.7     checkmate_2.0.0   xfun_0.21        
[13] tinytex_0.29      cli_2.3.0         clipr_0.7.1       htmltools_0.5.1.1
[17] ellipsis_0.3.1    yaml_2.2.1        digest_0.6.27     assertthat_0.2.1 
[21] tibble_3.0.6      lifecycle_1.0.0   crayon_1.4.1      processx_3.4.5   
[25] callr_3.5.1       ps_1.5.0          vctrs_0.3.6       fs_1.5.0         
[29] curl_4.3          glue_1.4.2        evaluate_0.14     rmarkdown_2.6    
[33] reprex_1.0.0      compiler_4.0.4    pillar_1.4.7      backports_1.2.1  
[37] pkgconfig_2.0.3  
@dhersz dhersz added the bug Something isn't working label Feb 18, 2021
@mem48
Copy link
Contributor

mem48 commented Feb 18, 2021

@dhersz can you share the code you are using for opentripplanner otherwise I can't see what you are doing

@mem48
Copy link
Contributor

mem48 commented Feb 18, 2021

Also I suggest you use the lubridate package as formatting date-times in R can be fiddly

date_time = lubridate::dmy_hms("13-05-2019 14:00:00", tz = "America/Sao_Paulo")
tolower(format(date_time, "%I:%M%p", tz = "America/Sao_Paulo"))
[1] "02:00pm"

@dhersz
Copy link
Contributor Author

dhersz commented Feb 18, 2021

This is how I'm calling otp_plan():

  routes <- otp_plan(
    otpcon, 
    from_place, 
    to_place, 
    fromID = from_place_ids, 
    toID = to_place_ids,
    mode = c("WALK", "TRANSIT"),
    date_time = as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S"),
    maxWalkDistance = 1000,
    numItineraries = 1,
    ncores = n_cores,
    distance_balance = TRUE,
    get_geometry = FALSE
  )

EDIT:

I think this has to do with my OS, I just tried with lubridate and I get the same results.

@mem48
Copy link
Contributor

mem48 commented Feb 18, 2021

Did you specify a timezone in otpcon and have you tried using lubridate. I think the problem is with how you are specifying the date_time

@mem48
Copy link
Contributor

mem48 commented Feb 18, 2021

Doesn't sound like a problem with the package, as I can get the correct time on my computer. I'd try googling date/time problems in R

@dhersz
Copy link
Contributor Author

dhersz commented Feb 18, 2021

Yes, otpcon$timezone is specified as "America/Sao Paulo".

Also tried with lubridate and didn't work. The problem might be with my OS? Not sure.

date_time <- lubridate::dmy_hms("13-05-2019 14:00:00", tz = "America/Sao_Paulo")

date <- format(date_time, "%m-%d-%Y", tz = "America/Sao_Paulo")
time <- tolower(format(date_time, "%I:%M%p", tz = "America/Sao_Paulo"))

date
#> [1] "05-13-2019"
time
#> [1] "02:00"

@dhersz
Copy link
Contributor Author

dhersz commented Feb 18, 2021

From strftime() documentation:

%p
AM/PM indicator in the locale. Used in conjunction with %I and not with %H. An empty string in some locales (for example on some OSes, non-English European locales including Russia). The behaviour is undefined if used for input in such a locale.

@mem48
Copy link
Contributor

mem48 commented Feb 18, 2021

Perhaps your local does not support the %p ? OpenTripPlanner expects the time with an AM/PM value. Could you change your local in R. I think Greenland shares a timezone with Brazil?

@dhersz
Copy link
Contributor Author

dhersz commented Feb 18, 2021

Changing system locale did the trick!

Sys.setlocale("LC_TIME","en_US.UTF-8")
#> [1] "en_US.UTF-8"

date_time <- as.POSIXct("13-05-2019 14:00:00", format = "%d-%m-%Y %H:%M:%S")
timezone <- "America/Sao_Paulo"

time <- tolower(format(date_time, "%I:%M%p", tz = timezone))
time
#> [1] "02:00pm"

Thanks for the heads-up @mem48!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants