Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

fix(ical): preserve second-precision time zone offsets

Stamina94eb8c3adf824
diff --git a/src/Filee.Engines/Data/ICalendar.cs b/src/Filee.Engines/Data/ICalendar.cs
index f84998b8..345440f0 100644
--- a/src/Filee.Engines/Data/ICalendar.cs
+++ b/src/Filee.Engines/Data/ICalendar.cs
@@ -232,8 +232,9 @@ internal static partial class ICalendar
{
if (zones.TryGetValue(id, out var definition) && !definition.HasRule && definition.Transitions.Count > 0)
{
- var ordered = definition.Transitions.OrderBy(t => new DateTimeOffset(t.Local, t.From)).ToList();
- var found = ordered.LastOrDefault(t => new DateTimeOffset(t.Local, t.From) <= instant);
+ // DateTimeOffset only accepts whole-minute offsets; VTIMEZONE also allows seconds.
+ var ordered = definition.Transitions.OrderBy(t => t.Local - t.From).ToList();
+ var found = ordered.LastOrDefault(t => t.Local - t.From <= instant.UtcDateTime);
return found == default ? ordered[0].From : found.To;
}
try { return TimeZoneInfo.FindSystemTimeZoneById(id).GetUtcOffset(instant); }
@@ -254,7 +255,17 @@ internal static partial class ICalendar
return text + "Z";
var id = property.Parameter("TZID");
var offset = id.Length == 0 ? null : Offset(id, value, zones);
- return offset is null ? text : new DateTimeOffset(value, offset.Value).ToString("yyyy-MM-dd'T'HH:mm:sszzz", CultureInfo.InvariantCulture);
+ return offset is null ? text : DateCellAtOffset(value, offset.Value);
+ }
+
+ private static string DateCellAtOffset(DateTime local, TimeSpan offset)
+ {
+ // ISO offset strings in our CSV and DateTimeOffset cannot represent offset seconds or offsets beyond 14 hours.
+ // A UTC cell preserves the instant without rounding it.
+ if (offset.Ticks % TimeSpan.TicksPerMinute != 0 || offset.Duration() > TimeSpan.FromHours(14))
+ return (local - offset).ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture);
+ return new DateTimeOffset(DateTime.SpecifyKind(local, DateTimeKind.Unspecified), offset)
+ .ToString("yyyy-MM-dd'T'HH:mm:sszzz", CultureInfo.InvariantCulture);
}
private readonly record struct CalendarDuration(TimeSpan Days, TimeSpan Clock);
@@ -333,9 +344,9 @@ internal static partial class ICalendar
if (!date && !utc && duration.Clock != TimeSpan.Zero && zone.Length > 0
&& Offset(zone, dayEnd, component.Zones) is { } dayOffset)
{
- var instant = new DateTimeOffset(dayEnd, dayOffset).Add(duration.Clock);
+ var instant = new DateTimeOffset(dayEnd - dayOffset, TimeSpan.Zero).Add(duration.Clock);
if (OffsetAtInstant(zone, instant, component.Zones) is { } endOffset)
- endCell = instant.ToOffset(endOffset).ToString("yyyy-MM-dd'T'HH:mm:sszzz", CultureInfo.InvariantCulture);
+ endCell = DateCellAtOffset(instant.UtcDateTime + endOffset, endOffset);
}
}
rows.Add(
diff --git a/tests/Filee.Engines.Tests/ICalendarTests.cs b/tests/Filee.Engines.Tests/ICalendarTests.cs
index 446e54f1..e1fd64bc 100644
--- a/tests/Filee.Engines.Tests/ICalendarTests.cs
+++ b/tests/Filee.Engines.Tests/ICalendarTests.cs
@@ -149,6 +149,38 @@ public class ICalendarTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.EndsWith("+03:00", Cell(rows, 4, "Start"));
}
+ [Fact]
+ public async Task Custom_zone_with_second_offsets_keeps_instants_and_duration()
+ {
+ var zone = string.Join("\r\n", "BEGIN:VTIMEZONE", "TZID:Custom/Seconds", "BEGIN:STANDARD",
+ "DTSTART:19000101T000000", "TZOFFSETFROM:+001932", "TZOFFSETTO:+001932", "END:STANDARD",
+ "BEGIN:DAYLIGHT", "DTSTART:19000601T020000", "TZOFFSETFROM:+001932", "TZOFFSETTO:+012032",
+ "END:DAYLIGHT", "END:VTIMEZONE");
+ var rows = await ReadCalendar(Calendar(zone,
+ Event("before", "DTSTART;TZID=Custom/Seconds:19000601T013000", "DURATION:PT2H"),
+ Event("after", "DTSTART;TZID=Custom/Seconds:19000601T040000",
+ "DTEND;TZID=Custom/Seconds:19000601T050000")));
+
+ Assert.Equal("1900-06-01T01:10:28Z", Cell(rows, 1, "Start"));
+ Assert.Equal("1900-06-01T03:10:28Z", Cell(rows, 1, "End"));
+ Assert.Equal("1900-06-01T02:39:28Z", Cell(rows, 2, "Start"));
+ Assert.Equal("1900-06-01T03:39:28Z", Cell(rows, 2, "End"));
+ }
+
+ [Theory]
+ [InlineData("+001932", "1900-06-01T11:40:28Z")]
+ [InlineData("-003015", "1900-06-01T12:30:15Z")]
+ [InlineData("+1500", "1900-05-31T21:00:00Z")]
+ [InlineData("+053000", "1900-06-01T12:00:00+05:30")]
+ public async Task Custom_offset_is_exported_without_losing_its_instant(string offset, string expected)
+ {
+ var zone = string.Join("\r\n", "BEGIN:VTIMEZONE", "TZID:Custom/Fixed", "BEGIN:STANDARD",
+ "DTSTART:19000101T000000", "TZOFFSETFROM:" + offset, "TZOFFSETTO:" + offset,
+ "END:STANDARD", "END:VTIMEZONE");
+ var rows = await ReadCalendar(Calendar(zone, Event("fixed", "DTSTART;TZID=Custom/Fixed:19000601T120000")));
+ Assert.Equal(expected, Cell(rows, 1, "Start"));
+ }
+
[Fact]
public async Task Rdate_transitions_before_between_after_and_tab_fold_with_escapes()
{