New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
request: mechanism to parse date strings limited to future or past #141
Comments
It seems like a bit of a stretch. For example |
It's possible that I'm just missing something in the API. Here are a few more examples, please tell me what is the best way to handle these. Let's say the use case is scheduling an appointment, for which only future values are valid.
|
Well let's say for example that the user enters
|
In order to advance the date I'd have to pick an interval. 1 year doesn't work for the other examples, but picking the right interval doesn't seem possible without knowing what kind of input the user has provided (eg day-of-week, date, time, etc). As far as I can tell sugar doesn't expose that. |
Hm... well this really begs a larger question now I think. Let's say that you want the date to be in the future, but what they enter is What then? |
If I've called an API to parse a future date, a date string that cannot be parsed as a future value should be rejected. Just like any other invalid input. |
I see... Well even if something like this were to exist, it wouldn't belong as part of
|
Agreed, As I mentioned in the original comment, it is possible to move towards this behavior be adding prefix/postfix strings to the users' input. But this means you don't get to take advantage of the localization built into sugar. Also, in practice you need to try lots of different mutations to get this to work out well. ( |
Yea... I'm trying to fight it but I get the need... In fact Sugar does have an internal property that is the specificity of the date as it was parsed. For example This could I suppose be retooled in this way. Perhaps |
I would also really like to see this feature. Personally, I would like to see the future/past thing be a preference rather than an absolute restriction. So the question above about what to do if the user enters something like "June 11, 1998" would, in my opinion, be answered, "Sugar should return a Date object for June 11, 1998." The only time the past/future preference would come into play is if the date is ambiguous. |
Note that the trick of adding suffixes like tomorrow, of next week, of next month, of next year, etc. breaks down when the user has entered a time as well as a date. So, for example, "May 28 of next year" might work, but "May 28 00:00:00 of next year" does not. This really needs to be implemented inside the library to work properly. |
So how do you feel about |
Personally, my preference is for APIs that have a limited number of entry-points with a mechanism for passing behavior options into the entry points, rather than for APIs that have many entry-points that do slightly different things. |
Andrew, I like your proposal for |
+1 to |
I have to agree... I like how explicit that interface is. |
Ok, now that I'm getting around to this there I'd like to list out my thinking in this area. I'm going to list out all the date formats brought up in this thread:
First, #1 is clearly the odd man out. "What time are you going?" - "4 hours" is not an intelligible answer in the first place and needs more context, so that's out (there are a lot better ways to do this depending on an apps logic, anyway... Note that I understand that "in 4 hours" IS intelligible, and I'm going to add this one, as I already have "4 hours from now" and that is equivalent). So, 2-5 are clearly valid, and they all have something in common. They contain an ambiguity one unit higher than their highest defined unit. In other words, if "tuesday" is a "weekday", then the "week", which is one unit higher than "day", is ambiguous, and can be subject to being shifted. Likewise, if "January" is a month, then the "year" is ambiguous. Finally, "5:00am" has a defined "hour", and therefore the "day" is ambiguous... it could be yesterday, today, or tomorrow. Note that what I'm calling "highest defined unit" is not the same as the specificity that I have to work with internally. For example, "tuesday" would have "weekday" as both it's "highest defined unit" and it's "specificity" (most specific unit), however "tuesday 3:25pm" is also ambiguous, but has "minutes" as it's specificity, so something other than specificity is required here. Also note that these 3 units... "month", "weekday", and "hour", seem to be the only ones for which this ambiguity applies. Here is a list of other units and a description of why they don't work:
#1 is invalid because there is only one #2 is perhaps arguable, but I can't imagine a situation in which someone would refer to "the 2nd week" without a point of reference such as "the 2nd week of March". Also there is another ambiguity that would make this difficult as ordinal weeks may be in relation to months ("2nd week of March"), or in relation to years (ISO weeks are 0-52), so I think this one is best left untouched. #3 is not even supported as a format in the first place, (which I think I may fix), however I think this is also invalid for the purposes of the conversation here. Intuition is what drives my thinking here, but then again such is human language. When discussing your plans, saying "sunday" pretty clearly means "the next sunday" (coming up). Conversely, when discussing something you did, it means "the last sunday" (that just passed). However, saying "I'm going on the 15th" on the 30th of a given month is still pretty ambiguous. Granted, in the full context of a month it's understandable (let's say you're talking about the month of November and say "on the 15th"), but barring that extra, non-existent context, it's pretty much safe to assume that the speaker means "the 15th of THIS month", unless they explicitly say "on the 15th of next month" (which is a format Sugar can already parse... w00t!). #4 makes no sense because no one (that I know of) refers to a specific minute, second, or millisecond with an ambiguous parent unit. This would be the equivalent of saying "I'm going on the 45th minute" ... well which 45th minute?... the last hour's, this hour's, or the next hour's? People just don't talk that way. So, that's where I'm at. It seems like the required units that need to have this ambiguity handled are "month", "weekday", and "hour" only... Also note that as suggested above, these would be preferences only. Sugar will never return an invalid date if the date can be parsed so if you have, for example, free user input for a "todo" app or something, and they still enter a date in the past, you will have to manually check the output of Any other thoughts? |
Next question: If user input is Edit: Considering creating a date resets the time, I'm leaning toward |
I'd vote for making it next sunday as the user should use |
Personally, I think your third case above, "the 15th" (or better "on the 15th") is a reasonable format to support for past and future dates. I can certainly imagine someone trying to specify a date in that format. Having said that, it would probably be above-and-beyond the minimum viable product, i.e., I certainly don't think it's a format you must support. I agree that Date.future('Sunday') (if it's currently Sunday) should return next Sunday, not today. |
Andrew, I think most of your reasoning makes sense here. There's at least one important case I can think of that might not be covered:
I'm in agreement with general consensus on I don't think I follow your reasoning wrt Finally, wrt the |
Okay... first I want to note that we're walking a fine line here.... most of the points made could be argued either way. The challenge for me here is to figure out what can be intuited beyond a reasonable doubt and without any other context. The second part is key because with the proper context, most of these things make sense, and of course it is reasonable to expect them. However the separation comes in the fact that "context" in this sense effectively means application logic that the developer provides to Sugar... for example in the act of using So, in the first case, yes you're right that Similarly, saying With the final example, the point is that durations don't make sense as dates unless your application is forcing that connection somehow. That may be perfectly valid, but it's that extra context that your application has to add. The sentence "and make sense of it where a duration might make sense in a given context (this depends heavily on what question you've asked the user)" is entirely the responsibility of the application, not Sugar. The example with
This code is perfectly fine... it's an example of how extra context needs to be applied by the application if needed before reaching Sugar (or to be more clear, the Sugar parsing mechanism). Note however that I have added |
No question this is tricky stuff. At the library level there's really no way to anticipate all the different types of interactions the programmer might want to have with the user. I agree that its to no one's benefit to have a library that seems to be making subjective decisions that the programmer can't predict.
This gives the programmer a clear contract and provides a mechanism for behaviors to be fine-tuned based on specific contexts/interactions. In the I completely agree that durations should be treated differently than dates, and that it would not make sense for a function that parses dates to also parse a duration (sorry if that wasn't clear). I wasn't sure if there was a duration parsing function somewhere in the library that I wasn't aware of: the answer seems to be no. I think it would be useful (again, as a distinct function) - perhaps I should file that as a separate request though. |
ok thanks to everyone who weighed in here... v1.3 now gives you |
Thanks Andrew, this is certainly a nice addition to the API.
|
I'll have a look at the The second part is something I have fixed recently... I'm assuming you're using 1.3? Can you give it a shot in the edge build (in the |
I tried out the edge build and still get the same problem with |
Hm that's weird... it seems to be working for me... can you provide an exact repro and environment? Also note that if the date is explicitly in the past, |
Same test case but this time I was testing at 9am and used the input string '8am' I made an argument earlier for having Having |
On 8/9/2012 1:00 PM, ziIizeqQ wrote:
jik |
Definitely don't want to have it return Mostly against the anchor date because of overloading the arguments... also I think that if you're going that far what you need can be accomplished somehow else. |
Ok this issue is now fixed in 1.3.1. |
There are many applications in which a user can be expected to input a date that must be in the future or must be in the past. Sugar's Date.create() mechanism is powerful but doesn't seem to expose a way for the programmer to hint the user's future or past intention if it is known. This limits its utility.
In these cases, the user is required what seems to be redundant information. For instance, if I'm scheduling an appointment I should only need to say '4 hours' rather than 'in 4 hours'. Likewise, if I say 'Tuesday' or 'January' I'd expect to get the closest match in the future and rather than having to say 'next Tuesday' or 'next January'.
It's possible for a user of the library to work around this by adding prefix/postfix hints to the user input, but this solution isn't portable and can go wrong in unexpected ways. (As an example, try passing the string 'feb of next month' to Date.create())
The text was updated successfully, but these errors were encountered: