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 Period converter support #21136
Add Period converter support #21136
Conversation
Add basic support to convert String to Period. See spring-projectsGH-21080
Add basic support to convert Period to String.
Add initial values to bind periods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Grubhart good job! just left few small comments. Also, perform ./gradlew format
command.
|
||
/** | ||
* {@link Converter} to convert from a {@link Number} to a {@link Period}. Supports | ||
* {@link Period#parse(CharSequence)} as well a more readable {@code 10s} form. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since Period
doesn't support time I think we can change 10s
to 10m
public abstract String print(Period value, ChronoUnit unit); | ||
|
||
/** | ||
* Detect the style then parse the value to return a duration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change duration
to period
/** | ||
* Detect the style then parse the value to return a duration. | ||
* @param value the value to parse | ||
* @return the parsed duration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change duration
to period
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link DurationToStringConverter}. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is PeriodToStringConverter
} | ||
|
||
public Period parse(String value) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove empty line
Add converter support for `javax.time.Period` including: String -> Period Number -> Period Period -> String Period to Number conversion is not supported since `Period` has no ability to deduce the number of calendar days in the period. See gh-21136
Polish period converter support, primarily by changing `PeriodStyle` to parse and print periods that include more than one unit. See gh-21136
Thanks very much @Grubhart and @eddumelendez. I've managed to squeeze this into 2.3.0. I have made some slight tweaks in commit 5ae623c to ensure that periods with more than one unit can be printed and parsed. I.e |
/** | ||
* Simple formatting, for example '1d'. | ||
*/ | ||
SIMPLE("^([\\+\\-]?\\d+)([a-zA-Z]{0,2})$") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to see this! My little grain of sand.
You don't need to escape +
and -
(if it's at the end) in a character class
SIMPLE("^([+-]?\\d+)([a-zA-Z]{0,2})$")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was accident that came about because I copy/pasted some code from Period
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll apply the same fix to the other patterns that we define.
Added PeriodToString, PeriodToNumber, and StringToPeriod Classes
see gh-21080