to_time
does work. It converts both times to your local timezone, but that makes no difference when subtracting them. This definitely works:
date_time2.to_time - date_time1.to_time
Your real problem is that you're not parsing the PM, which is why your difference ends up off by 12 hours! Look at your example
date_time1 = DateTime.strptime("01-15-2014 01:11:12 PM", '%m-%d-%Y %l:%M:%S')
# date_time1: 2014-01-15T01:11:12+00:00
You are asking for 1:11 PM UTC, but it is telling you that date_time1
is 1:11 AM. You need to add to your format string
'%m-%d-%Y %l:%M:%S %p'
Here's an example from my timezone if you're still skeptical.
d1 = DateTime.now
#<DateTime: 2014-06-18T11:47:22-04:00 ((2456827j,56842s,704352659n),-14400s,2299161j)>
d1.to_time - DateTime.strptime("06-18-2014 03:47:00 PM", '%m-%d-%Y %l:%M:%S %p').to_time
# 22.704352659
Note that 3:47 PM UTC is 11:47 AM in UTC-4 (the timezone of d1
), so ~22 seconds is the correct answer.
EDIT
If, however, you want to change them to be in the same timezone before calculating the offset, you can do the following:
date_time2.to_time - (date_time1 - date_time2.offset).to_time