从整数(具有毫秒精度的 unix 纪元时间)和指定区域(字符串)创建 TimeWithZone 对象
Create TimeWithZone object from integer (unix epoch time with millisecond precision) and with specified zone (string)
我有一个整数,表示以毫秒为单位的 unix 纪元时间和一个时区字符串。我需要用它们创建一个 TimeWithZone 对象。
epoch_ms_integer = 1586653140000
time_zone = "America/Los_Angeles"
正在尝试转换为:
Sat, 11 Apr 2020 20:59:00 PDT -07:00
我能够通过以下方式完成此任务:
Time.at(epoch_ms_integer/1000).asctime.in_time_zone("America/Los_Angeles")
但想知道这是否是实现此目标的最佳方法。我正在使用的应用程序配置为 EST/EDT 时区,因此 Time.at(epoch_ms_integer/1000)
returns 2020-04-11 20:59:00 -0400
.
我能够在此处的一个答案中找到 asctime 解决方案 Ruby / Rails - Change the timezone of a Time, without changing the value
这里问了同样的问题,但没有答案 converting epoch time with milliseconds to datetime。
假设时间戳是毫秒,那么1586653140000就是
Epoch: 1586653140
GMT: Sunday, April 12, 2020 12:59:00 AM
PDT: Saturday, April 11, 2020 17:59:00 PM -in time zone America/Los Angeles
这些只是指代世界各地特定时间点的 3 种不同方式。
2020 年 4 月 11 日星期六 20:59:00 PDT -07:00 和 2020-04-11 20:59:00 -0400 分别指代不同的时间点,与纪元 (1586653140)
不同
由于 Unix 纪元(或 Unix 时间或 POSIX 时间或 Unix 时间戳)是自 1970 年 1 月 1 日(午夜 UTC/GMT)以来经过的秒数,它不会采用 1586653140
并仅更改时区而不添加时区偏移量是有意义的,因为现在您正在谈论另一个时间点。
要获得从纪元到任何时区的正确"translation",你可以做到
Time.zone = "GMT"
Time.zone.at(1586653140)
=> Sun, 12 Apr 2020 00:59:00 GMT +00:00
Time.zone = "America/Los_Angeles"
Time.zone.at(1586653140)
=> Sat, 11 Apr 2020 17:59:00 PDT -07:00
在 rails 的时区处理日期时,重要的是只使用考虑设置时区的函数:
不要使用
- Time.now
- Date.today
- Date.today.to_time
- Time.parse("2015-07-04 17:05:37")
- Time.strptime(字符串, "%Y-%m-%dT%H:%M:%S%z")
请使用
- Time.current
- 2.hours.ago
- Time.zone.today
- Date.current
- 1.day.from_now
- Time.zone.parse("2015-07-04 17:05:37")
- Time.strptime(字符串, "%Y-%m-%dT%H:%M:%S%z").in_time_zone
还要记住,在 Rails 应用中,我们有三个不同的时区:
- 系统时间,
- 申请时间,
- 数据库时间。
This post by thoughtbot 解释得很清楚。
我有一个整数,表示以毫秒为单位的 unix 纪元时间和一个时区字符串。我需要用它们创建一个 TimeWithZone 对象。
epoch_ms_integer = 1586653140000
time_zone = "America/Los_Angeles"
正在尝试转换为:
Sat, 11 Apr 2020 20:59:00 PDT -07:00
我能够通过以下方式完成此任务:
Time.at(epoch_ms_integer/1000).asctime.in_time_zone("America/Los_Angeles")
但想知道这是否是实现此目标的最佳方法。我正在使用的应用程序配置为 EST/EDT 时区,因此 Time.at(epoch_ms_integer/1000)
returns 2020-04-11 20:59:00 -0400
.
我能够在此处的一个答案中找到 asctime 解决方案 Ruby / Rails - Change the timezone of a Time, without changing the value
这里问了同样的问题,但没有答案 converting epoch time with milliseconds to datetime。
假设时间戳是毫秒,那么1586653140000就是
Epoch: 1586653140
GMT: Sunday, April 12, 2020 12:59:00 AM
PDT: Saturday, April 11, 2020 17:59:00 PM -in time zone America/Los Angeles
这些只是指代世界各地特定时间点的 3 种不同方式。 2020 年 4 月 11 日星期六 20:59:00 PDT -07:00 和 2020-04-11 20:59:00 -0400 分别指代不同的时间点,与纪元 (1586653140)
不同由于 Unix 纪元(或 Unix 时间或 POSIX 时间或 Unix 时间戳)是自 1970 年 1 月 1 日(午夜 UTC/GMT)以来经过的秒数,它不会采用 1586653140
并仅更改时区而不添加时区偏移量是有意义的,因为现在您正在谈论另一个时间点。
要获得从纪元到任何时区的正确"translation",你可以做到
Time.zone = "GMT"
Time.zone.at(1586653140)
=> Sun, 12 Apr 2020 00:59:00 GMT +00:00
Time.zone = "America/Los_Angeles"
Time.zone.at(1586653140)
=> Sat, 11 Apr 2020 17:59:00 PDT -07:00
在 rails 的时区处理日期时,重要的是只使用考虑设置时区的函数:
不要使用
- Time.now
- Date.today
- Date.today.to_time
- Time.parse("2015-07-04 17:05:37")
- Time.strptime(字符串, "%Y-%m-%dT%H:%M:%S%z")
请使用
- Time.current
- 2.hours.ago
- Time.zone.today
- Date.current
- 1.day.from_now
- Time.zone.parse("2015-07-04 17:05:37")
- Time.strptime(字符串, "%Y-%m-%dT%H:%M:%S%z").in_time_zone
还要记住,在 Rails 应用中,我们有三个不同的时区:
- 系统时间,
- 申请时间,
- 数据库时间。
This post by thoughtbot 解释得很清楚。