Skip to content
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

3.0 - Use DateTime in Time utility 'is' and 'was' comparisons #2251

Merged
merged 4 commits into from Nov 8, 2013

Conversation

bcrowe
Copy link
Contributor

@bcrowe bcrowe commented Oct 31, 2013

No description provided.

@ravage84
Copy link
Member

All this code looks very similar. May be you can put the main code into a protected method and parameterize it?

Something like:

 _isSameDate($dateString, $format1, $format2, $timezone = null)

@markstory
Copy link
Member

I like @ravage84's suggestion.

@bcrowe
Copy link
Contributor Author

bcrowe commented Oct 31, 2013

@markstory @ravage84

Or, alternatively, do we want to scrap all these methods and have a single public method with an options parameter?

Incomplete, but illustrates the idea:

is($relative, $dateString, $timezone) {
    $dateTime = new \DateTime;
    switch($relative) {
        case 'today':
            $format = 'Y-m-d';
            $relativeTo = 'now';
            break;
        case 'yesterday':
            $format = 'Y-m-d';
            $relativeTo = 'yesterday';
    }
    $timestamp = $dateTime->setTimestamp(static::fromString($dateString, $timezone))
        ->format($format);
    $now = $dateTime->setTimestamp(static::fromString($relativeTo, $timezone))
        ->format($format);
    return $timestamp === $now;
}

Edit: Maybe name the method 'compareTo'?

@markstory
Copy link
Member

I think the named methods are nice as they make the calling code more expressive and easier to read. If those methods all use a single method internally that is fine.

@bcrowe
Copy link
Contributor Author

bcrowe commented Oct 31, 2013

@markstory Sounds good, will update later this evening.

@Phally Phally closed this Nov 3, 2013
@Phally Phally reopened this Nov 3, 2013
@bcrowe
Copy link
Contributor Author

bcrowe commented Nov 4, 2013

Any comments on this?

* @return boolean True if datetime string is within the constraint
*/
protected static function _evaluateConstraint($dateString, $timezone = null, $anchor, $format) {
$dateTime = new \DateTime;
Copy link
Member

Choose a reason for hiding this comment

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

I am usually a fan of keeping object variables uppercase on first letter ($DateTime).

Copy link
Member

Choose a reason for hiding this comment

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

I think having it as lowercase is fine for 3.x. We will be using object more extensively and the code will look like a giant collection of uppercase letters if we follow that.

Copy link
Member

Choose a reason for hiding this comment

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

I used to use $Foo as well, but it got really annoying as code started having more objects.

Copy link
Member

Choose a reason for hiding this comment

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

So will we drop if for 3.x completely? Or just for php-internal objects etc?

Copy link
Contributor

Choose a reason for hiding this comment

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

I like lowercase as well.

Also maybe $dateTime = new \DateTime(); instead of $dateTime = new \DateTime; (without the brackets).

@lorenzo
Copy link
Member

lorenzo commented Nov 4, 2013

Refactoring looks good, although I'm concerned that we are making more difficult to spot and fix this ticket which is related to the functions you are changing #2032

@markstory
Copy link
Member

The new refactorings look good.

* @param string $format The date and time format used for comparison
* @return boolean True if datetime string is within the constraint
*/
protected static function _evaluateConstraint($dateString, $timezone = null, $anchor, $format) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The default value for $timezone isn't really useful here, because the rest of the parameters are mandatory.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah optional arguments followed by mandatory ones is generally frowned upon.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe $timezone (if optional compared to the others) should then be the last one.

@bcrowe
Copy link
Contributor Author

bcrowe commented Nov 6, 2013

Moved the "timezone" parameter to be the last parameter. Renamed the method to be semantically correct.

@markstory
Copy link
Member

Looks great.

@ravage84
Copy link
Member

ravage84 commented Nov 6, 2013

Still not fully happy with the name _evaluateTimeSpan but that shouldn't be a total show stopper 👍

@Phally
Copy link
Contributor

Phally commented Nov 6, 2013

Yeah I feel (because it returns a boolean) it should be _isWithinTimeSpan(). The rest of the changes look good! 👍

@bcrowe
Copy link
Contributor Author

bcrowe commented Nov 6, 2013

@Phally 👍 I like that better too.

@Phally
Copy link
Contributor

Phally commented Nov 6, 2013

Excellent!

lorenzo added a commit that referenced this pull request Nov 8, 2013
3.0 - Use DateTime in Time utility 'is' and 'was' comparisons
@lorenzo lorenzo merged commit 56f9a6e into cakephp:3.0 Nov 8, 2013
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.

None yet

7 participants