Hi Tom,<div><br></div><div>Thanks for your reply, and for the trick of forcibly establishing the timezone to GMT.</div><div><br></div><div>I'm surprised of the behavior with c(), and can understand that data.table inherits it. However, for the record, when rbinding data.frames (instead of data.tables), there is no loss of timezone info (this is what I'm currently doing in my code, building an intermediate data.frame, and then converting it wholesale to data.table when it's done -- timezones stay what they should be, even though this is not optimal). So apparently, there are multiple instances of core R code doing date vector concatenation, and they disagree as to timezone handling...</div>
<div><br></div><div>I will impatiently await the integer date and time classes. (I currently have to manually set the storage.mode of a POSIXct vector to "integer" to be able to use them as keys in my data.tables.) Moreover, I looked into chron and it's not suitable for a few things I need from POSIXct.</div>
<div><br></div><div> Best,</div><div> + Nicolas</div><div><br><br><div class="gmail_quote">On Tue, Aug 31, 2010 at 10:05 PM, Short, Tom <span dir="ltr"><<a href="mailto:TShort@epri.com">TShort@epri.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div>
<div dir="ltr" align="left"><font face="Arial" size="2"><span>Nicolas, </span></font></div>
<div dir="ltr" align="left"><font face="Arial" size="2"><span></span></font> </div>
<div dir="ltr" align="left"><font face="Arial" size="2"><span>That's not really a data.table issue. It's generally a
problem with POSIXct. See here:</span></font></div>
<div dir="ltr" align="left"><font face="Arial" size="2"><span></span></font> </div>
<div dir="ltr" align="left"><font face="Arial" size="2"><span>> x = seq(as.POSIXct("2010-01-01", "GMT"),
as.POSIXct("2010-01-10", "GMT"), length.out=2)<br>> <br>> c(x,x)<br>[1]
"2009-12-31 19:00:00 EST" "2010-01-09 19:00:00 EST"<br>[3] "2009-12-31 19:00:00
EST" "2010-01-09 19:00:00 EST"<br></span></font></div>
<div dir="ltr" align="left"><font face="Arial" size="2"><span>To get around this, I generally fix the timezone for
the system as follows:</span></font></div>
<div dir="ltr" align="left"><font face="Arial" size="2"><span> <br>> Sys.setenv(TZ="GMT") # time zones drive
me crazy!<br>> <br>> c(x,x)<br>[1] "2010-01-01 GMT" "2010-01-10 GMT"
"2010-01-01 GMT" "2010-01-10 GMT"<br></span></font></div>
<div><font face="Arial" size="2"><span>Also, if you just
have dates, it's better to stick with the Date class (or chron). In the
development version of data.table, we also have integer date and time classes
that work well with data.table (faster sorting and
grouping).</span></font></div>
<div><font face="Arial" size="2"><span></span></font> </div>
<div><font face="Arial" size="2"><span>-
Tom</span></font></div>
<div><font face="Arial" size="2"></font> </div><font face="Arial" size="2"></font><br>
<blockquote dir="ltr" style="padding-left:5px;margin-left:5px;border-left:#000000 2px solid;margin-right:0px">
<div lang="en-us" dir="ltr" align="left">
<hr>
<font face="Tahoma" size="2"><b>From:</b>
<a href="mailto:datatable-help-bounces@lists.r-forge.r-project.org" target="_blank">datatable-help-bounces@lists.r-forge.r-project.org</a>
[mailto:<a href="mailto:datatable-help-bounces@lists.r-forge.r-project.org" target="_blank">datatable-help-bounces@lists.r-forge.r-project.org</a>] <b>On Behalf Of
</b>Nicolas Chapados<br><b>Sent:</b> Tuesday, August 31, 2010
15:40<br><b>To:</b>
<a href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">datatable-help@lists.r-forge.r-project.org</a><br><b>Subject:</b> [datatable-help]
Automagic timezone convertion on data.table rbind<br></font><br></div><div><div></div><div class="h5">
<div></div>Dear list,
<div><font face="Arial" size="2"></font><br></div>
<div>I'm using a data.table that contains date columns (in POSIXct format).
There appears to be an issue with automatic timezone conversion when
rbinding from NULL (or an empty data.table), as would occur when one is
progressively building up the table in a loop.</div>
<div><font face="Arial" size="2"></font><br></div>
<div>For example:</div>
<div><font face="Arial" size="2"></font><br></div>
<div>
<div>> require(data.table)</div>
<div>Loading required package: data.table</div>
<div><font face="Arial" size="2"></font><br></div>
<div>## Currently located in the North-America Eastern Standard Time
zone.</div>
<div>> Sys.timezone()</div>
<div>[1] "America/Montreal"</div>
<div>> a <- NULL</div>
<div><font face="Arial" size="2"></font><br></div>
<div>## Create a table with a single date column (GMT timezone)</div>
<div>> b <- data.table(x = seq(as.POSIXct("2010-01-01", "GMT"),
as.POSIXct("2010-01-10", "GMT"), length.out=10))</div>
<div>> b</div>
<div> x</div>
<div> [1,] 2010-01-01</div>
<div> [2,] 2010-01-02</div>
<div> [3,] 2010-01-03</div>
<div> [4,] 2010-01-04</div>
<div> [5,] 2010-01-05</div>
<div> [6,] 2010-01-06</div>
<div> [7,] 2010-01-07</div>
<div> [8,] 2010-01-08</div>
<div> [9,] 2010-01-09</div>
<div>[10,] 2010-01-10</div>
<div><font face="Arial" size="2"></font><br></div>
<div>## Bind it from NULL: Oops! The timezone changes to EST!</div>
<div>> rbind(a,b)</div>
<div>
x</div>
<div> [1,] 2009-12-31 19:00:00</div>
<div> [2,] 2010-01-01 19:00:00</div>
<div> [3,] 2010-01-02 19:00:00</div>
<div> [4,] 2010-01-03 19:00:00</div>
<div> [5,] 2010-01-04 19:00:00</div>
<div> [6,] 2010-01-05 19:00:00</div>
<div> [7,] 2010-01-06 19:00:00</div>
<div> [8,] 2010-01-07 19:00:00</div>
<div> [9,] 2010-01-08 19:00:00</div>
<div>[10,] 2010-01-09 19:00:00</div>
<div><font face="Arial" size="2"></font><br></div>
<div>## Same behavior from an empty data table.</div>
<div>> rbind(data.table(), b)</div>
<div>
x</div>
<div> [1,] 2009-12-31 19:00:00</div>
<div> [2,] 2010-01-01 19:00:00</div>
<div> [3,] 2010-01-02 19:00:00</div>
<div> [4,] 2010-01-03 19:00:00</div>
<div> [5,] 2010-01-04 19:00:00</div>
<div> [6,] 2010-01-05 19:00:00</div>
<div> [7,] 2010-01-06 19:00:00</div>
<div> [8,] 2010-01-07 19:00:00</div>
<div> [9,] 2010-01-08 19:00:00</div>
<div>[10,] 2010-01-09 19:00:00</div></div>
<div><font face="Arial" size="2"></font><br></div>
<div>This behavior is somewhat puzzling. Any pointers as to how to
preserve timezone information would be greatly appreciated! For the
record, I'm with R version 2.9.2 and time.date version 1.4.1 (the latest
on CRAN).</div>
<div><font face="Arial" size="2"></font><br></div>
<div>Best regards,</div>
<div>+ Nicolas Chapados</div>
<div><br></div></div></div></blockquote></div>
</blockquote></div><br></div>