How to store partially known datetimes

1 回表示 (過去 30 日間)
Russell Shomberg
Russell Shomberg 2020 年 10 月 7 日
コメント済み: Siddharth Bhutiya 2020 年 10 月 23 日
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?
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 10 月 7 日
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
Stephen23 2020 年 10 月 8 日
編集済み: Stephen23 2020 年 10 月 19 日
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):
  1. use a calendarduration or duration object to store the lowest known unit, e.g. duration 00:01:00.000
  2. similarly with a date vector, e.g. [0,0,0,0,1,0] (downside: not scalar, but perhaps slightly easier to work with)
  3. similarly with a logical vector, e.g. [false,false,false,false,true,false] (downside: not scalar)
  4. 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.
  4 件のコメント
Russell Shomberg
Russell Shomberg 2020 年 10 月 19 日
It's for a database that holds dates and times when certain events were observed. Sometimes those known to the second, but other times, they are only estimates and might only contain a year and month. I need to be able to store the datetimes for all of the, but also show which are estimates ISO 8601 says that this can be represented by just leaving the end digits off, but this causes MATLAB to assume they are all 1.
Users might want to pull of certain date ranges, or only for the estimates. Also if I was plotting them times, I would need to know which are estimates and by how much.
Siddharth Bhutiya
Siddharth Bhutiya 2020 年 10 月 23 日
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'.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by