Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
duration() interpreting 0[units] as 1[units] when argument is single string #507
I only tested this for hours and minutes (as you can see below), so I'm not sure how general the issue is.
I had some free time on my hands recently so I decided to take a look at the parsing code.
Lines 45 and 46 of src/period.c, inside
I guess this is to enforce the statement, on line 197 of R/durations.r, that
i.e. to take care of the case where the numeral is missing but the unit is there (e.g. "1days 3mins" and "days 3mins" will yield the same result).
As noted in a pertinent comment inside function
it seems that the current way of detecting missing numerals (receiving a zero from
If the current (missing numeral) behavior is to be kept, maybe a counter should be implemented to check if at least 1 character has been parsed? You could then make the function return an
I'm not familiar enough with the package to judge whether the current missing numeral behavior is the most desirable, but, from a user's perspective, I think I would prefer to be informed if I forgot to pass a numeral next to my unit; it's also more likely (I would think) that, if the input was produced by another function/program/software, a missing numeral really "meant to denote" 0 units and not 1. Or is this not your experience? Like I said, I'm not really sure what the big picture is in this situation.
Don't know if this is entirely correct or actually useful to you (it was certainly not a very extensive investigation) but I just thought I'd share. I didn't run any code/tests, just followed the function trail.
Your identification of the problem is correct. There must be an easy fix but I have been very busy recently.
I think I made the decision to default missing units to 1 because that parsing code is likely to be used in other places such as rounding or conversion where most common case is to specify one unit of time ("day", "minute" etc). I think I also didn't think much about 0 units back then. Let me have a fresh look.