11

When looking at the History.db from Safari, there's a table named history_visits which has a column named visit_time, which is a REAL value. It has values such as 470799793.096987. What format is that in? I'd like to see it in a format such as 12/08/2015 05:12:05.

3 Answers 3

28

It's the number in seconds since 00:00:00 UTC on 1 January 2001. It must be coming from an NSDate.

NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).

NSDate Class Reference

To get a decent human value out of it, you must add 978307200 (the epoch for 2001-01-01 00:00:00).

This query should give you what you want:

.headers on

select datetime(v.visit_time + 978307200, 'unixepoch', 'localtime') as date, v.visit_time + 978307200 as epoch, v.visit_time, i.domain_expansion, i.url
from history_items i left join history_visits v on i.id = v.history_item
order by i.id desc
limit 100;

Example output:

date|epoch|visit_time|domain_expansion|url
2015-12-31 11:51:27|1451562687.28465|473255487.284646|duckduckgo|https://duckduckgo.com/?q=current+timestamp+2015-12-31+11:51&t=osx

PS: Just for future reference, the Safari db file is located at ~/Library/Safari/History.db

2
  • What exactly is 978307200, the number of seconds?, what would 0 be in that space?
    – jxramos
    Mar 10, 2018 at 1:19
  • Just figured it out using this cool tool epochconverter.com, basically 0 is the Unix epoch, 0 seconds, and it maps to January 1, 1970 12:00:00 AM, and 1 maps to January 1, 1970 12:00:01 AM. In this coordinate space then 978307200 is the number of seconds that take you to the epoch date you give which is something Apple specific apparently.
    – jxramos
    Mar 10, 2018 at 1:24
1

I found the domain_expansion field to be null in some cases, here's a modified query:

SELECT SUBSTR(
SUBSTR(url, INSTR(url, '/')+2),
1,
INSTR(SUBSTR(url, INSTR(url, '/')+2),'/') - 1
) domain,
url,
datetime(hv.visit_time + 978307200, 'unixepoch', 'localtime') visit_time 
FROM history_items hi
JOIN history_visits hv on hi.id = hv.history_item;
-1

To convert the visit_time value in the history.db in an excel spread sheet, open the history.db file in a tool such as DB browser for SQLLite (Windows) and export the history_visits values to a CSV file.

Open the CSV file and create a column where you will populate your values in human readable time adjusted to your time zone, and use the following formula convert your NSDate:

=((((C2+978307200)/60)/60)/24)+DATE(1970,1,1)+(-5/24)

In the above formula, the time value is in cell C2, and my time zone GMT-5. To adjust to your own time zone adjust the statement in the last set of parenthesis. Presently I have (-5/24) to represent GMT-5.

When I first approached this conversion, I mistakenly assumed the time in the history.db to be epoch time, which starts at 1/1/1970, and did not understand why there was such a skew in time. Adding the required conversion factor +978307200 solved the problem.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.