Does comparing string representation of dates in a binary tree implementation offer any advantages over comparing dates directly? - comparison

Does lexical comparation of Dates (or DateTimes) offer any advantages when constructing Binary Search Trees over direct comparation of Dates (or DateTimes) when constructing the tree using the date as a key? The newer dates will always follow after the dates that were already added, which would result in a degenerate tree when comparing the dates. I would like to know if using a lexical comparation of the string representation of the date will help with this issue.
P.S. If this question is not appropriate for SO, please comment, I will remove it.

Related

NLP approaches to identify dates/time expressions in text

I need to develop an application which identifies the date inside the given text using some NLP approach. Let's assume I have a data in DB with dates column "from", "to" and if the text is below,
Get data between 1st August and 15th August
I need to identify the dates and form the query to retrieve the data. I used Natty NLP and I was able to identify the dates. But I'm stuck for more complex time expressions like:
Get data uploaded next week
Get data uploaded last week
Here for the first one I need to identify next week Monday's date and Sunday's date and form the query same for the 2nd one. But with Natty it gives me next week from today's date. What other solutions exist? Or do I need to manipulate the expression by coding? I am using Java.
Your questions is a bit confusing, but I guess you want to achieve two things:
Identify words that represent a time expression
Map these words to a formal machine-readable representation
If that is what you need check the duckling framework, it identifies time expressions, and it normalise them into a single unique formal date representation.
Note that you need to pass a reference date, for ambiguous time expressions.
You can run it as a service and call it from your code.

neo4j date was indexed as string (import tool) and using apoc for parsing queries is good approach?

Date fields were indexed as string (import tool) and using apoc for parsing queries is good approach?
How it's affect performance?
What's the best approach?
Be aware that my initial load is from import tool
Thanks
in advance
I suggest you to store your dates as integers representing milliseconds since epoch (milliseconds passed since 01-01-1970 00:00:00:00). It has some advantages:
Easy to compare: You do not need to convert the dates before comparing since it is a simple number. So to verifiy if the birth date of a is greater to birth date of b you simply do a.birthDate > b.birthDate.
Without dependencies: You will not depend on any library to compare your dates.
Since you are storing your dates as an simple integer you can convert it in the front-end application and present it in any format you choose.
Also when you need you will use the APOC procedures to manipulate the timestamps stored in the database.

Fetch data with complex criteria

