feat: Implement xsd:time codec and data type#77
Conversation
Summary of ChangesHello @mark-dropbear, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request delivers full support for the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces support for the xsd:time datatype, including the XsdTime class, a corresponding XsdTimeCodec, and unit tests. The implementation is comprehensive and handles various aspects of the xsd:time specification, such as floating times, timezones, and the special '24:00:00' case. I've identified a critical issue in the parsing logic for fractional seconds that needs to be addressed. Additionally, there are a couple of minor performance optimizations that can be made by reusing RegExp objects. The tests are well-written, though expanding them to cover more fractional second edge cases would be beneficial. Overall, this is a solid addition to the library.
| if (fractionStr != null) { | ||
| // fractionStr includes the dot, e.g. ".123" | ||
| final fractionalPart = double.parse(fractionStr); | ||
| final ms = (fractionalPart * 1000).round(); | ||
| millisecond = ms % 1000; | ||
| microsecond = ((fractionalPart * 1000000).round()) % 1000; | ||
| } |
There was a problem hiding this comment.
The current logic for parsing fractional seconds is flawed. Using double.parse can introduce precision errors, and the subsequent calculations for milliseconds and microseconds are incorrect for certain inputs (e.g., those with more than 3 fractional digits).
A more robust approach is to handle the fractional part as a string to avoid floating-point inaccuracies and ensure correct parsing.
if (fractionStr != null) {
// fractionStr includes the dot, e.g. ".123456"
var fraction = fractionStr.substring(1);
// DateTime supports up to 6 digits for fractional seconds (microseconds).
// Truncate if longer.
if (fraction.length > 6) {
fraction = fraction.substring(0, 6);
}
// Pad with zeros to make it 6 digits long for easy parsing.
final paddedFraction = fraction.padRight(6, '0');
millisecond = int.parse(paddedFraction.substring(0, 3));
microsecond = int.parse(paddedFraction.substring(3, 6));
}| // Special case for 24:00:00 which is valid in XSD but not in DateTime | ||
| if (input.startsWith('24:00:00')) { | ||
| // Check if it has fractional seconds that are all zero | ||
| final fractionalMatch = RegExp(r'^24:00:00(\.0+)?').firstMatch(input); |
There was a problem hiding this comment.
The RegExp for matching 24:00:00 is created on each call to parse. For better performance, this should be defined as a static final field at the class level and reused here.
For example, you can add:
static final _twentyFourHourRegex = RegExp(r'^24:00:00(\.0+)?');And then use it in this line.
| final fractionalMatch = RegExp(r'^24:00:00(\.0+)?').firstMatch(input); | |
| final fractionalMatch = _twentyFourHourRegex.firstMatch(input); |
| // For now let's keep it simple and consistent with XsdDateTime | ||
| timePart = '$timePart.$ms$us'; | ||
| // Strip trailing zeros from fraction | ||
| timePart = timePart.replaceAll(RegExp(r'0+$'), ''); |
There was a problem hiding this comment.
The RegExp for stripping trailing zeros is created on each call to toString. For better performance, this should be defined as a static final field at the class level and reused here.
For example, you can add:
static final _trailingZerosRegex = RegExp(r'0+$');And then use it in this line.
timePart = timePart.replaceAll(_trailingZerosRegex, '');
No description provided.