-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Add support for converting a String to an Instant #30312
Comments
Because the usual way of representing a "time" is by using the number of ms, not seconds. I understand that you'd prefer seconds but that's not what the default representation is at the moment: the seconds variant is merely a shortcut for those who don't need ms precision. Both inputs are numbers so it's impossible for us to know which format you want to use automatically. I can't even suggest to configure a custom converter, unless you want to trump the default one for every fields. Having said all of that, I am not sure I understand what you'd expect from us. Can you clarify? |
Sorry, maybe I need to rephrase my question as that was not what I meant. I absolutely prefer millis and I agree. I was just adding considerations and alternatives that someone might ask for instead.
Sure! (Should I also edit the original question?) Currently it is not possible to use a timestamp as a property of type If you try the code I suggested above, it fails with:
|
Oh. I think the considerations have confused me. Yes, I agree supporting instant would be nice and I don't think we should bother about the seconds variant. |
@snicoll Given the involvement of |
Yes. We do have issues to move some of the converters that we have in Spring Boot already so that makes sense. |
Hey! Can I take this one? :) |
@RemusRD I think it is worth a shot yes. From my perspective it should be possible to retrofit this parsing into existing @Test
void testBindInstantAsLongEpochMillis() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instant", 1234L);
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isZero();
assertThat(binder.getBindingResult().getRawFieldValue("instant"))
.isInstanceOf(Instant.class)
.isEqualTo(Instant.ofEpochMilli(1234L));
assertThat(binder.getBindingResult().getFieldValue("instant"))
.hasToString("1970-01-01T00:00:01.234Z");
} |
I already started working on this! I added your test which seems to be already passing, should I keep it? Also, would be great if you could share with me any other merge requests that are already implemented similar to this one so I can take inspiration :) from. |
I have added more tests and the functionality to the @ParameterizedTest
@ArgumentsSource(RandomInstantProvider.class)
void should_parse_into_an_Instant_from_epoch_mili(Instant input) throws ParseException {
String inputS = String.valueOf(input.toEpochMilli()); // toEpochMilli() will overflow and make the test fail
Instant expected = Instant.ofEpochMilli(Long.parseLong(inputS));
Instant actual = instantFormatter.parse(inputS, null);
assertThat(actual).isEqualTo(expected);
} |
yeah |
any progress @RemusRD ? |
Submitting the PR shortly, I added tests into the InstantFormatter, wondering if it would make sense to do a more end to end test? Like checking it works from parsing a yaml or something like that. |
@simonbasle I have opened the PR, but not sure how I can link it here? #30546 |
|
This commit adds support of parsing a simple long from a String and turning it to an `Instant` by considering it represents a timestamp in milliseconds (see `Instant.ofEpochMilli`). Failing to parse a long from the String, the previous algorithm is used: first check for an RFC-1123 representation then an ISO_INSTANT representation. See gh-30312 Closes gh-30546
What I'm trying to achieve:
with the following
application.properties
:where the value is millis since the epoch, see
Instant.ofEpochMilli()
.Edit:
Currently this fails with a
Perhaps this is a good candidate for a default binding.
Considerations:
Instant.ofEpochMilli()
and notInstant.ofEpochSecond()
? It just felt more natural. Is this uncertainty reason enough to refuse this request? That said, there already is a registered-by-defaultConverter<Long, Instant>
which usesofEpochMilli
. Therefore, an additional Formatter would be consistent with the already-existing Converter.java.util.Date
orjava.sql.Timestamp
? I do no think think so as neither of them has a simpleofEpochMilli()
-like creator method or clear semantics. Otherjava.time
also are not good targets for this unless OffsetDateTime/ZonedDateTime is considered okay given a default timezone.The text was updated successfully, but these errors were encountered: