Repository navigation
[RFC] ext/intl: Implement IntlRelativeDateTimeFormatter - #23514
Open
LamentXU123 wants to merge 2 commits into
Open
LamentXU123 wants to merge 2 commits into
LamentXU123 wants to merge 2 commits into
Conversation
LamentXU123
force-pushed
the
reldateformatter
branch
from
August 31, 2026 10:54
85eca77 to
f971830
Compare
devnexen
reviewed
Sep 7, 2026
LamentXU123
force-pushed
the
reldateformatter
branch
from
September 8, 2026 08:39
4b2eee8 to
97450a4
Compare
LamentXU123
force-pushed
the
reldateformatter
branch
from
October 13, 2026 08:30
290bb7c to
665fc2b
Compare
LamentXU123
marked this pull request as ready for review
October 13, 2026 08:31
Member
Author
|
Okay this is now fine for reviews. Given the current voting status this should be accepted. https://wiki.php.net/rfc/reldateformatter |
IntlRelativeDateTimeFormatterIntlRelativeDateTimeFormatter
devnexen
reviewed
Oct 13, 2026
devnexen
reviewed
Oct 13, 2026
| using icu::Locale; | ||
| using icu::UnicodeString; | ||
|
|
||
| struct reldateformatter_data { |
Member
There was a problem hiding this comment.
--- a/ext/intl/reldateformatter/reldateformatter_class.cpp
+++ b/ext/intl/reldateformatter/reldateformatter_class.cpp
@@
struct reldateformatter_data {
+ Locale locale;
std::unique_ptr<icu::RelativeDateTimeFormatter> formatter;
std::unique_ptr<icu::BreakIterator> sentence_iterator;
};
@@ static reldateformatter_data *reldateformatter_open(
- Locale icu_locale(locale);
+ result->locale = Locale(locale);
if (capitalization_context == UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) {
/* ICU can fail to create this iterator before adopting the number formatter. */
- result->sentence_iterator.reset(icu::BreakIterator::createSentenceInstance(icu_locale, *status));
+ result->sentence_iterator.reset(icu::BreakIterator::createSentenceInstance(result->locale, *status));
@@
result->formatter.reset(new icu::RelativeDateTimeFormatter(
- icu_locale, number_formatter_clone.release(), style, capitalization_context, *status));
+ result->locale, number_formatter_clone.release(), style, capitalization_context, *status));
@@ static zend_string *reldateformatter_format_result(
- result.toTitle(sentence_iterator, Locale(ZSTR_VAL(obj->locale)),
+ result.toTitle(sentence_iterator, RELDATEFORMATTER_OBJECT(obj)->locale,
U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT);
Member
There was a problem hiding this comment.
apart of this looks ok even tough C++11 builds might worry me a little bit (maybe for nothing).
Member
Author
There was a problem hiding this comment.
Good point, Thank you!
Member
Author
There was a problem hiding this comment.
C++11 builds might worry me a little bit
Well Actually only starting from C++17 we have define the order of release().
Ah, I think we should instead pass get() when contructing and after the return value of new is not empty we release. And check for ICU status code.
Member
Author
There was a problem hiding this comment.
i.e.
--- a/ext/intl/reldateformatter/reldateformatter_class.cpp
+++ b/ext/intl/reldateformatter/reldateformatter_class.cpp
@@ -241,18 +241,20 @@ static reldateformatter_data *reldateformatter_open(
}
}
- /* If allocation fails, the constructor arguments (including release()) are not evaluated.
- * With a valid style/context and no sentence iterator to create, ICU adopts the clone
- * before any initialization failure. */
+ /* C++11 may evaluate constructor arguments even if allocation fails, so keep
+ * ownership of the clone until we know that the constructor ran. */
result->formatter.reset(new icu::RelativeDateTimeFormatter(
- result->locale, number_formatter_clone.release(), style, capitalization_context, *status));
- if (U_FAILURE(*status)) {
- return nullptr;
- }
+ result->locale, number_formatter_clone.get(), style, capitalization_context, *status));
if (result->formatter == nullptr) {
*status = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}
+ /* With a valid style/context and no sentence iterator to create, the constructor
+ * adopts the clone even if initialization fails. Release it before checking status. */
+ number_formatter_clone.release();
+ if (U_FAILURE(*status)) {
+ return nullptr;
+ }
return result.release();
}
LamentXU123
force-pushed
the
reldateformatter
branch
from
October 13, 2026 16:41
cdc86a3 to
3faeeb5
Compare
Add locale-aware formatting of relative offsets, enum-based style, capitalization and unit options, and custom NumberFormatter support. Observe subsequent NumberFormatter configuration changes and cover formatting, error handling, and object lifetime in tests. RFC: https://wiki.php.net/rfc/reldateformatter Co-authored-by: NickSdot <32384907+NickSdot@users.noreply.github.com> Co-authored-by: David Carlier <devnexen@gmail.com>
LamentXU123
force-pushed
the
reldateformatter
branch
from
October 13, 2026 16:52
3faeeb5 to
bfc5807
Compare
devnexen
reviewed
Oct 13, 2026
| #include <unicode/uchar.h> | ||
| #include <unicode/ucasemap.h> | ||
| #include <unicode/udisplaycontext.h> | ||
| #include <unicode/unum.h> |
|
|
||
| zend_object_std_init(&obj->zo, class_type); | ||
| object_properties_init(&obj->zo, class_type); | ||
| obj->zo.handlers = &reldateformatter_handlers; |
Drop the unused unum.h include and rely on zend_object_std_init() to copy the class's default object handlers. Co-authored-by: David Carlier <devnexen@gmail.com>
devnexen
approved these changes
Oct 13, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Implement: https://wiki.php.net/rfc/reldateformatter
Discussion: https://externals.io/message/132187
TODO:
After these, this PR is ready for reviews. However, I am glad to see any ideas code-wise anytime if possible :)