Skip to content

write_* quietly changes datetime to UTC #555

@atungate

Description

@atungate

I noticed that haven exports datetimes as UTC regardless of timezone (which I do not think needs to change). However, I think it helpful for the UTC conversion to be explicit. E.g., with either a message in R if there is a non-UTC datetime, or a note in the documentation. Small issue, but some haven users may not realize they are exporting their datetime as UTC.

Exemplified here:

library(haven)
library(tibble)

test = tibble(date = as.Date("1999-12-31 22:00"),
              time = readr::parse_time("22:00"),
              date_time_edt = as.POSIXct("1999-12-31 22:00 EST"), # haven quietly coerces to UTC for date-time
              date_time_utc = as.POSIXct("1999-12-31 22:00", tz = "UTC"))

# SPSS 
spss_tmpfile = tempfile(fileext = ".sav")
write_sav(test, spss_tmpfile)[]
read_sav(spss_tmpfile)

# SAS 
sas_tmpfile = tempfile(fileext = ".sas7bdat")
write_sas(test, sas_tmpfile)[]
read_sas(sas_tmpfile)

# STATA 
stata_tmpfile = tempfile(fileext = ".dta")
write_dta(test, stata_tmpfile)[]
read_dta(stata_tmpfile) # side-note, check "time" column on import. you may be aware but I don't use stata, so I am not sure

# Cleanup
ls(pattern = "_tmpfile") # just to make sure I don't remove anything unintended from your env
file.remove(unlist(mget(ls(pattern = "_tmpfile"))))
rm(list = ls(pattern = "_tmpfile"))

Related:
#25

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions