Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use specified culture/format provider when provided to CalDateTime.ToString #560

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Ical.Net.CoreUnitTests/CalDateTimeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Ical.Net.CalendarComponents;
using Ical.Net.DataTypes;
using NUnit.Framework;
Expand Down Expand Up @@ -150,5 +151,36 @@ public static IEnumerable<ITestCaseData> DateTimeKindOverrideTestCases()
.Returns(DateTimeKind.Unspecified)
.SetName("DateTime with Kind = Unspecified and null tzid returns DateTimeKind.Unspecified");
}

[Test, TestCaseSource(nameof(ToStringTestCases))]
public string ToStringTests(DateTime localDateTime, string format, IFormatProvider formatProvider)
=> new CalDateTime(localDateTime, "Pacific/Auckland").ToString(format, formatProvider);

public static IEnumerable<ITestCaseData> ToStringTestCases()
{
yield return new TestCaseData(new DateTime(2022, 8, 30, 10, 30, 0), null, null)
.Returns($"{new DateTime(2022, 8, 30, 10, 30, 0)} Pacific/Auckland")
.SetName("Date and time with current culture formatting returns string using BCL current culture formatted date and time");

yield return new TestCaseData(new DateTime(2022, 8, 30), null, null)
.Returns($"{new DateTime(2022, 8, 30):d} Pacific/Auckland")
.SetName("Date only with current culture formatting returns string using BCL current culture formatted date");

yield return new TestCaseData(new DateTime(2022, 8, 30, 10, 30, 0), "o", null)
.Returns("2022-08-30T10:30:00.0000000+12:00 Pacific/Auckland")
.SetName("Date and time formatted using format string with no culture returns string using BCL formatter");

yield return new TestCaseData(new DateTime(2022, 8, 30), "o", null)
.Returns("2022-08-30T00:00:00.0000000+12:00 Pacific/Auckland")
.SetName("Date and time formatted using format string with no culture returns string using BCL formatter");

yield return new TestCaseData(new DateTime(2022, 8, 30, 10, 30, 0), null, CultureInfo.InvariantCulture)
.Returns("08/30/2022 10:30:00 Pacific/Auckland")
.SetName("Date and time with format provider");

yield return new TestCaseData(new DateTime(2022, 8, 30), null, CultureInfo.InvariantCulture)
.Returns("08/30/2022 Pacific/Auckland")
.SetName("Date only with current format provider");
}
}
}
4 changes: 2 additions & 2 deletions src/Ical.Net/DataTypes/CalDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,13 @@ public string ToString(string format, IFormatProvider formatProvider)
}
if (HasTime && HasDate)
{
return Value + tz;
return Value.ToString(formatProvider) + tz;
}
if (HasTime)
{
return Value.TimeOfDay + tz;
}
return Value.ToString("d") + tz;
return Value.ToString("d", formatProvider) + tz;
}
}
}