andrewplummer / Sugar

A Javascript library for working with native objects.
https://sugarjs.com/
MIT License
4.53k stars 306 forks source link

Setting default time when parsing strings #594

Open Secretmapper opened 7 years ago

Secretmapper commented 7 years ago

With Sugar.Date, I can create specific datetimes through human readable strings. For example Sugar.Date.create('today') sets it to today at 12:00AM

For some use cases, such as sayusing it to set a deadline, It wouldn't make sense to set a deadline for today if the time will be set at 12:00am. Is there a way to set a default time, (say 9:00pm) when time is ambiguous from the input string?

Secretmapper commented 7 years ago

I figured it might be possible to use params to solve this, but unfortunately the info being returned is not enough.

i.e.

tomorrow will give params as {unit: 4, num: 1, day: 1, specificity: 4}

tomorrow at 8:00 will still give params as {unit: 4, num: 1, day: 1, specificity: 4} even though it has a time set.

If there's a am/pm in the time (tomorrow at 8:00pm) I can use ampm from params to know there was a time set, but for 2400 hrs set there's no way to know.

andrewplummer commented 7 years ago

I think the way you're approaching it is correct, however the specificity thing seems to be a bug. If I can fix the bug, then I think it should work for you?

Secretmapper commented 7 years ago

Thanks, I think it should work if specificity is fixed yeah!

For more info, 8:00, 8:00:00, 8:00pm all give the correct specificity, but adding a date to it (tomorrow at 8:00pm) defaults the specificity to 4 (day)

andrewplummer commented 7 years ago

Ah but that's correct. 4 is actually hours (units count up from milliseconds starting at 0... Planning to improve this in the next version). So I think what's happening is that "today" is adding hours so it's showing 4 when it really should be 5 (specificity should be based on input), so that can be considered a bug.

Secretmapper commented 7 years ago

Oops sorry about that, for reference I'm using the list here:

https://github.com/andrewplummer/Sugar/issues/589

In it I'm expecting tomorrow at 8:00pm to have specificity of 3 (hour). Maybe actually 2 (minute) because minute is technically defined with the 00. Or did I miss something?

0 = millisecond
1 = second
2 = minute
3 = hour
4 = day
5 = week
6 = month
7 = year
andrewplummer commented 6 years ago

Wow, I really dropped the ball on this one. I'm sure it's no use to you anymore, but fixed that bug... will go out next patch update.