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.
相关问题
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- Display time in local timezone when querying Influ
- ruby 1.9 wrong file encoding on windows
相关文章
- Ruby using wrong version of openssl
- How to truncate seconds in TSQL?
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- How to remove seconds from datetime?
- Segmentation fault with ruby 2.0.0p247 leading to
- OLS with pandas: datetime index as predictor
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:
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:
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")
To convert from string form to a date or time object you need to use
strptime
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.