Skip to content

Remove date and time arithmetic from CalDateTime#930

Merged
axunonb merged 3 commits intoversion/6.0from
maknapp/remove-caldatetime-arithmetic
Mar 15, 2026
Merged

Remove date and time arithmetic from CalDateTime#930
axunonb merged 3 commits intoversion/6.0from
maknapp/remove-caldatetime-arithmetic

Conversation

@maknapp
Copy link
Copy Markdown
Collaborator

@maknapp maknapp commented Feb 28, 2026

From discussion in #849 and #854, one way to resolve math issues is to leave math to NodaTime (or System.DateTime). This removes all Add*() methods from CalDateTime and replaces usage by converting to NodaTime and back.

Doing this adds more support towards having NodaTime in the public API. Making it easy to convert to and from NodaTime types is important.

While this change does make sense, it does complicate things significantly for users who use Add*(). For those unfamiliar with NodaTime, they would have to learn NodaTime while also understanding that CalDateTime can hold date, datetime, or datetime with time zone without offset. CalDateTime will need to be converted to the correct NodaTime type based on the data it holds and what date/time units are being added. This will need to be well documented.

Fixes #849

@maknapp maknapp requested a review from axunonb February 28, 2026 14:55
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Impacted file tree graph

@@              Coverage Diff              @@
##           version/6.0    #930     +/-   ##
=============================================
+ Coverage         70.2%   70.3%   +0.1%     
=============================================
  Files              118     118             
  Lines             4645    4622     -23     
  Branches          1094    1086      -8     
=============================================
- Hits              3260    3247     -13     
+ Misses            1025    1013     -12     
- Partials           360     362      +2     
Files with missing lines Coverage Δ
Ical.Net/CalendarComponents/VTimeZone.cs 72.6% <100.0%> (ø)
Ical.Net/DataTypes/CalDateTime.cs 90.1% <ø> (-1.4%) ⬇️
Ical.Net/DataTypes/Duration.cs 80.6% <100.0%> (+10.3%) ⬆️
Ical.Net/NodaTimeExtensions.cs 100.0% <100.0%> (ø)
Ical.Net/Utility/DateUtil.cs 100.0% <ø> (ø)

... and 1 file with indirect coverage changes

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

@axunonb
Copy link
Copy Markdown
Collaborator

axunonb commented Mar 8, 2026

While this change does make sense, it does complicate things significantly for users who use Add*(). For those unfamiliar with NodaTime,

We already have a couple of breaking changes in terms of CalDatetime

  • Date and Time now return NodaTime types
  • Comparison operators and several methods removed
  • Replaced CalDateTime for occurrences

So this step is just kind of finalizing the direction we took in v6.

As you know I was hoping for an easy migration path from v5 to v6. Now v6 requires a direct dependency on NodaTime and users will have to learn it and rewrite their current ical.net integration.

}
}

#if NET6_0_OR_GREATER
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could be removed in favor of NodaTime.

/// </summary>
public LocalTime? Time => _localTime;

#if NET6_0_OR_GREATER
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could be removed in favor of NodaTime.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think it is better to keep these. These are simple convenience methods that do not add complexity. For those who refuse to use NodaTime, these could be more useful than being stuck with only DateTime.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Those ical.net users who refuse to use NodaTime will have a difficult time ahead of them 😉

{
return ToLocalDateTime().InUtc();
}
else
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Redundant else

{
return ToLocalDateTime().InZoneLeniently(timeZone);
}
else
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Redundant else


public ZonedDateTime ToZonedDateTime(string zoneId)
{
return ToZonedDateTime(DateUtil.GetZone(zoneId));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe use expression body?

{
return ToLocalDateTime().InZoneLeniently(timeZone);
}
else
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Redundant else

@@ -464,103 +464,6 @@
return new(ToZonedDateTime(otherTzId));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Add a comment that this drops the timezone offset and converting back to ZonedDatetime will not resolve the same value?

Comment thread Ical.Net/DataTypes/Duration.cs

namespace Ical.Net.Utility;

internal static class DateUtil
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we still need this class?
WeekOfMonth(DateTime d) has just one reference

axunonb
axunonb previously approved these changes Mar 8, 2026
Copy link
Copy Markdown
Collaborator

@axunonb axunonb left a comment

Choose a reason for hiding this comment

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

See few minor comments

}

public NodaTime.Period ToPeriod()
public static Duration FromPeriod(NodaTime.Period p)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not for this PR: Should we remove the Period ambiguity between NodaTime and ical.net by using another name, e.g. CalPeriod?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I would like all NodaTime name overlaps to be renamed - CalPeriod, CalDuration, and any others.

@maknapp
Copy link
Copy Markdown
Collaborator Author

maknapp commented Mar 8, 2026

Do we still need this class? WeekOfMonth(DateTime d) has just one reference

I have been looking at #749 and handling time zones defined in the calendar. This will remove the last major reference to DateUtil.

Add a comment that this drops the timezone offset and converting back to ZonedDatetime will not resolve the same value?

This and the many redundant else issues can be in a separate PR. It might make more sense to move all ToZonedDateTime() related methods from CalDateTime to a TimeZoneResolver that can handle calendar time zones.

@maknapp maknapp force-pushed the maknapp/remove-caldatetime-arithmetic branch from 889dd85 to 3a3fab1 Compare March 9, 2026 18:36
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Mar 9, 2026

@maknapp maknapp requested a review from axunonb March 12, 2026 11:59
Copy link
Copy Markdown
Collaborator

@axunonb axunonb left a comment

Choose a reason for hiding this comment

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

LG, tnx

@axunonb axunonb merged commit 5e47a7d into version/6.0 Mar 15, 2026
10 checks passed
@axunonb axunonb deleted the maknapp/remove-caldatetime-arithmetic branch March 15, 2026 20:53
axunonb pushed a commit that referenced this pull request Mar 15, 2026
* Remove time arithmetic from CalDateTime

* Remove date arithmetic from CalDateTime

* Add tests for Duration
@axunonb
Copy link
Copy Markdown
Collaborator

axunonb commented Mar 15, 2026

@maknapp After merging this PR I performed a rebase of version/6.0. (There's a temporary version/6.0-backup with a copy before the rebase.)
I added a commit Fix file encoding: convert .cs files from UTF-16 LE to UTF-8. This mixed encoding gave me a hard time during the rebase.
Probably the .gitattributes and .editorconfig must be updated. Then this should not happen again. I'll submit a PR for that.

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.

2 participants