Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
53 changes: 29 additions & 24 deletions dotnet-datetime/DateTimeFormatInCSharp/Test/CustomFormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void WhenUsingCustomFormatString_ThenValuesAreFromattedOrParsed()
var datetime = new DateTime(2017, 8, 24, 14, 35, 0);
Console.WriteLine("{0:MM/dd/yy H:mm:ss}", datetime); // 08/24/17 14:35:00

Assert.AreEqual($"08/24/17 14:35:00{Environment.NewLine}", sw.ToString());
Assert.AreEqual($"08/24/17 14:35:00{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -31,7 +31,7 @@ public void WhenUsingDayFormatSpecifier_ThenDayValuesAreShown()
Console.WriteLine(datetime.ToString("dd MMMM", CultureInfo.CreateSpecificCulture("en-US"))); // 04 August

Assert.AreEqual($"4 August{Environment.NewLine}" +
$"04 August{Environment.NewLine}", sw.ToString());
$"04 August{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -45,7 +45,7 @@ public void WhenUsingDayFormatSpecifier_ThenWeekDaysAreShown()
Console.WriteLine(datetime.ToString("dddd, d MMMM", new CultureInfo("en-US"))); // Friday, 4 August

Assert.AreEqual($"Fri, 4 August{Environment.NewLine}" +
$"Friday, 4 August{Environment.NewLine}", sw.ToString());
$"Friday, 4 August{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -59,7 +59,7 @@ public void WhenUsingMonthFormatSpecifier_ThenMonthValuesAreShown()
Console.WriteLine(datetime.ToString("MM", CultureInfo.CreateSpecificCulture("en-US"))); // 08

Assert.AreEqual($"8{Environment.NewLine}" +
$"08{Environment.NewLine}", sw.ToString());
$"08{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -73,7 +73,7 @@ public void WhenUsingMonthFormatSpecifier_ThenMonthsAreShown()
Console.WriteLine(datetime.ToString("MMMM", CultureInfo.CreateSpecificCulture("en-US"))); // August

Assert.AreEqual($"Aug{Environment.NewLine}" +
$"August{Environment.NewLine}", sw.ToString());
$"August{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -87,7 +87,7 @@ public void WhenUsingSingleCharacterCustomIdentifier_ThenWorksAsCustomFormatStri
Console.WriteLine(datetime.ToString(" y", CultureInfo.CreateSpecificCulture("en-US")).Trim()); // 17

Assert.AreEqual($"4{Environment.NewLine}" +
$"17{Environment.NewLine}", sw.ToString());
$"17{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -105,7 +105,7 @@ public void WhenUsingYearCustomFormatIdentifier_ThenIsShownWithOneToFourChars()
Assert.AreEqual($"1{Environment.NewLine}" +
$"01{Environment.NewLine}" +
$"2001{Environment.NewLine}" +
$"2001{Environment.NewLine}", sw.ToString());
$"2001{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -119,7 +119,7 @@ public void WhenUsingHourFormatSpecifier_ThenOnetoTwelveDigitsAreShown()
Console.WriteLine(datetime.ToString("hh", CultureInfo.CreateSpecificCulture("en-US"))); // 10

Assert.AreEqual($"10{Environment.NewLine}" +
$"10{Environment.NewLine}", sw.ToString());
$"10{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -133,7 +133,7 @@ public void WhenUsingHourFormatSpecifier_ThenOnetoTwentyThreeDigitsAreShown()
Console.WriteLine(datetime.ToString("HH", CultureInfo.CreateSpecificCulture("en-US"))); // 22

Assert.AreEqual($"22{Environment.NewLine}" +
$"22{Environment.NewLine}", sw.ToString());
$"22{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -147,7 +147,7 @@ public void WhenUsingMinuteFormatSpecifier_ThenMinutesAreShown()
Console.WriteLine(datetime.ToString("mm", CultureInfo.CreateSpecificCulture("en-US"))); // 35

Assert.AreEqual($"35{Environment.NewLine}" +
$"35{Environment.NewLine}", sw.ToString());
$"35{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -161,7 +161,7 @@ public void WhenUsingSecondFormatSpecifier_ThenSecondsAreShown()
Console.WriteLine(datetime.ToString("ss", CultureInfo.CreateSpecificCulture("en-US"))); // 15

Assert.AreEqual($"15{Environment.NewLine}" +
$"15{Environment.NewLine}", sw.ToString());
$"15{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -185,7 +185,7 @@ public void WhenUsingSecondFractionsFormatSpecifier_ThenSecondsFractionsAreShown
$"22:35:15.0180{Environment.NewLine}" +
$"22:35:15.01800{Environment.NewLine}" +
$"22:35:15.018000{Environment.NewLine}" +
$"22:35:15.0180000{Environment.NewLine}", sw.ToString());
$"22:35:15.0180000{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -209,7 +209,7 @@ public void WhenUsingSecondFractionsFormatSpecifier_ThenNonZeroSecondsFractionsA
$"22:35:15.018{Environment.NewLine}" +
$"22:35:15.018{Environment.NewLine}" +
$"22:35:15.018{Environment.NewLine}" +
$"22:35:15.018{Environment.NewLine}", sw.ToString());
$"22:35:15.018{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -223,7 +223,7 @@ public void WhenUsingMeridienFormatSpecifier_ThenMeridienLabelIsShown()
Console.WriteLine(datetime.ToString("hh:mm:ss tt", CultureInfo.CreateSpecificCulture("en-US"))); // 10:35:15 PM

