Search code examples
ruby-on-railsrubyunix-timestampepoch

WebKit/Chrome Timestamp convert into Ruby/Rails


How to convert WebKit/Chrome Timestamp into Ruby/Rails.

Here is the timestamp data from Chrome excel 13130755192116927 but how I convert into the human-readable format using Ruby/Rails.

I have found some examples like How to convert a unix timestamp (seconds since epoch) to Ruby DateTime? but this data length is 13 & my data length is 17.

How I achieve that as like this WebKit/Chrome Timestamp Converter.

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

Thanks.


Solution

  • From this question

    Google timestamp is formatted as the number of microseconds since January, 1601

    So here an Ruby example :

    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'