Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

Provide support for DateTimes in DateRange #3

Merged
merged 4 commits into from
Apr 5, 2016
Merged

Conversation

rquadling
Copy link
Contributor

Currently DateRange only deals with dates. diff() amends date by a day and __toString() outputs only the date.

DateTimeRange overrides these 2 methods and processes times.

All unit tests cloned from DateRangeTest, times added and all pass.

@rquadling
Copy link
Contributor Author

I can't see what the conflicts are. Unless it is the lock file. Which should be removed.

@gws
Copy link
Owner

gws commented Mar 11, 2016

Yeah, I removed the lockfile in 2cf7dfa before I saw your PR.

Set FUTURE to be the end of the year
Introduce DateTimeRange with tests
@rquadling
Copy link
Contributor Author

Any chance of a merge of this PR?

@gws
Copy link
Owner

gws commented Mar 22, 2016

@rquadling Yeah, I think so. I'm low on free time, but should have some time later this week. I will probably be merging https://github.com/gws/php-valueobjects/tree/use-datetimeinterface soon once I polyfill DateTimeImmutable::createFromMutable, and that branch will conflict with this PR.

@rquadling
Copy link
Contributor Author

DateTimeImmutable cannot polyfill to PHP 5.4 due to DateTime class not being able to implement DateTimeInterface. I don't want to swap out the use of \DateTime for my own class as that changes WAY too much code.

Bit stuck with this. I have to continue using 5.4 (going to 7 this year sometime).

If you could merge my PR and tag it before use-datetimeinterface, then I can fix my dependency to that tag. As soon as we are 7, then we can move our dependency up to the latest stable.

@gws
Copy link
Owner

gws commented Mar 22, 2016

I see. It's no problem to hold off on merging that branch and bumping the requirement to 5.5, then.

'/',
array(
$this->getStart()->format('Y-m-d\TH:i:s\Z'),
$this->getEnd()->format('Y-m-d\TH:i:s\Z')
Copy link
Owner

Choose a reason for hiding this comment

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

Using \Z here is a lie if the DateTimes aren't in that timezone. Something like $this->getStart()->format('c') would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the output has to be timezone neutral, so maybe using DateTime::setTimezone(new DateTimeZone('UTC')) would be more appropriate.

Copy link
Owner

Choose a reason for hiding this comment

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

That would be valid in the sense that it would at least represent the correct instant in time, but it doesn't respect the time zone(s) that the user created the range with. Non-UTC timezones are perfectly valid, and in some situations (e.g. debugging a calendaring/scheduling app), they may be desired.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://en.wikipedia.org/wiki/ISO_8601#Time_intervals and mentions the 4 ways and none of them are tz specific. As the format being represented is an interval, there is no relevance to the timezone.

And https://www.cs.tut.fi/~jkorpela/iso8601.html#sol, and specifically, "Period of time format", also use Zulu format.

But having said that, http://www.uai.cl/images/sitio/biblioteca/citas/ISO_8601_2004en.pdf (Section 4.4 page 20) makes no mention of the Z and explicitly refers to "local time".

So, no \Z part if not UTC timezone.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think. It's late.

Copy link
Owner

Choose a reason for hiding this comment

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

If I create a DateTimeRange as

$dr = new DateTimeRange(
  new DateTime('2016-01-01T00:00:00-0700'),
  new DateTime('2016-01-01T04:00:00-0400')
);

That's a 1 hour interval and I'd expect __toString() to print 2016-01-01T00:00:00-0700/2016-01-01T04:00:00-0400, which is perfectly valid (example 1 here: https://en.wikipedia.org/wiki/ISO_8601#Time_intervals).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. It is annoying that none of the documentation for time intervals includes an example with timezone offset, just zulu time.

I'll update the code to use 'c' type. At least that does fix the issue for non UTC users.

Fixes output when non UTC timezone used.
* @return DateRange
* @throws OutOfRangeException
*/
public function diff(DateRange $arg)
Copy link
Owner

Choose a reason for hiding this comment

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

Can we type hint on DateTimeRange here as well as updating any relevant docstrings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type hint has to remain as a DateRange, of which a DateTimeRange is a descendent. I've added a unit test for DateRange->diff(DateTimeRange) and DateTimeRange->diff(DateRange).

@gws
Copy link
Owner

gws commented Mar 23, 2016

@rquadling I think I'm done with review - if we can resolve my most recent comment I'll merge this.

@rquadling
Copy link
Contributor Author

Finally!!!

@gws gws merged commit 05d9ebc into gws:master Apr 5, 2016
@gws
Copy link
Owner

gws commented Apr 5, 2016

@rquadling OK, merged. Thank you!

@rquadling
Copy link
Contributor Author

Thank you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants