posix/unix time to datetime

조회 수: 149 (최근 30일)
Pablo Armañac Julián
Pablo Armañac Julián 2023년 3월 20일
댓글: Pablo Armañac Julián 2023년 3월 20일
Hi!
I just purchased a device to register some signals and I need to know the TimeStamp of the samples.
The program gives me two options to export the data (.csv and .mat).
The problem comes with the .mat format. I choose to export the data synchronized, with unix format, and I don't get the same datetime as if I export it in csv (which is the datetime that I know is correct). For convenience I need to export the data into a .mat, and also I want to know why I don't get the same datetimes. I've been looking for this and can't find the solution. BTW, I need millisecond resolution.
For example:
0) From the .csv (which I know it is correct), the datetime samples are: (var: TimestampSync_FormattedUnix_CAL)
2023/03/20 12:06:04.100
2023/03/20 12:06:04.102
2023/03/20 12:06:04.104
...
1) From the .mat I get: ( var: TimestampSync_Unix_CAL)
1679310364100.53
1679310364102.48
1679310364104.43
...
2) Which I try to convert to datetime as:
formatted_timeStamp = datetime ( TimestampSync_Unix_CAL , 'convertfrom','posixtime' , 'Format','dd-MMM-yyyy HH:mm:ss.SSS');
and the result is:
'24-Mar-55185 13:08:20.526'
'24-Mar-55185 13:08:22.480'
'24-Mar-55185 13:08:24.433'
...
I attach the variables I'm using in case it helps.
Thank you very much in advance

채택된 답변

Stephen23
Stephen23 2023년 3월 20일
편집: Stephen23 2023년 3월 20일
Unix time is actually defined as the number of seconds since the epoch. The times you show are the milliseconds since the epoch. MATLAB uses the standard UNIX/POSIX definition.
You can easily make the conversion yourself, here are two approaches:
V = [1679310364100.53;1679310364102.48;1679310364104.43];
D = datetime(V/1000, 'convertfrom','posixtime', 'Format','dd-MMM-yyyy HH:mm:ss.SSS')
D = 3×1 datetime array
20-Mar-2023 11:06:04.100 20-Mar-2023 11:06:04.102 20-Mar-2023 11:06:04.104
E = datetime(1970,1,1);
D = datetime(V, 'convertfrom','epochtime', 'Epoch',E,'TicksPerSecond',1000, 'Format','dd-MMM-yyyy HH:mm:ss.SSS')
D = 3×1 datetime array
20-Mar-2023 11:06:04.100 20-Mar-2023 11:06:04.102 20-Mar-2023 11:06:04.104
  댓글 수: 3
Steven Lord
Steven Lord 2023년 3월 20일
I wouldn't expect you to need to include an extra hour in the data. Perhaps this is a time zone effect? Or maybe Daylight Savings Time?
Pablo Armañac Julián
Pablo Armañac Julián 2023년 3월 20일
Yes, now everything makes sense and 'TimeZone' works
added: " 'TimeZone','Europe/Madrid' " , instead of "+hours(1)"
TimestampSync_FormattedUnix_CAL = datetime ( TimestampSync_Unix_CAL./1000 , 'convertfrom','posixtime' , 'TimeZone','Europe/Madrid' , 'Format','yyyy/MMM/dd HH:mm:ss.SSS') ;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by