Skip to content

feat: Implement xsd:time codec and data type#77

Open
mark-dropbear wants to merge 1 commit intomainfrom
time
Open

feat: Implement xsd:time codec and data type#77
mark-dropbear wants to merge 1 commit intomainfrom
time

Conversation

@mark-dropbear
Copy link
Contributor

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 xsd:time data type, enhancing the library's capability to handle XML Schema time values. It introduces a robust XsdTime class that accurately parses and formats time strings, including timezone information and fractional seconds, and provides a dedicated codec for serialization and deserialization. This feature ensures compliance with XSD 1.1 specifications for time representation.

Highlights

  • New xsd:time Data Type: Introduced a new XsdTime class to represent XSD time values, supporting fractional seconds and timezone offsets.
  • xsd:time Codec Implementation: Implemented XsdTimeCodec for seamless conversion between XsdTime objects and their string representations.
  • Special '24:00:00' Handling: Added specific logic to correctly parse '24:00:00' as '00:00:00' in accordance with XSD specifications.
  • Documentation and Testing: Updated CHANGELOG.md and README.md to reflect the new feature, and added comprehensive unit tests for both the XsdTime class and its codec.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +93 to +99
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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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+$'), '');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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, '');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant