Enhance CalDateTime and DateTimeSerializer#638
Conversation
01de82a to
0743c68
Compare
0743c68 to
a4cd5c0
Compare
08c7ff1 to
28df6fd
Compare
69f6508 to
a5b1bf5
Compare
Ical.Net/DataTypes/CalDateTime.cs
Outdated
| if (isEmpty) | ||
| if (string.IsNullOrWhiteSpace(value)) | ||
| { | ||
| Initialize(_value, _timeOnly.HasValue, value, Calendar); |
There was a problem hiding this comment.
Changing the TzId now initializes with immediate effect
7ff8e90 to
edf6103
Compare
Ical.Net/DataTypes/CalDateTime.cs
Outdated
| { | ||
| // The date and time parts that were used to initialize the instance | ||
| // or by the Value setter. | ||
| private DateTime _value; |
There was a problem hiding this comment.
I think, we shouldn't maintain both, the DateTime value and Date-/TimeOnly. I suggest to remove _value altogether.
There was a problem hiding this comment.
Without the backing value we lose the HasDate and HasTime "toggle". The Date-/TimOnly fields are just the replacement for the backing _hasDate and _hasTime booleans. This implementation is mainly because HasDate and HasTime require a setter from the IDateTime interface.
There was a problem hiding this comment.
I thought it would be an option to calculate the DateTime value from the other two just in time, when needed.
There was a problem hiding this comment.
I think I get your point: HasDate and HasTime wouldn't have backing fields, they would just become pure boolean getters/setters. Then _value can disappear. Yep, that's better.
There was a problem hiding this comment.
Will be updated as proposed.
Ical.Net/DataTypes/CalDateTime.cs
Outdated
| private void Initialize(DateTime value, string tzId, Calendar cal) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(tzId) && !tzId.Equals("UTC", StringComparison.OrdinalIgnoreCase)) | ||
| if ((tzId != null && !string.IsNullOrWhiteSpace(tzId) && !tzId.Equals("UTC", StringComparison.OrdinalIgnoreCase)) |
There was a problem hiding this comment.
I know, it's legacy (so nothing to necessarily deal with in this PR), but I think that we shouldn't have special handling for white space, or empty strings. Either the value is set (!= null) or not (== null). Input sanitization is not something this class should be responsible for.
There was a problem hiding this comment.
Will be updated as proposed.
Ical.Net/DataTypes/CalDateTime.cs
Outdated
| TzId = tzId; | ||
| _tzId = tzId; | ||
|
|
||
| initialValue = DateTime.SpecifyKind(dateTime, DateTimeKind.Local); |
There was a problem hiding this comment.
Also legacy, but IMHO important: I think we should avoid DateTimeKind.Local wherever we can, because dealing with the system's local time really is a special case. If no tzid is specified, this would usually mean that the instance represents floating time (which btw cannot be converted to a different time zone). Floating time should rather be represented by DateTimeKind.Unspecified than .Local.
There was a problem hiding this comment.
Will be updated as proposed.
|
@axunonb I only had a very brief look at the new CalDateTime class. While I think that this PR brings some improvements, I feel that the whole design of the CalDateTime class deserves some more attention and improvements. Just a few thoughts:
jm2c |
|
Todo in this PR: Use of
|
* Introduced new methods and test cases in `CalDateTimeTests.cs` to ensure correct behavior of `CalDateTime` properties and methods. * Updated various tests in `DeserializationTests.cs` and `RecurrenceTests.cs` to handle `CalDateTime` without time components and improve clarity. * Refined `IsAllDay` property in `CalendarEvent.cs` and updated methods in `UniqueComponent.cs` and `VTimeZone.cs` to use `CalDateTime` with time components. * Removed outdated comments and improved code formatting. * Enabled nullable reference types and updated `IDateTime` interface. * Added new methods and properties to `CalDateTime.cs` for better date and time management. * Refactored methods in `Period`, `RecurrencePatternEvaluator`, and `RecurringEvaluator` classes to handle `HasTime` property correctly. * Improved `DateTimeSerializer` class for better performance and handling of nullable types. * Added XML documentation and marked obsolete methods. Resolves ical-org#630 Resolves ical-org#633 Resolves ical-org#635 Resolves ical-org#636 Resolves ical-org#637
1d15f23 to
54ee355
Compare
Depending on the time offset from local time to UTC the tests failed. Changed all date kinds to UTC.
54ee355 to
e7f772f
Compare
* `if (Math.Abs(right.TotalDays % 1) > AlmostZeroEpsilon)` is now `if ((right.Ticks % TimeSpan.TicksPerDay) != 0)` * `DateUtil.DateTime GetSimpleDateTimeData(IDateTime dt)` returns `dt.Value` * Move internal `DateUtil.SimpleDateTimeToMatch` as private to `RecurrenceTests` * Move internal `DateUtil.MatchTimeZone` as private to `RecurrencePatternEvaluator` * Create tasks in ical-org#646 and ical-org#647 for new PRs
Also refactor for SonarCloud complaints
b8a64e7 to
7bf9c3b
Compare
|
|
@minichma Merged as you already reviewed twice :) |
|
Sorry for not being able to review this any earlier. Anyhow, this is a significant improvement, congrats! |



Breaking Changes
Calendarargument to constructor overload ofCalDateTimeCalDateTilmeonly usesDateTilmeKind.UtcandDateTimeKind.Unspecified.DateTimeKind.Localhas been dropped.nullfor the timezone will apply the systems local time withAsDateTimeOffset`.DateTimeKindof aDateTimevalue. This is also the case, if a UTC date and a timezone (not null) is used.CalDateTime.TzId: Will not read or writeCalDateTime.Parameters. The functionality has been moved to `DateTimeSerializer.Deserialize(TextReader)DateTimeSerializer.Deserialize(TextReader)will now throw for unrepresentable date/time values.CalDateTime.TzIdcan benullor a valid timezone ID. Whitespace is not allowed.CalDateTime
DateOnlyandTimeOnlyin order to return correct values forCalDateTime.HasTimeandCalDateTime.HasDate, even if hour, minutes and seconds are zero.ValueorTzIdwill re-initialize the instance in the same way as with CTOR overloads.ToStringoverloads to convert the date and time to a string representation. Thanks to @nzbart for his initial PR.DateTimeSerializer
SerializeToStringmethod to ensure the TZID parameter is correctly handled based on whether the date-time is in UTC.DateOnlyandTimeOnlyparts accordingly.Other
IDateTimePortable.System.DateTimeOnlyfor netstandard2.0 and netstandard2.1 for compatibility with net6.0 and later.Resolves #630
Resolves #633
Resolves #635
Resolves #636
Resolves #637
Resolves #197