Sugar icon indicating copy to clipboard operation
Sugar copied to clipboard

Setting default time when parsing strings

Open Secretmapper opened this issue 7 years ago • 6 comments

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 avatar Jun 07 '17 13:06 Secretmapper

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.

Secretmapper avatar Jun 08 '17 06:06 Secretmapper

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?

andrewplummer avatar Jun 08 '17 07:06 andrewplummer

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)

Secretmapper avatar Jun 08 '17 07:06 Secretmapper

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.

andrewplummer avatar Jun 08 '17 07:06 andrewplummer

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

Secretmapper avatar Jun 08 '17 07:06 Secretmapper

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.

andrewplummer avatar Aug 11 '18 15:08 andrewplummer