5

I have a time in the format of "1/1/2010 10:00". I would like to convert this to a time object. This time is a company time based in Calgary, Alberta. Please note there is no daylight savings adjustment made in the company time. How would I convert this to a date-time object?

smci
  • 32,567
  • 20
  • 113
  • 146
user2946746
  • 1,740
  • 3
  • 21
  • 36
  • 1
    What do you mean "without a time zone"? A time by definition has a time zone.... – nico May 05 '15 at 19:06
  • 1
    Sorry, I was meaning the company time does not change with daylight savings, therefore for a good part of the year the companies times do not line up with the local time. – user2946746 May 05 '15 at 19:09
  • Then you mean MT timezone not MST. Right? – smci May 24 '18 at 23:17
  • I think you meant "conver the time, making the 1 hour adjustment for the (MT-MST) discrepancy, based on date" – smci May 24 '18 at 23:25
  • Then presumably you have a column of datetimes, not just one single value. – smci May 24 '18 at 23:26

2 Answers2

1

You can use as.POSIXct to create the date/time object. Looks like Calgary, Alberta is UTC-07:00 as far as time zones go, so you can do

strptime("1/1/2010 10:00", format="%m/%d/%Y %H:%M", tz="Etc/GMT-7")

(assuming month/day/year format -- see ?strptime for other format options). Rather than specifying the true time zone, you could always use "GMT" instead.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • **Duplicate of [Import date-time at a specified timezone, disregard Daylight Savings Time](https://stackoverflow.com/questions/8004050/import-date-time-at-a-specified-timezone-disregard-daylight-savings-time)**. Please vote-to-close. – smci May 25 '18 at 09:19
0

Under the assumption that you do not want the time zone to appear in the final object you can try the following. I am posting this as an answer rather than a comment to MrFlick's answer because if I try using my code with just one date it does not seem to work. For some reason my code seems to only work if there is a data.frame of dates / times.

my.data <- read.csv(text='
id,date,time,latitude,longitud
AA,3/16/2017,1:30,19.735,-156.085
AA,3/16/2017,2:57,19.800,-156.065
AA,3/16/2017,3:42,19.830,-156.057
AA,3/16/2017,17:31,19.963,-155.952
BB,3/16/2017,17:44,19.964,-155.951
BB,3/17/2017,2:46,19.985,-155.998
', header = TRUE, stringsAsFactors = FALSE, sep = ',')

my.data$date.time <- do.call(paste0, list(my.data$date, ' ', my.data$time))

my.data$date.time <- strptime(my.data$date.time, "%m/%d/%Y %H:%M", tz = "")
my.data
#  id      date  time latitude longitud           date.time
#1 AA 3/16/2017  1:30   19.735 -156.085 2017-03-16 01:30:00
#2 AA 3/16/2017  2:57   19.800 -156.065 2017-03-16 02:57:00
#3 AA 3/16/2017  3:42   19.830 -156.057 2017-03-16 03:42:00
#4 AA 3/16/2017 17:31   19.963 -155.952 2017-03-16 17:31:00
#5 BB 3/16/2017 17:44   19.964 -155.951 2017-03-16 17:44:00
#6 BB 3/17/2017  2:46   19.985 -155.998 2017-03-17 02:46:00

str(my.data)
#'data.frame':   6 obs. of  6 variables:
# $ id       : chr  "AA" "AA" "AA" "AA" ...
# $ date     : chr  "3/16/2017" "3/16/2017" "3/16/2017" "3/16/2017" ...
# $ time     : chr  "1:30" "2:57" "3:42" "17:31" ...
# $ latitude : num  19.7 19.8 19.8 20 20 ...
# $ longitud : num  -156 -156 -156 -156 -156 ...
# $ date.time: POSIXlt, format: "2017-03-16 01:30:00" "2017-03-16 02:57:00" "2017-03-16 03:42:00" "2017-03-16 17:31:00" ...
Mark Miller
  • 12,483
  • 23
  • 78
  • 132