Working with date and time in Octave

When processing time series in Octave, you probably have timestamps to deal with. I recently had data with timestamps formatted like “25-9-2022 19:17”. Reading data like this from a file can be done with the textscan function. In the case of timestamps and a double value, separated by a tab, you would do this via

data=textscan(fid,"%s %f","Delimiter","\t");

The variable data is of the cell type, which can hold several different data types. You can access the array of double values from the second column via


In order to be able to do something with the timestamps, you have to parse the timestamp strings and convert them to a number. This is done via the datenum function:

timestamps=datenum(data{1},'dd-mm-yyyy HH:MM');

This results in timestamps that are days since January 1st 0000. I’m not sure how Octave deals with leap years and other calendar changes, so be a bit careful here. These timestamps can be converted to other formats with the functions shown in this figure on the Octave Wiki. For computing the difference between timestamps, it’s probably sufficient to e.g. multiply times 86400 to get a time difference in seconds. Be aware of precision/rounding errors here – I had timestamps at 10 minute intervals that turned out to be, according to Octave, just a tiny bit shorter than 600 seconds, so rounding to the nearest full second might be a smart move.

Een reactie plaatsen

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *