title: How to create a day/night factor from POSIXct variable
body: <p>I have a data table with a column of POSIXct date/times. I would like to create a column that specifies "day" or "night" based on the POSIXct timestamp. Day is defined in local time as between 05:30:00 and 20:00:00.</p>
  
  <p>I have tried to create the new variable using an ifelse statement based on a column of "time of day" created using strptime, but end up with strange results.</p>
  
  <p>Here is a simple example - setting it up as a data table to match my real dataset.</p>
  
  <pre><code>library(data.table)
  
  SightingTime = c("2017-07-31 09:56:27 UTC", "2017-07-31 10:36:30 UTC", "2017-08-01 00:07:20 UTC","2017-08-01 01:31:00 UTC", "2017-08-01 10:38:23 UTC", "2017-08-01 21:13:06 UTC", "2017-08-02 15:13:30 UTC", "2017-08-02 18:05:28 UTC", "2017-08-02 21:04:08 UTC")
  x=data.table(SightingTime)
  </code></pre>
  
  <p>First I extract the time of day from the date/time variable - I want this in  local time because I'll specify sunrise/sunset in local time.</p>
  
  <pre><code>x$TOD = strftime(x$SightingTime, format="%H:%M:S",tz="America/Halifax")
  </code></pre>
  
  <p>I'm not sure why the new TOD variable is still in UTC even though I specify a different time zone.</p>
  
  <p>Then a try to create a new variable with an ifelse statement</p>
  
  <pre><code>x$daynight = with(x,
             ifelse(TOD &gt; 05:30:00 &amp; TOD &lt; 20:00:00, "Day", "Night")) 
  </code></pre>
  
  <p>I've clearly got this bit incorrect as I get warning messages AND the results in the "day/night" column do not make sense.</p>
  
  <p>What I'm hoping for is something like this.</p>
  
  <pre><code>             SightingTime      TOD daynight
  1: 2017-07-31 09:56:27 UTC 06:56:27    Day
  2: 2017-07-31 10:36:30 UTC 07:36:30    Day
  3: 2017-08-01 00:07:20 UTC 21:07:20    Night
  4: 2017-08-01 01:31:00 UTC 22:31:00    Night
  5: 2017-08-01 10:38:23 UTC 07:38:23    Day
  6: 2017-08-01 21:13:06 UTC 08:13:06    Day
  7: 2017-08-02 15:13:30 UTC 12:13:30    Day
  8: 2017-08-02 18:05:28 UTC 15:05:28    Day
  9: 2017-08-02 21:04:08 UTC 18:04:08    Day
  </code></pre>