Changes in print output format when creating a vector of datetimes


#1

Hello!

I found a weird behaviour when I was creating data to test some logic. Creating a vector of dates changes the way R prints the value in the console (and in the interface of R Studio). Values are stored ok, but it is a little bit anoying and I want to know why it happens.

library(lubridate)
my_date <- ymd_hms("2018-01-01 13:01:00")

print(my_date)
[1] "2018-01-01 13:01:00 UTC"
print(c(my_date))
[1] "2018-01-01 10:01:00 -03"

And when I create it with timezone it also happens.

library(lubridate)
my_date_tz <- ymd_hms("2018-01-01 13:01:00 -03")
print(my_date_tz)
[1] "2018-01-01 16:01:00 UTC"
print(c(my_date_tz))
[1] "2018-01-01 13:01:00 -03"

When creating and printing a data.frame happens the same:

library(lubridate)
my_date <- ymd_hms("2018-01-01 13:01:00")
x <- data.frame(
   my_date = my_date,
   c.mydate = c(my_date)
)
print(x)
              my_date            c.mydate
1 2018-01-01 13:01:00 2018-01-01 10:01:00

Captura2

My sessionInfo:

sessionInfo()

R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Argentina.1252  LC_CTYPE=Spanish_Argentina.1252   
[3] LC_MONETARY=Spanish_Argentina.1252 LC_NUMERIC=C                      
[5] LC_TIME=Spanish_Argentina.1252   

Why is this happening? Any ideas?

Regards!


#2

You should use Sys.setenv(TZ = "UTC") (taken from here):

Sys.unsetenv("TZ")
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
my_date <- ymd_hms("2018-01-01 13:01:00")
print(my_date)
#> [1] "2018-01-01 13:01:00 UTC"
print(c(my_date))
#> [1] "2018-01-01 14:01:00 CET"

my_date_tz <- ymd_hms("2018-01-01 13:01:00 -03")
print(my_date_tz)
#> [1] "2018-01-01 16:01:00 UTC"
print(c(my_date_tz))
#> [1] "2018-01-01 17:01:00 CET"

my_date <- ymd_hms("2018-01-01 13:01:00")
x <- data.frame(
  my_date = my_date,
  c.mydate = c(my_date)
)
print(x)
#>               my_date            c.mydate
#> 1 2018-01-01 13:01:00 2018-01-01 14:01:00

Sys.setenv(TZ = "UTC")
my_date <- ymd_hms("2018-01-01 13:01:00")
print(my_date)
#> [1] "2018-01-01 13:01:00 UTC"
print(c(my_date))
#> [1] "2018-01-01 13:01:00 UTC"
Sys.unsetenv("TZ")

Created on 2018-05-14 by the reprex package (v0.2.0).