Add location as required argument for Time.parse#6369
Conversation
This makes it more clear in the API that a default value is required instead of defaulting to `nil` which would raise in case of time zone information missing from the formatted string. `Time.parse_utc` and `Time.parse_local` are added as short cuts to the most common use cases.
|
The convenience overloads are good, but I'm not sure I like either the current API or the proposed ones too much. def parse(time : String, pattern : String, location : Location)
def parse(time : String, pattern : String, *, raise_on_missing_location : Bool)? Honestly personally I would be fine with def parse(time : String, pattern : String, location : Location)
def parse(time : String, pattern : String) # raises, separate overload for nicer docs and nobody doing a meaningless (..., nil)Yes, it's a runtime error but it's rather uncommon to parse a diverse set of time formats at the same time, so caught early in development. |
|
You second suggestion is essentially the same as we have currently: def parse(time : String, pattern : String, location : Location? = nil)The issue with this solution is that the shortest overload So, maybe even more explicitness would be nice. This could either be a required argument ( |
Hence my first suggestion first and my personal opinion second. As I wrote in the second suggestion the separate overloads would be for nicer docs and code completion/IDE integration. I do see your point though and hence my compromise proposal stands first. Though I see now it doesn't make sense since there's no sensible behavior for passing false. How terrible would it be to have a fake location like |
|
Alternatively, make the current parse assume a location. Go for example assumes UTC. But I'm not sure. In my workplace, for the current project, we have a tool where you specify a range of dates in the command line. It's a C# application. C# also seems to assume a location, and now I'm curious which one is it. This program sends the date to a server, and should probably be in UTC, or the server's time. So forcing the user to think a bit about whether they want UTC or local by default might be a good idea, even if it's a bit annoying. But also, maybe the current behavior isn't bad, like jhass says, because you'll find out soon if there's something wrong (unless it's user input, which might not be a common case). I can't make up my mind about this topic :-P |
|
If we had |
|
I tend to like using a different method name altogether. It clearly communicates a difference in behaviour and doesn't do so over special argument types or values. |
|
For reference, I just noticed a similar use case in |
|
I added a commit that changes |
d2babbd to
41d6f3c
Compare
This makes it more clear in the API that a default value is required instead of defaulting to
nilwhich would raise in case of time zone information missing from the formatted string.Time.parse_utcandTime.parse_localare added as short cuts for the most common use cases.See #6258 (comment)