Assert.AreEqual($"10:35:15 P{Environment.NewLine}" +
$"10:35:15 PM{Environment.NewLine}", sw.ToString());
$"10:35:15 PM{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -233,13 +233,18 @@ public void WhenUsingTimeZoneFormatSpecifier_ThenDependsOnKindProperty()
Console.SetOut(sw);

var datetimeLocal = new DateTime(2017, 8, 4, 22, 35, 15, DateTimeKind.Local);
Console.WriteLine(datetimeLocal.ToString("%K", CultureInfo.CreateSpecificCulture("en-US"))); // +00:00
Console.WriteLine(datetimeLocal.ToString("%K", CultureInfo.CreateSpecificCulture("en-US"))); // the machine's UTC offset, e.g. +02:00

var datetimeUtc = new DateTime(2017, 8, 4, 22, 35, 15, DateTimeKind.Utc);
Console.WriteLine(datetimeUtc.ToString("%K", CultureInfo.CreateSpecificCulture("en-US"))); // Z

Assert.AreEqual($"+00:00{Environment.NewLine}" +
$"Z{Environment.NewLine}", sw.ToString());
// K on a Local value prints the machine's offset, so the expectation is computed
// rather than hard-coded -- a literal "+00:00" only passes where the clock is UTC.
var localOffset = TimeZoneInfo.Local.GetUtcOffset(datetimeLocal);
var expectedLocalOffset = (localOffset < TimeSpan.Zero ? "-" : "+") + localOffset.ToString(@"hh\:mm");

Assert.AreEqual($"{expectedLocalOffset}{Environment.NewLine}" +
$"Z{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -255,7 +260,7 @@ public void WhenUsingTimeZoneFormatSpecifier_ThenDependsOnInternalOffsetProperty
Console.WriteLine(datetimeUtc.ToString("%K", CultureInfo.CreateSpecificCulture("en-US"))); // +00:00

Assert.AreEqual($"+02:00{Environment.NewLine}" +
$"+00:00{Environment.NewLine}", sw.ToString());
$"+00:00{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -271,7 +276,7 @@ public void WhenUsingTimeZoneFormatSpecifier_ThenSignedTimeZoneIsShown()

Assert.AreEqual($"+2{Environment.NewLine}" +
$"+02{Environment.NewLine}" +
$"+02:00{Environment.NewLine}", sw.ToString());
$"+02:00{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -287,7 +292,7 @@ public void WhenUsingEraFormatSpecifier_ThenEraIsShown()

Assert.AreEqual($"AD{Environment.NewLine}" +
$"AD{Environment.NewLine}" +
$"d. C.{Environment.NewLine}", sw.ToString());
$"d. C.{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -302,7 +307,7 @@ public void WhenUsingCustomTimeSeparator_ThenCustomSeparatorIsUsed()

Console.WriteLine(datetime.ToString("HH:mm:ss", formatInfo)); // 04;22;35

Assert.AreEqual($"04;22;35{Environment.NewLine}", sw.ToString());
Assert.AreEqual($"04;22;35{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -316,7 +321,7 @@ public void WhenUsingCustomDateSeparator_ThenCustomSeparatorIsUsed()
formatInfo.DateSeparator = "-";
Console.WriteLine(datetime.ToString("dd/MM/yyyy", formatInfo)); // 24-08-2017

Assert.AreEqual($"24-08-2017{Environment.NewLine}", sw.ToString());
Assert.AreEqual($"24-08-2017{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -328,7 +333,7 @@ public void WhenUsingLiteralCharacters_ThenCharactersAppearUnchanged()
var datetime = new DateTime(2017, 8, 24, 4, 22, 35, 15);
Console.WriteLine(datetime.ToString("dd/MM/yyyy hh:mm:ss EST", CultureInfo.CreateSpecificCulture("en-US"))); // 24/08/2017 04:22:35 EST

Assert.AreEqual($"24/08/2017 04:22:35 EST{Environment.NewLine}", sw.ToString());
Assert.AreEqual($"24/08/2017 04:22:35 EST{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -342,7 +347,7 @@ public void WhenUsingFormatSpecifiersAsLiteralCharacters_ThenCharactersAppearInT
Console.WriteLine(datetime.ToString("dd/MM/yyyy hh:mm:ss 'gmt'", CultureInfo.CreateSpecificCulture("en-US"))); // 24/08/2017 04:22:35 gmt

Assert.AreEqual($"24/08/2017 04:22:35 gmt{Environment.NewLine}" +
$"24/08/2017 04:22:35 gmt{Environment.NewLine}", sw.ToString());
$"24/08/2017 04:22:35 gmt{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void WhenUsingStandardFormatSpecifiers_ThenCurrentCultureFormatIsApplied(
var datetime = new DateTime(2017, 8, 24);
Console.WriteLine(datetime.ToString("d")); // 8/24/2017

Assert.AreEqual($"8/24/2017{Environment.NewLine}", sw.ToString());
Assert.AreEqual($"8/24/2017{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -34,7 +34,7 @@ public void WhenUsingStandardFormatSpecifiers_ThenOutputChangesAccordingToCultur

Assert.AreEqual($"8/24/2017{Environment.NewLine}" +
$"24/8/2017{Environment.NewLine}" +
$"2017/08/24{Environment.NewLine}", sw.ToString());
$"2017/08/24{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -49,7 +49,7 @@ public void WhenUsingDateTimeFormatInfo_ThenOutputChangesAccordingToCustomFormat

Console.WriteLine(datetime.ToString("d", formatInfo)); // 8-24-2017

Assert.AreEqual($"8-24-2017{Environment.NewLine}", sw.ToString());
Assert.AreEqual($"8-24-2017{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}

[TestMethod]
Expand All @@ -62,11 +62,12 @@ public void WhenUsingFormatSpecifierAndCulture_ThenCanParseCorrectString()
}

[TestMethod]
[ExpectedException(typeof(FormatException))]
public void WhenUsingFormatSpecifierAndWrongCulture_ThenThrowsException()
{
var dateLiteral = "8/24/2017";
DateTime.ParseExact(dateLiteral, "d", CultureInfo.CreateSpecificCulture("ja-JP"));

Assert.ThrowsExactly<FormatException>(() =>
DateTime.ParseExact(dateLiteral, "d", CultureInfo.CreateSpecificCulture("ja-JP")));
}

[TestMethod]
Expand All @@ -82,7 +83,7 @@ public void WhenUsingLongDateStandardFormat_ThenOutputChangesAccordingToCulture(

Assert.AreEqual($"Thursday, August 24, 2017{Environment.NewLine}" +
$"jueves, 24 de agosto de 2017{Environment.NewLine}" +
$"Donnerstag, 24. August 2017{Environment.NewLine}", sw.ToString());
$"Donnerstag, 24. August 2017{Environment.NewLine}", TextExpectation.NormalizeSpaces(sw.ToString()));
}
}
}
Loading
Loading