Ruby

Convert WebKit/Chrome timestamps to human readable date & Unix time in Ruby/Rails

Convert WebKit/Chrome timestamps to human readable date & Unix time in Ruby/Rails.

This timestamp format is used in web browsers such as chrome & safari, So how we convert human readable date & Unix time in Ruby/Rails.

Chrome’s base time is 01/01/1601 00:00:00. To calculate local time, Chrome time has to be converted to seconds by dividing by one million, and then the seconds differential between 01/01/1601 00:00:00 and 01/01/1970 00:00:00 must be subtracted.

Q. What is the significance of January 1, 1601 00:00:00?

A. The Gregorian calendar operates on a 400-year cycle, and 1601 is the first year of the cycle that was active at the time Windows NT was being designed. In other words, it was chosen to make the math come out nicely. Reference.

Now we will declare a method which can convert this WebKit/Chrome timestamps to human readable date & Unix time with Ruby/Rails

def convert_unix_epoch_timestamp(webkit_timestamp)
	unix_epoch_timestamp = webkit_timestamp.to_i
	# 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 = (unix_epoch_timestamp / 1000000) + since_epoch
	# Print DateTime
	date = DateTime.strptime(final_epoch.to_s, '%s')
	return date
end

Now call this with timestamps data

convert_unix_epoch_timestamp(13130918479860115).strftime('GMT: %A, %B %-d, %Y %-I:%M:%S %p')
#=> GMT: Tuesday, February 7, 2017 5:21:20 AM

here we go!

WebKit/Chrome Timestamp

Fool Dev

I didn't get trained by the school system like other kids, and when I did concentrate on learning, my mind was cluttered and locked by the programming of the system. © Huey Newton