tidyverse / hms

A simple class for storing time-of-day values
https://hms.tidyverse.org/
Other
138 stars 25 forks source link

conflict with the documentation? #71

Closed randomgambit closed 4 years ago

randomgambit commented 5 years ago

Hello there

Consider this simple example with hms_0.4.2

tibble(gmt_time = c('2016-07-08 15:30:10'), value = c(1)) %>% 
  mutate(gmt_time = ymd_hms(gmt_time),
         est_time = with_tz(gmt_time, tz = 'US/Eastern'),
         mytz = tz(gmt_time),
         myhour_utc_no = as.hms(gmt_time),
         myhour_est_no = as.hms(est_time),
         myhour_utc = hms::as.hms(gmt_time, tz = 'UTC'),
         myhour_est = hms::as.hms(est_time, tz = 'US/Eastern'))
# A tibble: 1 x 8
  gmt_time                value est_time                mytz  myhour_utc_no myhour_est_no myhour_utc myhour_est
  <dttm>                  <dbl> <dttm>                  <chr> <time>        <time>        <time>     <time>    
1 2016-07-08 15:30:10.000     1 2016-07-08 11:30:10.000 UTC   11:30         11:30         15:30      11:30     

The current documentation says that

tz
The time zone in which to interpret a POSIXt time for extracting the time of day. The default is now the zone of x but was "UTC" for v0.3 and earlier. The previous behavior can be restored by calling pkgconfig::set_config("hms::default_tz", "UTC"), see pkgconfig::set_config().

So I am surprised that myhour_utc_no is actually wrong and returns the time in my local timezone ('US/Eastern'). Am I missing something here?

Thanks!

krlmlr commented 4 years ago

as.hms() is deprecated in favor of as_hms(), which no longer has a tz argument: https://hms.tidyverse.org/reference/Deprecated.html.

Does with_tz() give the results you expect?

library(tidyverse)
library(hms)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:hms':
#> 
#>     hms
#> The following object is masked from 'package:base':
#> 
#>     date
tibble(gmt_time = c("2016-07-08 15:30:10"), value = c(1)) %>%
  mutate(
    gmt_time = ymd_hms(gmt_time),
    est_time = with_tz(gmt_time, tz = "US/Eastern"),
    mytz = tz(gmt_time),
    myhour_utc_no = as_hms(gmt_time),
    myhour_est_no = as_hms(est_time),
    myhour_utc = as_hms(with_tz(gmt_time, "UTC")),
    myhour_est = as_hms(with_tz(est_time, "US/Eastern"))
  ) %>%
  glimpse()
#> Rows: 1
#> Columns: 8
#> $ gmt_time      <dttm> 2016-07-08 15:30:10
#> $ value         <dbl> 1
#> $ est_time      <dttm> 2016-07-08 11:30:10
#> $ mytz          <chr> "UTC"
#> $ myhour_utc_no <time> 15:30:10
#> $ myhour_est_no <time> 11:30:10
#> $ myhour_utc    <time> 15:30:10
#> $ myhour_est    <time> 11:30:10

Created on 2019-12-31 by the reprex package (v0.3.0)

randomgambit commented 4 years ago

Great. That is much less error prone

github-actions[bot] commented 3 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.