How to convert time in microseconds (queryperformancecounter(qpc)),import from an excel file, to time (hh:mm:ss)?
Show older comments
Hi!
I saved some data from an eye tracker and imported it into matlab, among these I am interested in the time stamp...I would like to understand how to transform the computer time stamp ('the Computer timestamp column contains the value of the win32clock "QueryPerformanceCounter"(QPC) in microseconds') which is in microseconds into a hh:mm:ss format, does anyone know how to do this ?
for example i want to convert this number "168892633108" in microseconds that it would be 04/05/2023 16:14:45
Thank you
5 Comments
Star Strider
on 31 May 2023
I can’t find anything in the Micro$oft documentation that tells how to convert that to something usable.
Good luck!
Stephen23
on 31 May 2023
The QPC gives only the relative time since Windows started. It is not synchronized with absolute time.
So at first glance, what you request is not possible... unless you happen to have stored a refernce absolute time for some time for which you also know the exact QPC count. Then what you request might be possible.
Giada
on 1 Jun 2023
Giada
on 3 Jun 2023
Accepted Answer
More Answers (2)
Diwakar Diwakar
on 31 May 2023
Try this code:
% Example timestamp value in microseconds
timestamp_microseconds = 168892633108;
% Convert microseconds to seconds
timestamp_seconds = timestamp_microseconds / 1e6;
% Extract the number of whole seconds
whole_seconds = floor(timestamp_seconds);
% Calculate the remaining microseconds
microseconds = timestamp_microseconds - (whole_seconds * 1e6);
% Convert the number of whole seconds to a date string
date_string = datestr(whole_seconds/86400 + datenum(1970,1,1), 'dd/mm/yyyy HH:MM:SS');
% Display the result
disp(date_string);
1 Comment
Avoid deprecated DATESTR and DATENUM.
For nearly ten years MATLAB has recommended using DATETIME instead.
Also note that this answer does not consider the nature of the QPC counter: https://www.mathworks.com/matlabcentral/answers/1976229-how-to-convert-time-in-microseconds-queryperformancecounter-qpc-import-from-an-excel-file-to-tim#comment_2766299
You can convert a number of microseconds into a duration or (if you know the epoch time) into a date and time value.
M = 168892633108;
msPerSec = 1e6;
dur = seconds(M/msPerSec)
You can change the display format of the duration array to see it in a different style. This doesn't change how the data is stored, just how it is displayed.
dur.Format = 'hh:mm:ss.SSSSSS'
If I knew the epoch time (when the duration was relative to), for example 9 AM today:
E = datetime('today') + hours(9)
I could create a datetime array using M and E.
dt = datetime(M, 'ConvertFrom', 'epochtime', 'Epoch', E, 'TicksPerSecond', msPerSec)
M represents a little under two days (as we can see from dur) and dt is a little under two days after E, so this seems reasonable.
4 Comments
Giada
on 1 Jun 2023
Steven Lord
on 1 Jun 2023
Also because the date with that number in microseconds should be 04/05/2023 as I specified in the question.
How exactly do you know that? What relationship is there between 168892633108 and 04/05/2023?
The problem is that number in microseconds corresponds to a relative time (QCP, that is a computer stamp from an eye tracker) independent of UTC, which is apparently relative to the avivio of windows....so I wanted to know if there was a way from matlab to access this time, could you help me in this way?
So you don't know the epoch time, you're trying to find it? How and/or where is it stored? In a file as data, in the Windows registry somewhere, the creation time of a file (quite fragile), etc.?
I don't know what "avivio of windows" means and searching for "avivio" in Google didn't show any hits that appeared relevant.
Giada
on 2 Jun 2023
Steven Lord
on 2 Jun 2023
Okay. How are you importing the spreadsheet into MATLAB? Are you using the Import Tool to read the data (optionally also creating a MATLAB function file to automate the process)? If so select that you want to import those columns (the ones whose names start with Recording date and/or Export date) from the spreadsheet file as datetime arrays then use the datetime command I used (with 'epochtime' and 'TicksPerSecond').
If you're reading in manually, without using Import Tool, use the import options to control how MATLAB reads the data from the spreadsheet.
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!