【问题标题】:In ruby, how to convert epoch milli seconds into Time Object without losing milli seconds information?在ruby中,如何将纪元毫秒转换为时间对象而不丢失毫秒信息?
【发布时间】:2016-11-17 12:00:39
【问题描述】:

我想在不丢失毫秒信息的情况下将纪元毫秒转换为时间。可行吗?

【问题讨论】:

  • 举例说明你的时间格式

标签: ruby date time


【解决方案1】:

您应该使用Time.at 然后usec 方法来获取微秒时间。

epoch_milli = 1479383961245
t = Time.at(epoch_milli / 1000.0) # => 2016-11-17 09:59:21 -0200
micro = t.usec                    # => 244999
milli = micro / 1000.0            # => 244.999

【讨论】:

  • 你可以使用seconds, milliseconds = epoch_milli.divmod(1000); Time.at(seconds, milliseconds * 1000)来避免浮点错误。
  • @Stefan 或者,Time.at(epoch_milli / 1000r)(至少在 Ruby 2.3.1 中工作)。
  • @Jordan 不错! Time.at(epoch_milli.quo(1000)) 也可以。
  • @Stefan 哦,这很方便。我必须记住这一点。
猜你喜欢
  • 2012-03-08
  • 2015-07-07
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 2018-11-16
  • 1970-01-01
相关资源
最近更新 更多