Handling datetime from exported csv file

Jichao Fang on 12 Oct 2021
Latest activity Reply by Jichao Fang on 13 Nov 2021

I have both of my account profile and channel export time zones set to "(GMT+8000)Beijing". The datetime from the exported feeds.csv looks like:

'2021-09-06 11:16:28 CST'

I want to import data, but MATLAB do not directly understand its datetime format. I assume "CST" to be "China Standard Time", but when I try to convert it to datetime, MATLAB throws an error:

 >> datetime(s,'InputFormat',"yyyy-MM-dd HH:mm:ss z","TimeZone","Asia/Shanghai","Locale","zh_CN")
 Error using datetime (line 651)
 Unable to convert '2021-09-06 11:16:28 JST' to datetime using the format 'yyyy-MM-dd HH:mm:ss z' and locale 'zh_CN'.

However if I change the "Locale" to US instead:

>>datetime(s,'InputFormat',"yyyy-MM-dd HH:mm:ss z","TimeZone","Asia/Shanghai","Locale","en_US")
ans = 
    datetime
07-Sep-2021 01:16:28

There is no error, but the time is wrong and is 14 hours ahead. So I guess that MATLAB sees this "CST" as "Central Standard Time".

My point is:

  1. It's pretty strange that the datetime exported by ThingSpeak could not be directly understood by MATLAB. MATLAB is supposed to work with ThingSpeak data seamlessly, but now I have to find a detour to import ThingSpeak data to MATLAB.
  2. It seems that the datetime function needs improvement to understand this "CST" timezone, or that ThingSpeak could improve its datetime output format.
Vinod
Vinod on 12 Oct 2021

Thank you for sharing. That is good feedback. We'll update the export functionality to return the timestamps as ISO8601 format so it can be read in to MATLAB easily.

Jichao Fang
Jichao Fang on 13 Oct 2021

Thanks for the reply! Definitely looking forward to it.

Vinod
Vinod on 12 Nov 2021

We made an update earlier in the week that results in the data exported from a channel to be in ISO8601 format. The exported file can easily be imported back to a ThingSpeak channel, if you need to manually clear some bad data in a channel. It is also really simple now to import the exported CSV into MATLAB using code like this:

table = readtable('feeds.csv')
table.created_at = datetime(table.created_at, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ssXXX', 'TimeZone','UTC')
data = table2timetable(table)
Jichao Fang
Jichao Fang on 13 Nov 2021

Thank you! Now it's easier than ever to work with the data.