Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse Chrome bookmarks "date_added" value to a Date

The Chrome bookmarks file is JSON which contains a "date_added" value that represents a particular date and time, e.g.

{
 "checksum": "05b8bba8b5f0e9ad1cc8034755557735",
 "roots": {
    "bookmark_bar": {
       "children": [ {
          "children": [ {
             "date_added": "13170147422089597",
             "id": "121",
             "name": "NativeScript: Getting Started Guide",
             "type": "url",
             "url": "https://docs.nativescript.org/tutorial/chapter-0"
          } ],
...

I have tried treating the value as nanoseconds and passing to the Date constructor:

new Date(13170147422089597 / 1000); // 2387-05-07T06:17:02.089Z

but that doesn't seem correct.

How should the value "13170147422089597" be converted to a Date or date string?

like image 867
RobG Avatar asked Jul 14 '18 22:07

RobG


4 Answers

Just for those who need the same algorithm in .NET / PowerShell, here is the implementation in PowerShell (the .NET version should be then pretty straightforward):

$timeValue = 13268127530603048
$epoch = (New-Object DateTime(1601, 1, 1)).Ticks
$utcTime = New-Object DateTime(($epoch + $timeValue * 10), [System.DateTimeKind]::Utc)
$utcTime.ToLocalTime()
like image 162
pholpar Avatar answered Oct 16 '22 23:10

pholpar


The Chrome bookmarks time value is microseconds from an epoch of 1601-01-01T00:00:00Z. To convert to a Date:

  1. Divide by 1,000 to get milliseconds
  2. Adjust to an epoch of 1970-01-01T00:00:00Z
  3. Pass the resulting value to the Date constructor

E.g.

var timeValue = '13170147422089597';
new Date(Date.UTC(1601,0,1) + timeValue / 1000); // 2018-05-07T06:17:02.089Z

Storing the value Date.UTC(1601,0,1) as a constant (-11644473600000) and converting to a function gives:

function chromeTimeValueToDate(tv) {
  var epoch = -11644473600000;
  return new Date(epoch + tv / 1000);
}

// Example
['13170147422089597',
 '13150297844686316',
 '13115171381595644'].forEach( tv => {
   console.log(chromeTimeValueToDate(tv))
});
like image 35
RobG Avatar answered Oct 17 '22 00:10

RobG


It's WebKit/Chrome Timestamp, representing microseconds since 1601/1/1 UTC.

It comes from Windows NT Timestamp (as 100-nanoseconds since 1601/1/1 UTC), available as FileTime structure.

There is an online WebKit/Chrome Timestamp Converter in https://www.epochconverter.com/webkit

which provides its code there as well.

However the code is in Python2, so I transcribe it to Python3 as below:

import datetime
def date_from_webkit(webkit_timestamp):
    epoch_start = datetime.datetime(1601,1,1)
    delta = datetime.timedelta(microseconds=int(webkit_timestamp))
    print(epoch_start + delta) # py3 requires () for print

date_from_webkit(int(input('Enter a Webkit timestamp to convert: '))) # py3 integrates raw_input() and input() into input()
like image 44
Charles Jie Avatar answered Oct 17 '22 00:10

Charles Jie


I like to prototype these things in a spreadsheet and then it should be trivial to code the coded formula in whichever language you happen to be working with.

Formula = "2001-1-1" + A2/24/60/60/1000000 - 400*365.25+3+7/24

Explanation:

  • Starting with an arbitrary date that Excel (what I'm using as a spreadsheet) can handle... Jan1, 2001
  • Add the bookmark's data value [date_added] or [last_visited_desktop] or [date_modified] (in cell A2 in my formula above) in terms of days (24 hours by 60 minutes by 60 seconds by a millionth of a second
  • subtract 400 years in terms of days (that's 400 years including leap year days and 7 hours worth of clock adjustments since by 2001 date above, because somehow COBOL developers got stuck on year 1601, which became year 1 for them, so they made that their 'epoch'. Funny how things get started, eh?)

For example, Chrome's Bookmark [date_added] value is "13190650905699900", so: "2001-1-1"+ 13190650905699900/24/60/60/1000000 - 400*365.25+3+7/24 yields 12/30/2018 8:41:46pm

You can find the Bookmarks file here:

%LOCALAPPDATA%\Google\Chrome\User Data\Default\Bookmarks

as of current version of Chrome (v71)

PS - Window Time utility can do it for you too if all you want is a quick one-time conversion: type

w32tm.exe /ntte 131906509056999000

at a command prompt - notice I had to add a '0' (or multiply by 10).

like image 33
Christian dlP Avatar answered Oct 17 '22 00:10

Christian dlP