Conversation
Create a general Time Widget which is also the base for TimeAgo, TimeSince & TimeUntil
For this change to work the following PR is required: Icinga/icingaweb2#5238
src/Widget/Time.php
Outdated
|
|
||
| protected $tag = 'time'; | ||
|
|
||
| public function __construct(int|float|DateTimeInterface $time) |
There was a problem hiding this comment.
Please add PHPDoc.
This is not the right place for timezone handling. It is also unnecessary for daylight saving time, because IANA time zones take care of the shift.
nilmerg
left a comment
There was a problem hiding this comment.
Didn't look at JS yet.
Also, add tests for Time and its child classes. Test formatting and rendering.
src/Widget/TimeAgo.php
Outdated
| $values = ['at %s', 'An event happened at the given time']; | ||
| } | ||
|
|
||
| return sprintf(t(...$values), $time); |
There was a problem hiding this comment.
src/Widget/TimeSince.php
Outdated
| $values = ['since %s', 'A status is lasting since the given time, date or date and time']; | ||
| } | ||
|
|
||
| return sprintf(t(...$values), $time); |
There was a problem hiding this comment.
src/Widget/TimeUntil.php
Outdated
| $values = ['at %s', 'An event will happen at the given time']; | ||
| } | ||
|
|
||
| return sprintf(t(...$values), $time); |
There was a problem hiding this comment.
clean up unnecessary methods only allow \DateTime to construct widgets simplify formating of time strings
And fix linting error in Time.php
| /** @var string Tag of element. */ | ||
| protected $tag = 'time'; | ||
|
|
||
| public function __construct(DateTime $time) |
There was a problem hiding this comment.
There's still no phpdoc for the constructor… is there a reason why you neglect to add this?
| /** @var string Tag of element. */ | ||
| protected $tag = 'time'; | ||
|
|
||
| public function __construct(DateTime $time) |
There was a problem hiding this comment.
I'm afraid, but I just noticed that requiring DateTime is a breaking change. At least as long as this is the constructor for the child classes as well.
To solve this, please override the constructor in the child classes and widen the allowed types so that it's possible to pass what was previously allowed, but cast it to DateTime before calling the parent constructor.
src/Widget/Time.php
Outdated
| * | ||
| * Returns an array with the interval, the formatted string and the type of difference | ||
| * Type can be one of the constants RELATIVE, TIME, DATE, DATETIME | ||
| * Passing null as a parameter will use the current time |
src/Widget/TimeAgo.php
Outdated
| [$time, $type] = $this->diff($this->dateTime); | ||
| $format = $map[$type] ?? $onMessage; | ||
|
|
||
| return sprintf(t(N_($format[0]), N_($format[1])), $time); |
There was a problem hiding this comment.
Seems like you didn't understand how t() and N_() work yet. Look at their implementation, look where they're used (especially N_) and read up what gettext is and how it works.
Then open a PR at https://github.com/Icinga/developer-guidelines-drafts/pulls to explain what you've learned so that others don't make the same mistakes.
Yes, I mean this serious!
There was a problem hiding this comment.
This was a great idea, now i understand how they work!
We want to use DateTime objects for the Time class to work with but the subclasses need to be able to handle also timestamps
This reverts commit 7f1ab16.
Passing a timezone to `new DateTime()` when the input is a Unix timestamp prefixed with `@` is silently ignored by PHP — the timezone is always set to UTC in that case. Fix by calling `setTimezone()` explicitly after construction.
Instead of stripping the sign with Math.abs() before rendering and handling negation inconsistently across different calls, pass the raw signed value through and derive the display sign in render()
The until -> ago transition relies on a cached TimeAgo format. If none is cached yet, the transition silently fails. Fix by pre-assigning the ago label to TimeUntil widgets when they are animated (< 1h), ensuring the format is available when the transition occurs.
No code path in Time::diff() ever returns the DATETIME type, making the constant and its references in the subclass format maps dead code.
This PR removes the direct dependency of the time widgets on Icinga Web 2 (\Icinga\Date\DateFormatter::formatDateTime()) and introduces a native relative-time formatting behavior in IPL Web.
Resolve:
TimeAgo,TimeUntilandTimeSincefrom Icinga Web #341Depends on:
Icingatoipl/webbehaviors icingaweb2#5238(brings the ability to create js-behaviors outside of icingaweb2 )