Search code examples
c#winformsbrowser-history

Get time spent on each website from browser history using C#


I have written code to fetch browser's history of websites visited by a user on his machine using Winforms C#.Net

However, I also need to fetch time he spent on each website, for example, output should be:

John, www.stackoverflow.com , 20 minutes.

Read History file using SQLIte connenction and populate datatable. Used approach from this link - https://social.msdn.microsoft.com/Forums/vstudio/en-US/3e9f3588-ad0b-49af-b269-2abfda0b9abc/how-to-get-the-browser-history-in-program-using-c?forum=csharpgeneral

But 'Visit_duration' column of table 'Visits' does not provide time spent on each website. Refer red circle in attached screenshot.

Please advise if I am using the correct column. If yes, how would I convert this column to get exact time in hours or minutes

enter image description here

enter image description here

Edits:

While @imsmn 's reply is good, but I also need a Sum of "Visit_duration" . Note that "URL" column of Table 2(Visits) matches "ID" column of Table 1(urls). So, I want Totalsum of Table 2 "Visit_duration" grouped on Table 2 "URL". The matching 'Where clause is, table1["ID") = table2["URL") . Kindly help


Solution

  • last_visit_time and visit_time (actually all timestamps) are stored in microseconds since 1601-01-01. Seems like that's default format for WebKit timestamps (chrome previously used webkit).

    That means, all you have to do is adding the given microseconds to the base date.

    DateTime baseDate = new DateTime(1601, 01, 01);
    DateTime lastVisitDate = baseDate.AddSeconds(13268117172386444 / 1000000); // Convert to seconds by dividing with 1.000.000
    

    With that example the result would be 14.06.2021 04:06:12.

    Calculating the duration is just as easy - add the microseconds and subtract the base date.

    TimeSpan duration = baseDate.AddSeconds(811945003 / 1000000).Subtract(baseDate);
    

    In this case, the user spent 00:13:31 on the website.