Skip to content

Conversation

@Lanayx
Copy link
Contributor

@Lanayx Lanayx commented Dec 13, 2025

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

@codecov
Copy link

codecov bot commented Dec 13, 2025

Codecov Report

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

Files with missing lines Patch % Lines
XmlSchemaClassGenerator/Generator.cs 85.71% 1 Missing ⚠️
XmlSchemaClassGenerator/TypeModel.cs 96.55% 0 Missing and 1 partial ⚠️
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              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements support for generating System.DateOnly and System.TimeOnly types for XML Schema xs:date and xs:time types respectively, addressing feature requests #524 and #310. When enabled via the new UseDateOnly configuration option, the generator will produce .NET 6.0+ compatible date and time types instead of the traditional DateTime type.

Key Changes:

  • Adds a new UseDateOnly configuration property that controls whether xs:date and xs:time map to DateOnly/TimeOnly or DateTime
  • Introduces internal placeholder structs for DateOnly and TimeOnly to enable type mapping even when the generator runs on older .NET frameworks (netstandard2.0)
  • Adds CLI support via the --dateOnly (or -do) option
  • Fixes an existing bug where xs:date with DateTimeWithTimeZone=true incorrectly included the DataType attribute

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
XmlSchemaClassGenerator/GeneratorConfiguration.cs Adds the UseDateOnly boolean property with XML documentation
XmlSchemaClassGenerator/Generator.cs Exposes UseDateOnly property and includes minor whitespace formatting improvements
XmlSchemaClassGenerator/CodeUtilities.cs Defines internal DateOnly/TimeOnly structs, updates type mapping logic in GetEffectiveType, adds type reference mapping in CreateTypeReference, and consolidates DataType attribute logic for date/time types
XmlSchemaClassGenerator/TypeModel.cs Adds default value parsing support for DateOnly and TimeOnly using .Parse() methods, includes unrelated indentation/formatting changes
XmlSchemaClassGenerator.Tests/DateTimeTypeTests.cs Fixes test name and assertion to correctly reflect that DataType attribute should NOT be present when using DateTimeOffset with xs:date
XmlSchemaClassGenerator.Tests/DateOnlyTimeOnlyTests.cs Adds comprehensive test coverage for the new DateOnly/TimeOnly feature including default values and interaction with DateTimeWithTimeZone
XmlSchemaClassGenerator.Console/Program.cs Adds --dateOnly command-line option for CLI users

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Lines starting with # and empty lines are ignored.", v => nameSubstituteFiles.Add(v) },
{ "o|output=", "the {FOLDER} to write the resulting .cs files to", v => outputFolder = v },
{ "d|datetime-offset", "map xs:datetime and derived types to System.DateTimeOffset instead of System.DateTime", v => dateTimeWithTimeZone = v != null },
{ "do|dateOnly", "map xs:date to System.DateOnly and xs:time to System.TimeOnly", v => useDateOnly = v != null },
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

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

The new command-line option --dateOnly (or -do) should be documented in the README.md file to match the documentation pattern for other CLI options like --datetime-offset. Users need to be able to discover this new feature through the project documentation.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@mganss
Copy link
Owner

mganss commented Dec 15, 2025

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

@Lanayx Lanayx requested a review from Copilot December 15, 2025 20:32
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Lanayx Lanayx requested a review from Copilot December 15, 2025 22:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

-d, --datetime-offset map xs:datetime, xs:date and xs:time to System.
DateTimeOffset instead of System.DateTime
--do, --dateOnly map xs:date and xs:time to System.DateOnly and
System.TimeOnly instead of System.DateTime
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

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

The documentation should clarify that DateOnly and TimeOnly are only available in .NET 6+ to help users understand framework requirements.

Suggested change
System.TimeOnly instead of System.DateTime
System.TimeOnly instead of System.DateTime
(requires .NET 6.0 or later)

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This doesn't seem to be needed

@mganss mganss merged commit 86d8bd9 into mganss:master Dec 16, 2025
3 checks passed
@mganss
Copy link
Owner

mganss commented Dec 16, 2025

@Lanayx Thanks!

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.

Option to use DateOnly rather than DateTime when using xsd date type

2 participants