将UTC中的时间转换为太平洋时间

时间:2022-05-17 02:02:49

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.

我从一个外部方法获得一个字符串,其时间和日期为“07/09/10 14:50”,我是否可以将ruby中的时间转换为‘Pacific US’时间,知道它的‘UTC’时间?变更的原因是什么?我。如果时差导致一天的不同。

3 个解决方案

#1


36  

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

因为看起来您正在使用rails,所以您有许多选项。我建议阅读这篇关于时区的文章。

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

要转换为PST,这两种方法都是特定于rails的方法。无需重新发明*:

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:

更新:rails可能会尝试变得更聪明,并将时间指定为一个时区的字符串。为了确保时间解析为UTC,您应该在字符串中指定:

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

#2


13  

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

要将字符串形式转换为日期或时间对象,需要使用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.

这是纯粹的ruby,因此您可以为这个任务特别使用一些特定于rails的方法,但是这将使您开始学习。

#3


-4  

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)

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")

将t。strftime(“% % D %我:% M % p”)

#1


36  

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

因为看起来您正在使用rails,所以您有许多选项。我建议阅读这篇关于时区的文章。

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

要转换为PST,这两种方法都是特定于rails的方法。无需重新发明*:

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:

更新:rails可能会尝试变得更聪明,并将时间指定为一个时区的字符串。为了确保时间解析为UTC,您应该在字符串中指定:

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

#2


13  

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

要将字符串形式转换为日期或时间对象,需要使用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.

这是纯粹的ruby,因此您可以为这个任务特别使用一些特定于rails的方法,但是这将使您开始学习。

#3


-4  

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)

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")

将t。strftime(“% % D %我:% M % p”)