title: Date time conversion in R
body: <p>I know similar things have been discussed but here's a few things I haven't found answers yet.</p>
  <h1>1: Date to POSIXct</h1>
  <pre><code>&gt; Sys.timezone()
  [1] &quot;EST&quot;
  
  &gt; as.POSIXct(&quot;2011-01-30&quot;)
  [1] &quot;2011-01-30 EST&quot;
  
  &gt; as.POSIXct(as.Date(&quot;2011-01-30&quot;))
  [1] &quot;2011-01-29 19:00:00 EST&quot;
  
  &gt; as.POSIXct(as.Date(&quot;2011-01-30&quot;), tz=&quot;EST&quot;)
  [1] &quot;2011-01-29 19:00:00 EST&quot;
  
  &gt; as.POSIXct(as.Date(&quot;2011-01-30&quot;), tz=&quot;GMT&quot;)
  [1] &quot;2011-01-29 19:00:00 EST&quot;
  
  &gt; as.POSIXct(as.Date(&quot;2011-01-30&quot;), tz=&quot;America/New_York&quot;)
  [1] &quot;2011-01-29 19:00:00 EST&quot;
  
  &gt; as.POSIXct(as.Date(&quot;2011-01-30&quot;), tz=&quot;&quot;)
  [1] &quot;2011-01-29 19:00:00 EST&quot;
  </code></pre>
  <p>Can anybody tell me why, if the input is a Date, I cannot get midnight January 30 even though I specified the correct time zone?  While if the input is character string then I'm OK.</p>
  <h1>2: Date/Character conversion.</h1>
  <p>Both are horribly slow.  Then I found that to convert a character string to a Date it's actually a lot faster by converting it to POSIXlt first:</p>
  <pre><code>&gt; d3.str = &quot;2011-03-02 23:59:00&quot;;
  &gt; N=10000
  &gt; system.time(for(i in 1:N) r5.dt = as.Date(d3.str))
     user  system elapsed 
     1.25    0.00    1.24 
  &gt; system.time(for(i in 1:N) r6.dt = as.Date(strptime(d3.str, format=&quot;%Y-%m-%d&quot;)))
     user  system elapsed 
     0.37    0.00    0.38 
  &gt; r5.dt
  [1] &quot;2011-03-02&quot;
  &gt; r5.dt==r6.dt
  [1] TRUE
  </code></pre>
  <p>I'm now confused.  Is there any faster, more elegant way to convert a character to a Date?  And a Date to a character instead as.character which is also quite slow?</p>
  <p>Thank you in advance!</p>