My app has relatively simple model (inherited from existing web-site's model).
One of the attributes of the model is date. Unfortunately, values for this attribute are quite free-form and of type String. I mean, values are like:
"June 1888, Arles"
"c. 1530"
"1634-37"
"1508"
Is there a way to build a predicate that will allow me to transform these values to integer values so that I can perform search on values of the date attribute?
Currently I am using simple approach:
NSString *format = #"date contains[cd] %i";
NSPredicate *predicate = [NSPredicate predicateWithFormat:format, year];
but it's not allowing me to find everything between two given dates.
You... may have a problem here. Any date can be transformed to an integer value, and NSDateFormatter does a decent job of parsing dates if you know what format to use. You seem to have multiple, very different formats, so you're not going to get away with a simple transformation or search to get integer values for your date strings.
Finding just a four-digit year would be pretty easy, but your example of "1634-37" kind of blows that idea away. If your search was for 1636, it should match, but it won't without some extra help. If you can accept a certain margin of error, you might not care-- just call that one 1634 and be done with it. If not, first you'll need a range of dates (min 1634, max 1637) rather than a single date, and second, you'll need some way to realize that the "-37" should be applied to the "16". Good luck with that, because this is rapidly becoming a complex problem.
If there are only a few possible date formats, you could come up with a parser for each one and then either use regex matching or trial and error to choose the parser for a string. If there are lots of formats though, you're kind of screwed. You'll be better off editing the data by hand, entering the date in a preferred format that your app can make sense of. Ultimately this really depends on how exactly you need to use the data and on how much error you can tolerate in the results.
On a slightly separate note, if it were me, I'd want to convert these to numeric values when I received the data from the web server. Then when doing a fetch, I'd be able to use numeric matching in the predicate for speedy results. Matching strings-- especially if you need to use a regular expression-- is very slow compared to numeric comparisons.
Combine NSPredicate with regular expressions. Here is a link. Use regex to search for a 4 digit number
NSPredicate and Regex

Lua Date Format and Comparison

How do you compare dates with lua? What is the best string format for dates? Should I store dates in epoch? I am looking for performance ...
Is the best way os.difftime?
You are asking several things, so here are my answers:
Should I store dates in epoch?
In general yes, the best way to store the dates is by using epochs, as returned by os.time
How do you compare dates with lua?
It depends on how you want to "compare" them.
If you just want to know which one is newer/older, then the easiest fastest thing is storing them as "epochs" and then doing date1 < date2; since both dates are just numbers, this is both performant and clean.
If you want to know how many months/days/years have passed between two given dates, that's a bit more complex. You will need a code similar to this:
diff = os.date("*t", os.difftime(date1, date2))
On that example, the returned diff is a table similar to {year=1, month=5, day=1, hour=2, min=3, sec=40 ...}
I am looking for performance ...
If you are using os.date() too often to transform epochs into dates (for example, for printing) then you might want to "cache" the year, month, etc information in a table, so you don't have to call it again and again. But do this only if you experience a bad performance; don't pre-optimize.
What is the best string format for dates?
That completely depends on how you want to use them. For example, if your app interacts with another service that expects a certain date format, it makes sense to use that format in all your app.
If you have no particular need to use a format, then one candidate is (%x):
os.date("%x", date) -- 09/16/1998 (for example)
The string that gives you depends on the computer's locale. This might or might not be desirable.
If you want the representation to be the same across all computers, independently of their locale, you might want to try a standard format, like ISO 8601:
os.date("%Y-%m-%d", date) -- returns "1998-09-16" in all computers
This format has lots of advantages; the most obvious one is that dates sorted out alphabetically are also sorted out chronologically. But the most important one is that a lot of software out there is prepared to read/use it.
You can find more information about dates in Programming in Lua, Section 22.1 - Date and Time and in the lua-users wiki.

What data structure is recommended for multiple calendars, dates and durations?

I have a requirement to store dates and durations arising from multiple different calendars. In particular I need to store dates that:
Span the change to Gregorian calendars in different countries at different times
Cover a historic period of at least 500 years
Deal with multiple types of calendar - lunar, solar, Chinese, Financial, Christian, UTC, Muslim.
Deal with the change, in the UK, of the year end from 31st March to 31st December, and comparable changes in other countries.
I also need to store durations which I have defined as the difference between two timestamps (date and time). This implies the need to be able to store a "zero" date - so I can store durations of, say, three and a half hours; or 10 minutes.
I have details of the computations needed. Firebird's timestamp is based on a date function that starts at January 1st, 100 CE, so is not capable of being used for durations in the way I need to record them. In addition this data type is geared up (like most timestamp functions) to record the number of days since a base date; it is not geared up to record calendar dates.
Could anyone suggest:
A data structure to store dates and durations that meet the above requirements OR
A reference to such a data structure OR
Offer guidelines to approach the structuring of such storage OR
Any points that may help me to a solution.
EDIT:
#Warren P has provided some excellent work in his responses. I obviously have not explained what I am seeking clearly enough, as his work concentrates on the computations and how to go about calculating these. All valuable and useful stuff, but not what I intended my question to convey.
I do have details of all the computations needed to convert between various representations of dates, and I have a fairly good idea of how to implement them (using elements such as Warren suggests). However, my requirement is to STORE dates which meet the various criteria listed above. Example: date to be stored - 'Third June 13 Charles II'. I am trying to determine an appropriate structure within which to store such dates.
EDIT:
I have amended my proposed schema. I have listed the attributes on each table, and defined the tables and attributes by examples, given in the third section of the entity box. I have used the example given in this question and answer in my definition by example, and have amended the example in my question to correspond. Although I have proved my schema by describing somebody else's example, this schema may still be over complicated; over analysed; miss some obvious simplification and may prove very difficult to implement (Indeed, it may be plain wrong). Any comments or suggestions would be most welcome.
If you are writing your own, as I assume you intend to, I would make a class that contains a TDateTime, and other fields, and I would base it on the functionality in the very nicely written mxDateTime extension for Python, which is very easily readable, open source, C code, that you could use to extract the gregorian calendar logic you are going to need.
Within certain limits, TDateTime is always right. It's epoch value (0) is December 30, 1899 at midnight. From there, you can calculate other julian day numbers. It supports negative values, and thus it will support more than 400 years. I believe you will start having to do corrections, at the time of the last Gregorian calendar reforms. If you go from Friday, 15 October 1582, and figure out its julian day number, and the reforms before and after that, you should be able to do all that you require. Be aware that the time of day runs "backwards" before 1899, but that this is purely a problem in human heads, the computer will be accurate, and will calculate the number of minutes and seconds, up to the limit of double precision floating point math for you. Stick with TDateTime as your base.
I found some really old BorlandPascal/TurboPascal code that handles a really wide range of dates here.
If you need to handle arabic, jewish, and other calendars, again, I refer you to Python as a great source of working examples. Not just the mxdatetime extension, but stuff like this.
For database persistence, you might want to base your date storage around julian day numbers, and your time as C-like seconds since midnight, if the maximum resolution you need is 1 second.
Here's a snippet I would start with, and do code completion on:
TCalendarDisplaySubtype = ( cdsGregorian,cdsHebrew,cdsArabic,cdsAztec,
cdsValveSoftwareCompany, cdsWhoTheHeckKnows );
TDateInformation = class
private
FBaseDateTime:TDateTime;
FYear,FMonth,FDay:Integer; // if -1 then not calculated yet.
FCalendarDisplaySubtype:TCalendarDisplaySubtype;
public
function SetByDateInCE(Y,M,D,h,m,s:Integer):Boolean;
function GetAsDateInCE(var Y,M,D,h,m,s:Integer):Boolean;
function DisplayStr:String;
function SetByDateInJewishCalendar( ... );
property BaseDateTime:TDateTime read FDateTime write FDateTime;
property JulianDayNumber:Integer read GetJulianDayNumber write SetJulianDayNumber;
property CalendarDisplaySubType:TCalendarDisplaySubtype;
end;
I see no reason to STORE both the julian day number, and the TDateTime, just use a constant, subtract/add from the Trunc(FBaseDateTime) value, and return that, in the GetJulianDayNumber,SetJulianDayNumber functions. It might be worth having fields where you calculate the year, month, day, for the given calendar, once, and store them, making the display as string function much simpler and faster.
Update: It looks like you're better at ER Modelling than me, so if you posted that diagram, I'd upvote it, and that would be it. As for me, I'd be storing three fields; A Datetime field that is normalized to modern calendar standards, a text field (free form) containing the original scholarly date in whatever form, and a few other fields, that are subtype lookup table Foreign keys, to help me organize, and search on dates by the date and subtype. That would be IT for me.
Only a partial answer but an important piece.
Since you are going to store dates in a very broad range where a lot of things happened to calendars, you need to accommodate for those changes.
The timezone database TZ-database and the Delphi TZDB wrapper around the TZ-database will be of big help.
It has a database with rules how timezones historically behave.
I know they are based on the current calendar schemes, and you need to convert to UTC first.
You need to devise something similar for the other calendar schemes you want to support.
Edit:
The scheme I'd use would be like this:
find ways for all your calendars to convert to/from UTC
store the calendar type
store the dates in their original format, and the source of the date (just in case your source screwed up, and you need to recalculate).
use the UTC conversions to go from your original through UTC to the calendar types in your UI
--jeroen

Resources