WebKit / Chrome时间戳转换为Ruby / Rails

问题描述 投票:1回答:1

如何将WebKit / Chrome时间戳转换为Ruby / Rails。

这是来自Chrome excel 13130755192116927的时间戳数据,但我如何使用Ruby / Rails转换为人类可读的格式。

我找到了一些像How to convert a unix timestamp (seconds since epoch) to Ruby DateTime?这样的例子,但这个数据长度是13,我的数据长度是17。

我如何实现这一点就像这个WebKit/Chrome Timestamp Converter

GMT: Sunday, February 5, 2017 7:59:52 AM

谢谢。

ruby-on-rails ruby unix-timestamp epoch
1个回答
2
投票

来自this question

Google时间戳的格式为自1601年1月以来的微秒数

这是一个Ruby示例:

require 'date'

chrome_timestamp = 13130755192116927

# Get the January 1601 unixepoch
since_epoch = DateTime.new(1601,1,1).to_time.to_i

# Transfrom Chrome timestamp to seconds and add 1601 epoch
final_epoch = (chrome_timestamp / 1000000) + since_epoch

# Print DateTime
date = DateTime.strptime(final_epoch.to_s, '%s')

# without formating
puts date
=> '2017-02-05T07:59:52+00:00'

# with formating
puts date.strftime('%A, %B %-d, %Y %-I:%M:%S %p')
=> 'Sunday, February 5, 2017 7:59:52 AM'
© www.soinside.com 2019 - 2024. All rights reserved.