Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[Clock] AddDatePoint#18932
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
[Clock] AddDatePoint#18932
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
55 changes: 54 additions & 1 deletioncomponents/clock.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -30,6 +30,8 @@ Installation | ||
| .. include:: /components/require_autoload.rst.inc | ||
| .. _clock_usage: | ||
| Usage | ||
| ----- | ||
| @@ -60,7 +62,7 @@ The Clock component also provides the ``now()`` function:: | ||
| use function Symfony\Component\Clock\now; | ||
| // Get the current time as aDatePoint instance | ||
wouterj marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| $now = now(); | ||
| The ``now()`` function takes an optional ``modifier`` argument | ||
| @@ -73,6 +75,9 @@ which will be applied to the current time:: | ||
| You can use any string `accepted by the DateTime constructor`_. | ||
| Later on this page you can learn how to use this clock in your services and tests. | ||
| When using the Clock component, you manipulate | ||
| :class:`Symfony\\Component\\Clock\\DatePoint` instances. You can learn more | ||
| about it in :ref:`the dedicated section <clock_date-point>`. | ||
| .. versionadded:: 6.3 | ||
| @@ -207,6 +212,53 @@ being in a month or another. | ||
| The :class:`Symfony\\Component\\Clock\\ClockAwareTrait` was introduced in Symfony 6.3. | ||
| .. _clock_date-point: | ||
| The ``DatePoint`` Class | ||
| ----------------------- | ||
| The Clock component uses a special :class:`Symfony\\Component\\Clock\\DatePoint` | ||
| class. This is a small wrapper on top of PHP's :phpclass:`DateTimeImmutable`. | ||
| You can use it seamlessly everywhere a :phpclass:`DateTimeImmutable` or | ||
| :phpclass:`DateTimeInterface` is expected. The ``DatePoint`` object fetches the | ||
| date and time from the :class:`Symfony\\Component\\Clock\\Clock` class. This means | ||
| that if you did any changes to the clock as stated in the | ||
| :ref:`usage section <clock_usage>`, it will be reflected when creating a new | ||
| ``DatePoint``. You can also create a new ``DatePoint`` instance directly, for | ||
| instance when using it as a default value:: | ||
| use Symfony\Component\Clock\DatePoint; | ||
| class Post | ||
| { | ||
| public function __construct( | ||
| // ... | ||
| private \DateTimeImmutable $createdAt = new DatePoint(), | ||
| ) { | ||
| } | ||
| } | ||
| The constructor also allows setting a timezone or custom referenced date:: | ||
| // you can specify a timezone | ||
| $withTimezone = new DatePoint(timezone: new \DateTimezone('UTC')); | ||
| // you can also create a DatePoint from a reference date | ||
| $referenceDate = new \DateTimeImmutable(); | ||
| $relativeDate = new DatePoint('+1month', reference: $referenceDate); | ||
| .. note:: | ||
| In addition ``DatePoint`` offers stricter return types and provides consistent | ||
| error handling across versions of PHP, thanks to polyfilling `PHP 8.3's behavior`_ | ||
| on the topic. | ||
| .. versionadded:: 6.4 | ||
| The :class:`Symfony\\Component\\Clock\\DatePoint` class was introduced | ||
| in Symfony 6.4. | ||
| .. _clock_writing-tests: | ||
| Writing Time-Sensitive Tests | ||
| ---------------------------- | ||
| @@ -294,3 +346,4 @@ use them even if your project doesn't use PHP 8.3 yet. | ||
| .. _`accepted by the DateTime constructor`: https://www.php.net/manual/en/datetime.formats.php | ||
| .. _`PHP DateTime exceptions`: https://wiki.php.net/rfc/datetime-exceptions | ||
| .. _`symfony/polyfill-php83`: https://github.com/symfony/polyfill-php83 | ||
| .. _`PHP 8.3's behavior`: https://wiki.php.net/rfc/datetime-exceptions | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.