-1

I have two datetimes and need to determine the seconds between them. They were created like:

date_time1 = DateTime.strptime("01-15-2014 01:11:12 PM", '%m-%d-%Y %l:%M:%S')
date_time2 = DateTime.now

So they look like this:

date_time1: 2014-01-15T01:11:12+00:00
date_time2: 2014-01-16T00:11:12+10:00

How do I find the seconds between the two datetimes? I have tried converting to a time however the .to_time function is not working for me because the timezone apparently is not set.

Any help would be appreciated.

Thanks

Rj01
  • 93
  • 1
  • 7
  • Possibly a duplicate of [this](http://stackoverflow.com/questions/567636/how-do-i-get-the-number-of-seconds-between-two-datetimes-in-ruby-on-rails) – zishe Jun 18 '14 at 14:55
  • The real problem is that you're not setting the time zone so it's not clear what `date_time1` is supposed to be. Should it be UTC or the same time zone as `date_time2`? – Max Jun 18 '14 at 15:01
  • Thanks, I do agree, I am not sure how to set it. It should be UTC+0000 – Rj01 Jun 18 '14 at 15:07
  • Then what is the issue? In what way is `to_time` not working? – Max Jun 18 '14 at 15:09
  • When I do .to_time it uses the current local time. I have read this is because the timezone is not set. As far as I can see it is though... – Rj01 Jun 18 '14 at 15:13

1 Answers1

0

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
Max
  • 21,123
  • 5
  • 49
  • 71
  • I need them to be two different timezones and determine the difference in seconds between them. One is timezone +00:00 the other is +10:00. The two times are the current time of different timezones. – Rj01 Jun 18 '14 at 15:16
  • Thanks, I also tried that earlier with no luck. DateTime = DateTime.strptime(stringDateTime, '%m-%d-%Y %l:%M:%S %p') Time = DateTime.to_time results in the current local time of my machine. I realise it must be something wrong with my strptime function though. I also tried an uppercase P. – Rj01 Jun 18 '14 at 15:38
  • Please carefully read my answer. The timezone does not matter when you are subtracting times, so the fact that `to_time` converts to local time is irrelevant - they still represent the same (absolute) time as before. – Max Jun 18 '14 at 15:42
  • Sorry, I may be confusing this. Its not just the timezone that changes once the to_time is used. The entire Time object becomes the local system time. For example after the DateTime.strptime(...) function above the datetime produced looks like this "2014-06-18T15:44:27+00:00" which is correct (including converting 12 hour to 24 hour). As soon as I do the to_time function on that DateTime object it becomes this "2014-06-18T15:44:27+00:00". If I do a Time.new right after the to_time function the Time object is the same "2014-06-18T15:44:27+00:00" - which is what my local computer clock says. – Rj01 Jun 18 '14 at 15:49
  • What exactly is incorrect about converting "2014-06-18T15:44:27+00:00" to "2014-06-18T15:44:27+00:00"? – Max Jun 18 '14 at 15:57
  • I notice you have given the answer 22 seconds. I realise this however what I seek is the time difference between the two times in seconds. In this case it would be the total difference in the hours however expressed as seconds. So if it was 1pm at timezone 1 and 2pm at timezone 2 the seconds difference will be 3600. – Rj01 Jun 18 '14 at 16:02
  • Bad time for my copy to not work. DateTime = DateTime.strptime("06-18-2014 04:05:26 PM", '%m-%d-%Y %I:%M:%S %p') This results in 2014-06-18T16:05:26+00:00. When I do DateTime.to_time it results in 2014-06-19 02:05:26 +1000. Notice how the whole time changes? – Rj01 Jun 18 '14 at 16:07
  • Have you actually tried any of my code before commenting? That was my original answer to the question - just shift one time to match the timezone of the other! `date_time2.to_time - (date_time1 - date_time2.offset).to_time` – Max Jun 18 '14 at 16:08
  • Thank you, that worked. I did a search for offset and its the first time I see you mention it? Sorry about being a pain - thanks for the help! – Rj01 Jun 18 '14 at 16:28
  • Could you reward my patience by accepting my answer? – Max Jun 18 '14 at 16:40