title: How to save time zone information in R
body: <p>I am trying to find out if it is possible to save the date time zone into a
  file in R.
  Lets create a date/time variable set to EST time.</p>
  <pre class="lang-r prettyprint-override"><code>x &lt;- as.POSIXct(&quot;2015-06-01 12:00:00&quot;, tz = &quot;EST&quot;)
  x
  #&gt; [1] &quot;2015-06-01 12:00:00 EST&quot;
  
  lubridate::tz(x)
  #&gt; [1] &quot;EST&quot;
  
  tmpfile &lt;- tempfile()
  </code></pre>
  <p>I will save this date information (which is in EST time) using base R,
  <code>readr</code> and <code>data.table</code> to see how the information is preserved.</p>
  <p><strong>Base R version</strong></p>
  <p>The date gets converted to UTC and the time is not changed.</p>
  <pre class="lang-r prettyprint-override"><code>write.csv(data.frame(x = x), tmpfile)
  df &lt;- read.csv(tmpfile)
  
  df
  #&gt;   X                   x
  #&gt; 1 1 2015-06-01 12:00:00
  
  lubridate::tz(df$x)
  #&gt; [1] &quot;UTC&quot;
  </code></pre>
  <p><strong>tibble/readr version</strong></p>
  <p>It gets converted to UTC and the time is formatted as UTC as described in
  the help. Time is now 17:00:00.</p>
  <blockquote>
  <p>… POSIXct values are formatted as ISO8601 with a UTC timezone Note:
  POSIXct objects in local or non-UTC timezones will be converted to UTC
  before writing.</p>
  </blockquote>
  <pre class="lang-r prettyprint-override"><code>readr::write_csv(tibble::tibble(x = x), tmpfile)
  df &lt;- readr::read_csv(tmpfile)
  #&gt; Rows: 1 Columns: 1
  #&gt; ── Column specification ────────────────────────────────────────────────────────
  #&gt; Delimiter: &quot;,&quot;
  #&gt; dttm (1): x
  #&gt; 
  #&gt; ℹ Use `spec()` to retrieve the full column specification for this data.
  #&gt; ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
  
  df
  #&gt; # A tibble: 1 × 1
  #&gt;   x                  
  #&gt;   &lt;dttm&gt;             
  #&gt; 1 2015-06-01 17:00:00
  
  lubridate::tz(df$x)
  #&gt; [1] &quot;UTC&quot;
  </code></pre>
  <p><strong>data.table version</strong></p>
  <p>The date gets converted to UTC and the time is formatted as UTC as described in
  the help. Time is now also 17:00:00.</p>
  <pre class="lang-r prettyprint-override"><code>data.table::fwrite(data.table::data.table(x = x), tmpfile)
  df &lt;- data.table::fread(tmpfile)
  
  df
  #&gt;                      x
  #&gt; 1: 2015-06-01 17:00:00
  
  lubridate::tz(df$x)
  #&gt; [1] &quot;UTC&quot;
  </code></pre>
  <p>Is it possible to preserve the time zone when saving a file? If the user is
  not paying attention, this can go silently and create issues.</p>
  <p><sup>Created on 2022-03-14 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v2.0.1)</sup></p>