17

I get a string from a external method with a time and date like so "07/09/10 14:50" is there any way I can convert that time in ruby to 'Pacific US' time knowing its 'UTC' time? with changes accounted for in the date? I.e if the time difference results in the day being different.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Mo.
  • 40,243
  • 37
  • 86
  • 131
  • Please refer to the one I posted https://stackoverflow.com/questions/24577449/convert-time-to-other-timezone/44278155#44278155 – Kevin Li May 31 '17 at 07:22

3 Answers3

45

Since it appears you are using rails, you have quite a few options. I suggest reading this article that talks all about time zones.

To convert to PST, both of these are rails-specific methods. No need to re-invent the wheel:

time = Time.parse("07/09/10 14:50")
time.in_time_zone("Pacific Time (US & Canada)")

Hope this helps

UPDATE: rails might try to get smart and give the time you specify as a string a time zone. To ensure that the time parses as UTC, you should specify in the string:

time = Time.parse("07/09/10 14:50 UTC")
time.in_time_zone("Pacific Time (US & Canada)")
Geoff Lanotte
  • 7,490
  • 1
  • 38
  • 50
  • i had a look at the article but it dosent show how i can convert from string form to date or time object? the method dosent work if i call it on the string "07/09/10 14:50" – Mo. Jul 09 '10 at 17:02
  • I added the `Time.parse` statement to get the time from a string. – Geoff Lanotte Jul 09 '10 at 18:36
14

To convert from string form to a date or time object you need to use strptime

require 'date'
require 'time'

my_time_string = "07/09/10 14:50"
to_datetime = DateTime.strptime(my_time_string, "%m/%d/%y %H:%M")    

utc_time = Time.parse(to_datetime.to_s).utc
pacific_time = utc_time + Time.zone_offset("PDT")

puts utc_time
puts pacific_time

This is pure ruby, so there are likely some rails-specific methods you could use specifically for this task, but this should get you started.

michaelmichael
  • 13,755
  • 7
  • 54
  • 60
  • 1
    A small note, you should probably use `utc_time + Time.zone_offset("PDT")` instead, if the zone offset is negative, e.g. -18000" it will do "minus minus", which results in a positive number, and thus will result in addition which is not what you want. – Ole May 13 '14 at 14:44
  • I'm wondering if `PST` wouldn't be a better choice considering `PDT` returns the daylight time and `PST` returns the standard time. Currently, converting a `UTC` time with the `PDT` offset would not return the actual pacific time. (Example: it is currently 1pm UTC. The conversion using PDT would give you 6am, but PST would give you 5am, which is probably what you expect). – Pauline Nov 28 '17 at 13:19
-5

require 'time'

If you want pacific time just subtract 25200 seconds from Time.new There is a 7 hour difference between pacific time and GMT.

t = Time.parse((Time.new-25200).to_s)

Then use strftime to format however you want it to look.

puts t.strftime("%A %D at %I:%M%p")

Community
  • 1
  • 1
  • 1
    It is very discouraged to handle time zones manually. Elanguage has the best tools for that. Did you consider the DST? Leaps? – Amr Eladawy Apr 01 '17 at 03:59
  • Not only discouraged, but in this case DEAD WRONG. Subtracting a fixed time from a date to convert timezones ignores Daylight savings – Scott Apr 02 '17 at 12:42
  • I just realized why the above code was not working for me. I am using cloud 9 server must be in another time zone. I added the Ruby language to eclipse and then added the following code and it gives me the current time in PST. time = Time.new() puts time.strftime("%I:%M:%S") – Mike Futala May 06 '17 at 22:18
  • 3
    Thanks for the inputs anyway... glad to see everyone is so passionate about the programing !!! :) – Mike Futala May 06 '17 at 22:21