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

Writing POSIXlt columns loses timezones #1474

Open
tohka opened this issue Feb 27, 2023 · 3 comments
Open

Writing POSIXlt columns loses timezones #1474

tohka opened this issue Feb 27, 2023 · 3 comments
Labels
bug an unexpected problem or unintended behavior datetime 📆 write ✏️

Comments

@tohka
Copy link

tohka commented Feb 27, 2023

write_csv removes the time zone information from the POSIXlt values in tibble and appends Z to the output.

> version
               _                                
platform       x86_64-w64-mingw32               
arch           x86_64                           
os             mingw32                          
crt            ucrt                             
system         x86_64, mingw32                  
status                                          
major          4                                
minor          2.2                              
year           2022                             
month          10                               
day            31                               
svn rev        83211                            
language       R                                
version.string R version 4.2.2 (2022-10-31 ucrt)
nickname       Innocent and Trusting            

> library(readr)
> packageVersion("readr")
[1] ‘2.1.4> library(tibble)
> packageVersion("tibble")
[1] ‘3.1.8> Sys.timezone()
[1] "Asia/Tokyo"
> dt <- "2000/01/01 09:00:00"
> dt.ct <- as.POSIXct(dt, tz=Sys.timezone())
> dt.ct
[1] "2000-01-01 09:00:00 JST"
> dt.lt <- as.POSIXlt(dt, tz=Sys.timezone())
> dt.lt
[1] "2000-01-01 09:00:00 JST"
> df <- data.frame(ct=dt.ct, lt=dt.lt)
> df
                   ct                  lt
1 2000-01-01 09:00:00 2000-01-01 09:00:00
> tbl <- tibble(ct=dt.ct, lt=dt.lt)
> tbl
# A tibble: 1 × 2
  ct                  lt                 
  <dttm>              <dttm>             
1 2000-01-01 09:00:00 2000-01-01 09:00:00

> write_csv(df, "write_csv_df.csv")
> readLines("write_csv_df.csv")
[1] "ct,lt"                                    
[2] "2000-01-01T00:00:00Z,2000-01-01T00:00:00Z"
> write_csv(tbl, "write_csv_tbl.csv")
> readLines("write_csv_tbl.csv")
[1] "ct,lt"                                    
[2] "2000-01-01T00:00:00Z,2000-01-01T09:00:00Z"

"2000-01-01 09:00:00 JST" equals "2000-01-01 00:00:00Z".

However, when using tibble, the POSIXlt value of "2000-01-01 09:00:00 JST" is output as "2000-01-01 09:00:00Z".

@hadley
Copy link
Member

hadley commented Jul 31, 2023

Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for me to recreate your problem so that I can fix it: please help me help you! If you've never heard of a reprex before, start by reading about the reprex package, including the advice further down the page. Please make sure your reprex is created with the reprex package as it gives nicely formatted output and avoids a number of common pitfalls.

@hadley hadley added the reprex needs a minimal reproducible example label Jul 31, 2023
@tohka

This comment was marked as outdated.

@hadley hadley changed the title Timezone is removed from POSIXlt value when using tibble Writing POSIXlt columns loses timezones Aug 1, 2023
@hadley
Copy link
Member

hadley commented Aug 1, 2023

Here's a somewhat more minimal reprex:

library(readr)
lt <- as.POSIXlt("2000/01/01 09:00:00", tz = "Asia/Tokyo")

df <- data.frame(lt = lt)
str(df)
#> 'data.frame':    1 obs. of  1 variable:
#>  $ lt: POSIXct, format: "2000-01-01 09:00:00"

tbl <- tibble::tibble(lt = lt)
str(tbl)
#> tibble [1 × 1] (S3: tbl_df/tbl/data.frame)
#>  $ lt: POSIXlt[1:1], format: "2000-01-01 09:00:00"
cat(format_csv(tbl))
#> lt
#> 2000-01-01T09:00:00Z

Created on 2023-08-01 with reprex v2.0.2

There are two issues: data.frame() automatically turns POSIXlt to POSIXlt so there's no POSIXlt in the data frame example. So write_csv()/format_csv() always appears to lose timezones of POSIXlt variables.

@hadley hadley added bug an unexpected problem or unintended behavior write ✏️ datetime 📆 and removed reprex needs a minimal reproducible example labels Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior datetime 📆 write ✏️
Projects
None yet
Development

No branches or pull requests

2 participants