How to store partially known datetimes
1 view (last 30 days)
Show older comments
Russell Shomberg on 7 Oct 2020
Commented: Siddharth Bhutiya on 23 Oct 2020
What is the best way in MATLAB to deal with partially known date times?
I have a list of dates and times as part of a database. For most, I know the full date and time, but for many I only know down to the day, or month, or even just year. Currently, I have them stored in a table seperated by year, month, day, and time, but I would much prefer a single field if possible.
Is there a way to use the datetime or other datatype in MATLAB to convey that information about the date and time could be missing?
Walter Roberson on 7 Oct 2020
Unfortunately, datetime objects only have specific complete datetimes, or else the Not A Time object, NaT .
You could fill out the datetime objects as far as you know, using the "start" of the date (e.g., Jan 1, 00:00:00 of the year), and also keep a numeric encoding of which information is missing (perhaps an enumeration)
Stephen23 on 8 Oct 2020
Edited: Stephen23 on 19 Oct 2020
I don't believe that information can be encoded inside one datetime object, so you will have to use another object or variable to hold that information (which could be another datetime).
Here are a few idea (all examples are known down to the nearest minute):
- use a calendarduration or duration object to store the lowest known unit, e.g. duration 00:01:00.000
- similarly with a date vector, e.g. [0,0,0,0,1,0] (downside: not scalar, but perhaps slightly easier to work with)
- similarly with a logical vector, e.g. [false,false,false,false,true,false] (downside: not scalar)
- store one single integer index/count of the known units, e.g. 5 would indicate data is known to the nearest minute.
I would start with the fourth option (on the basis that this is by far the simplest) and see if that could work for your algorithm.
Siddharth Bhutiya on 23 Oct 2020
Based on this information, as you had mentioned in your earlier comment a custom class seems to be the way to go here. Your class can have 2 properties, a datetime and an 'estimate' metadata. What you choose as the datatype for your 'estimate' metadata would depend on your use case. For example, let's say, if the data is such that even if the time is provided, it still can be an estimate, i.e. it can be that time +/- x hrs, then it makes sense to store the 'estimate' as calendarDuration or duration. However, if the data is such that whatever is provided is accurate and only the missing pieces are unknown/estimates then you can store the 'estimate' as a scalar categorical that notes the last known unit ('day','month','hour',etc.), as Stephen mentioned above.
You mentioned that it is for a database, then maybe instead of complicating things by creating a class, you might be better off having 2 fields in your table one for the datetime and one for the 'estimate'.
More Answers (0)
Find more on Dates and Time 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!