XmlSchemaClassGenerator icon indicating copy to clipboard operation
XmlSchemaClassGenerator copied to clipboard

DateOnly and TimeOnly implementation

Open Lanayx opened this issue 2 weeks ago • 1 comments

Walkthrough - Implement DateOnly/TimeOnly Support

I have implemented support for generating System.DateOnly and System.TimeOnly types for xs:date and xs:time XML schema types.

Changes

1. GeneratorConfiguration.cs and Generator.cs

Added a new configuration property UseDateOnly.

// GeneratorConfiguration.cs
public bool UseDateOnly { get; set; } = false;

// Generator.cs
public bool UseDateOnly
{
    get { return _configuration.UseDateOnly; }
    set { _configuration.UseDateOnly = value; }
}

2. CodeUtilities.cs

  • Added internal DateOnly and TimeOnly structs to allow the generator to identify these types even when running on older .NET frameworks (netstandard2.0).
  • Updated GetEffectiveType to map XmlTypeCode.Date and XmlTypeCode.Time to these internal types when UseDateOnly is true.
  • Updated CreateTypeReference to map the internal types to System.DateOnly and System.TimeOnly string references in the generated code.

3. TypeModel.cs

Updated GetDefaultValueFor to handle standard default value parsers for DateOnly and TimeOnly (using .Parse()).

4. Program.cs

Added a new command line option --dateOnly (or -do) to the CLI.

xscgen -do schema.xsd

Verification

I added a new test file XmlSchemaClassGenerator.Tests/DateOnlyTimeOnlyTests.cs and confirmed that:

  • When -do is enabled, xs:date generates public System.DateOnly PropertyName.
  • When -do is enabled, xs:time generates public System.TimeOnly PropertyName.
  • When disabled, it falls back to DateTime.
  • Verified that default values for xs:date and xs:time are correctly parsed using System.DateOnly.Parse and System.TimeOnly.Parse.
  • Verified that xs:time and xs:date correctly map to DateTimeOffset when UseDateOnly is false and DateTimeWithTimeZone is true.

Usage

To use the new feature via CLI:

xscgen -do input.xsd

To use via code:

var generator = new Generator
{
    UseDateOnly = true
};
generator.Generate(schemaSet);

(implemented by Gemini Pro 3)

This should fix #524 and #310

Lanayx avatar Dec 13 '25 19:12 Lanayx

Codecov Report

:x: Patch coverage is 95.74468% with 2 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 94.27%. Comparing base (6f928ee) to head (78ff980). :warning: Report is 8 commits behind head on master.

Files with missing lines Patch % Lines
XmlSchemaClassGenerator/Generator.cs 85.71% 1 Missing :warning:
XmlSchemaClassGenerator/TypeModel.cs 96.55% 0 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #573      +/-   ##
==========================================
+ Coverage   94.24%   94.27%   +0.02%     
==========================================
  Files          21       21              
  Lines        3216     3232      +16     
  Branches      509      516       +7     
==========================================
+ Hits         3031     3047      +16     
  Misses        123      123              
  Partials       62       62              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Dec 13 '25 20:12 codecov[bot]

@copilot open a new pull request to apply changes based on the comments in this thread

mganss avatar Dec 15 '25 16:12 mganss

@Lanayx Thanks!

mganss avatar Dec 16 '25 11:12 mganss