{"id":2521,"date":"2019-05-29T14:03:19","date_gmt":"2019-05-29T21:03:19","guid":{"rendered":"https:\/\/lukemiller.org\/?p=2521"},"modified":"2019-05-29T14:03:19","modified_gmt":"2019-05-29T21:03:19","slug":"r-as-date-and-time-zones","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2019\/05\/r-as-date-and-time-zones\/","title":{"rendered":"R as.Date() and time zones"},"content":{"rendered":"\n<p>Here&#8217;s a fun quirk to watch out for when playing around in R with POSIX time values and converting them to dates. <\/p>\n\n\n\n<p>I&#8217;ll start by creating a time variable, in the POSIXct class, and set it to use my current time zone, which is Pacific Daylight Time (PST8PDT). <\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">mydatetime = as.POSIXct('2019-05-29 23:59', tz = 'PST8PDT')<\/pre>\n\n\n\n<p>If I then wanted to turn this time into a simple Date, dropping the time information, I would (naively) do the following:<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">as.Date(mydatetime)<\/pre>\n\n\n\n<p>and I&#8217;d get back the following result:<\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">\"2019-05-30\"<\/pre>\n\n\n\n<p>which is a day later than my original time value, which was May 29 in the Pacific Daylight Time zone. That&#8217;s because R has helpfully decided that the POSIXct time value I supplied to as.Date() must obviously be the UTC time zone, which is how all POSIXct values are stored internally (with a time zone offset applied as needed). <\/p>\n\n\n\n<p>To get the intended result, I need to tell as.Date() to use the correct time zone. I could either specify PST8PDT explicitly, or in this case, since my computer system&#8217;s time is set to PST8PDT, I can just use the tz=&#8221; argument, which means &#8220;use the current system time zone&#8221;.  <\/p>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">as.Date(mydatetime, tz = '')<\/pre>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">\"2019-05-29\"<\/pre>\n\n\n\n<p>In that case, I get the intended date. <\/p>\n\n\n\n<p>The as.Date() function&#8217;s help page spells all of this out:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>The as.Date methods accept character strings, factors, logical NA and objects of classes &#8220;POSIXlt&#8221; and &#8220;POSIXct&#8221;. (The last is converted to days by ignoring the time after midnight in the representation of the time in specified time zone, default UTC.)<\/p><\/blockquote>\n\n\n\n<p>but that would require actually reading the help file all the way through. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a fun quirk to watch out for when playing around in R with POSIX time values and converting them to dates. I&#8217;ll start by creating a time variable, in the POSIXct class, and set it to use my current time zone, which is Pacific Daylight Time (PST8PDT). If I then wanted to turn this [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[58],"class_list":["post-2521","post","type-post","status-publish","format-standard","hentry","category-journal","tag-r-project"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/2521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/comments?post=2521"}],"version-history":[{"count":2,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/2521\/revisions"}],"predecessor-version":[{"id":2523,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/2521\/revisions\/2523"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=2521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=2521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=2521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}