Skip to content

Conversation

@gRegorLove
Copy link
Member

Fixes #167

@gRegorLove gRegorLove requested review from Zegnat and aaronpk August 3, 2018 16:39
@Zegnat
Copy link
Member

Zegnat commented Aug 3, 2018

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 '';
}

@gRegorLove
Copy link
Member Author

Good point, @Zegnat. I think I started with Exception because I was going to check DateTime::getLastErrors() for warnings about invalid dates but ended up going a different route.

Don't code tired. :)

Copy link
Member

@Zegnat Zegnat left a 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) {
Copy link
Member

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?

Copy link
Member

@aaronpk aaronpk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see inline comment

@aaronpk aaronpk merged commit ba9d428 into microformats:master Aug 23, 2018
@gRegorLove gRegorLove deleted the issue167 branch July 8, 2022 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants