-
Notifications
You must be signed in to change notification settings - Fork 40
Normalize ordinal dates #189
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
Conversation
|
I would advise against using exceptions when what you really want is just a logic branch. Why not something like this? function normalizeOrdinalDate($dtValue) {
list($year, $day) = explode('-', $dtValue, 2);
$day = intval($day);
if ($day < 367 && $day > 0) {
$date = \DateTime::createFromFormat('Y-z', $dtValue);
$date->modify('-1 day'); # 'z' format is zero-based so need to adjust
if ($date->format('Y') === $year) {
return $date->format('Y-m-d');
}
}
return '';
} |
|
Good point, @Zegnat. I think I started with Exception because I was going to check Don't code tired. :) |
Zegnat
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that the exception throwing is gone, I think it all looks good!
| * @param string $dtValue | ||
| * @return string | ||
| */ | ||
| function normalizeOrdinalDate($dtValue) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function isn't properly validating the input, but I see that it is only ever called after the input is guaranteed to be in the format \d{4}-\d{2}. Can you add a comment to the function definition that explains that this function can only be called with pre-validated input?
aaronpk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see inline comment
Fixes #167