Parse Timestamp with milliseconds from HTTP GET request

8 views (last 30 days)
Hi. I am using a REST api where I get timestamps looking like this:
12(1),2022-09-15 00:00:00,2022-09-15 00:16:09.264,2022-09-14 00:00:00,94.100000000000
12(1),2022-09-15 19:44:50.559,2022-09-15 19:44:50.650,2022-09-15 00:00:00,90.840000000000
12(1),2022-09-16 19:47:06.936,2022-09-16 19:47:07.113,2022-09-16 00:00:00,91.350000000000
12(1),2022-09-19 19:50:21.285,2022-09-19 19:50:21.455,2022-09-19 00:00:00,92.000000000000
12(1),2022-09-20 19:46:47.152,2022-09-20 19:46:47.298,2022-09-20 00:00:00,90.620000000000
When reading this using matlab.net.http.RequestMessage and acceptType as text/csv, the timestamps with millisecond precision are just converted to "NaT".
MATLAB gives:
12(1),2022-09-15 00:00:00,NaN,2022-09-14 00:00:00,94.1
12(1),NaT,NaN,2022-09-15 00:00:00,90.84
12(1),NaT,NaN,2022-09-16 00:00:00,91.35
12(1),NaT,NaN,2022-09-19 00:00:00,92
12(1),NaT,NaN,2022-09-20 00:00:00,90.62
How can I control this parsing so it understand the timestamps?
According to this: HTTP Data Type Conversion - MATLAB & Simulink - MathWorks Nordic , it uses "readtable" underneath. This is what I want. If I try readtable locally from a file, and specify "datetime" in delimitedTextImportOptions as the VariableType, then the conversion works. So the format is recognized.
Surely there must be some way to control this so it is always recognized...

Answers (1)

Suraj
Suraj on 18 Sep 2023
Greetings Mikael,
I understand that you're trying to parse an API response of type "text/csv". Your investigation on the same is correct, MATLAB HTTP Interface does use "readtable" function under-the-hood to convert CSV responses to a table.
I tried reproducing the behaviour you've described on my end, and it appears that the issue could be with the way your response is structured. If you look closely, the second column of the first line in your CSV data does not contain any millisecond data, whereas all the other values under the second column include millisecond data. Conversion to “datetime” is performed column-wise in the "readtable" function, so the date/time format of the first entry in the column is adopted when parsing all other entries of the same column.
Consider a sample API response as below:
12(1),2022-09-15 00:00:00.000,2022-09-15 00:16:09.264,2022-09-14 00:00:00,94.100000000000
12(1),2022-09-15 19:44:50.559,2022-09-15 19:44:50.650,2022-09-15 00:00:00,90.840000000000
12(1),2022-09-16 19:47:06.936,2022-09-16 19:47:07.113,2022-09-16 00:00:00,91.350000000000
12(1),2022-09-19 19:50:21.285,2022-09-19 19:50:21.455,2022-09-19 00:00:00,92.000000000000
12(1),2022-09-20 19:46:47.152,2022-09-20 19:46:47.298,2022-09-20 00:00:00,90.620000000000
Here the date/time format is uniform for the entire column, and MATLAB should parse it correctly as shown below:
Var1 Var2 Var3 Var4 Var5
_________ _______________________ _______________________ ___________________ _____
{'12(1)'} 2022-09-15 00:00:00.000 2022-09-15 00:16:09.264 2022-09-14 00:00:00 94.1
{'12(1)'} 2022-09-15 19:44:50.559 2022-09-15 19:44:50.650 2022-09-15 00:00:00 90.84
{'12(1)'} 2022-09-16 19:47:06.936 2022-09-16 19:47:07.113 2022-09-16 00:00:00 91.35
{'12(1)'} 2022-09-19 19:50:21.285 2022-09-19 19:50:21.455 2022-09-19 00:00:00 92
{'12(1)'} 2022-09-20 19:46:47.152 2022-09-20 19:46:47.298 2022-09-20 00:00:00 90.62
This behaviour remains consistent when "readtable" is used directly, or as a part of another function like in the HTTP interface functions.
I suggest that you maintain a uniform format for date/time in your input to achieve this result.
I hope this helps.
Best regards,
Suraj.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!