Search code examples
javascriptpythonbashgoogle-chromewebkit

Convert current time to WebKit/Chrome 17-digit timestamp


I'm writing some bash script that parses and modifies multiple preferences and bookmarks of Google Chrome in OS X. Some of the values require 17-digit WebKit timestamp to be provided. For now I've hardcoded it with 13116319200000000 which corresponds to beginning of my workday this Monday in CEST, but it would be great to have this somehow calculated from current system time, as the script will be deployed at different times.

WebKit timestamp is a 64-bit value for microseconds since Jan 1, 1601 00:00 UTC. Such conversion in bash on OS X might be impossible, as the built-in version of data does not support resolution higher than a second (like %N), but maybe some highly-intelligent math would do.


Anyway, I've searched the web and found multiple examples on how to convert such timestamp to human readable time, but not the other way around. The best example would be this website, that lists python script from some dpcnull user:

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
inTime = int(raw_input('Enter a Webkit timestamp to convert: '))
date_from_webkit(inTime)

and also has this JavaScript on form's action (where document.we.wk.value points to that form):

function WebkitToEpoch() {
    var wk = document.we.wk.value;
    var sec = Math.round(wk / 1000000);
    sec -= 11644473600;
    var datum = new Date(sec * 1000);
    var outputtext = "<b>Epoch/Unix time</b>: " + sec;
    outputtext += "<br/><b>GMT</b>: " + datum.toGMTString() + "<br/><b>Your time zone</b>: " + datum.toLocaleString();
    $('#resultle1').html(outputtext);
}

that looks like it could be easily inverted, but I failed trying.

Interestingly, the site shows current WebKit timestamp, but after examination I believe it's calculated in PHP on server level, so there's no access to it.

I'd be glad if someone could help me with the script I could embed in mine.


Note: Although Google Chrome uses all 17-digits with precise microseconds, I don't really need such resolution. Just like on the linked website, rounding to seconds with last six digits being zeros is perfectly fine. The only important factor - it has to calculate properly.


Solution

  • Something like this?

    from datetime import datetime, timedelta
    
    
    def date_from_webkit(webkit_timestamp):
        epoch_start = datetime(1601, 1, 1)
        delta = timedelta(microseconds=int(webkit_timestamp))
        return epoch_start + delta
    
    
    def date_to_webkit(date_string):
        epoch_start = datetime(1601, 1, 1)
        date_ = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
        diff = date_ - epoch_start
        seconds_in_day = 60 * 60 * 24
        return '{:<017d}'.format(
            diff.days * seconds_in_day + diff.seconds + diff.microseconds)
    
    
    # Webkit to date
    date_from_webkit('13116508547000000')  # 2016-08-24 10:35:47
    
    # Date string to Webkit timestamp
    date_to_webkit('2016-08-24 10:35:47')  # 13116508547000000