|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.IO; |
| 5 | +using System.Xml; |
| 6 | +using Xunit; |
| 7 | +using System.Globalization; |
| 8 | + |
| 9 | +namespace System.ServiceModel.Syndication.Tests |
| 10 | +{ |
| 11 | + public class Rfc822DateParsingTests |
| 12 | + { |
| 13 | + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] // NetFx requires double digit dates and four digit years |
| 14 | + [Theory] |
| 15 | + [InlineData("Mon, 2 Jun 2003 09:39:21 GMT", 2003, 6, 2, 9, 39, 21)] |
| 16 | + [InlineData("2 Jun 2003 09:39:21 GMT", 2003, 6, 2, 9, 39, 21)] |
| 17 | + [InlineData("02 Jun 03 09:39:21 GMT", 2003, 6, 2, 9, 39, 21)] |
| 18 | + [InlineData("Mon, 2 Jun 03 09:39:21 GMT", 2003, 6, 2, 9, 39, 21)] |
| 19 | + [InlineData("2 Jun 03 09:39 GMT", 2003, 6, 2, 9, 39, 0)] |
| 20 | + [InlineData("02 Jun 03 09:39 GMT", 2003, 6, 2, 9, 39, 0)] |
| 21 | + [InlineData("Mon, 2 Jun 03 09:39 GMT", 2003, 6, 2, 9, 39, 0)] |
| 22 | + [InlineData("Mon, 02 Jun 03 09:39 GMT", 2003, 6, 2, 9, 39, 0)] |
| 23 | + public void Rss20ItemFormatter_Read_SingleDigitDay_And_TwoDigitYear(string pubDate, int year, int month, int day, int hour, int minute, int second) |
| 24 | + { |
| 25 | + string xml = $"<item><pubDate>{pubDate}</pubDate></item>"; |
| 26 | + using var stringReader = new StringReader(xml); |
| 27 | + using var reader = XmlReader.Create(stringReader); |
| 28 | + var formatter = new Rss20ItemFormatter(); |
| 29 | + formatter.ReadFrom(reader); |
| 30 | + DateTimeOffset expected = new DateTimeOffset(year, month, day, hour, minute, second, TimeSpan.Zero); |
| 31 | + Assert.Equal(expected, formatter.Item.PublishDate); |
| 32 | + } |
| 33 | + |
| 34 | + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] // NetFx requires double digit dates and four digit years |
| 35 | + [Theory] |
| 36 | + [InlineData("Mon, 02 Jun 03 09:39:21 +0000", 2003, 6, 2, 9, 39, 21)] |
| 37 | + [InlineData("Mon, 02 Jun 03 09:39:21 -0000", 2003, 6, 2, 9, 39, 21)] |
| 38 | + [InlineData("Mon, 02 Jun 03 09:39:21 UT", 2003, 6, 2, 9, 39, 21)] |
| 39 | + public void Rss20ItemFormatter_Read_SingleDigitDay_NormalizedTimeZones(string pubDate, int year, int month, int day, int hour, int minute, int second) |
| 40 | + { |
| 41 | + string xml = $"<item><pubDate>{pubDate}</pubDate></item>"; |
| 42 | + using var stringReader = new StringReader(xml); |
| 43 | + using var reader = XmlReader.Create(stringReader); |
| 44 | + var formatter = new Rss20ItemFormatter(); |
| 45 | + formatter.ReadFrom(reader); |
| 46 | + DateTimeOffset expectedUtc = new DateTimeOffset(year, month, day, hour, minute, second, TimeSpan.Zero); |
| 47 | + Assert.Equal(expectedUtc, formatter.Item.PublishDate); |
| 48 | + } |
| 49 | + |
| 50 | + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] // NetFx requires double digit dates and four digit years |
| 51 | + [Theory] |
| 52 | + [InlineData("2 Jun {year} 09:39 GMT", 6, 2, 9, 39, 0)] |
| 53 | + [InlineData("02 Jun {year} 09:39:21 +0000", 6, 2, 9, 39, 21)] |
| 54 | + [InlineData("02 Jun {year} 09:39:21 -0000", 6, 2, 9, 39, 21)] |
| 55 | + [InlineData("02 Jun {year} 09:39:21 UT", 6, 2, 9, 39, 21)] |
| 56 | + public void Rss20ItemFormatter_Read_SingleDigitDay_And_TwoDigitYear_Max(string pubDate, int month, int day, int hour, int minute, int second) |
| 57 | + { |
| 58 | + // As of .Net 8.0, CultureInfo.CurrentCulture.DateTimeFormat.Calendar.TwoDigitYearMax is 2049 for invariant and en-US cultures |
| 59 | + var maxDate = CultureInfo.CurrentCulture.DateTimeFormat.Calendar.TwoDigitYearMax % 100; |
| 60 | + |
| 61 | + // Test under/at the 2-digit year max threshold |
| 62 | + var underDate = pubDate.Replace("{year}", maxDate.ToString()); |
| 63 | + string xml = $"<item><pubDate>{underDate}</pubDate></item>"; |
| 64 | + using var stringReader = new StringReader(xml); |
| 65 | + using var reader = XmlReader.Create(stringReader); |
| 66 | + var formatter = new Rss20ItemFormatter(); |
| 67 | + formatter.ReadFrom(reader); |
| 68 | + DateTimeOffset expectedUnder = new DateTimeOffset(2000 + maxDate, month, day, hour, minute, second, TimeSpan.Zero); |
| 69 | + Assert.Equal(expectedUnder, formatter.Item.PublishDate); |
| 70 | + |
| 71 | + // Test over the 2-digit year max threshold |
| 72 | + var overDate = pubDate.Replace("{year}", (maxDate + 1).ToString()); |
| 73 | + xml = $"<item><pubDate>{overDate}</pubDate></item>"; |
| 74 | + using var stringReaderOver = new StringReader(xml); |
| 75 | + using var readerOver = XmlReader.Create(stringReaderOver); |
| 76 | + formatter = new Rss20ItemFormatter(); |
| 77 | + formatter.ReadFrom(readerOver); |
| 78 | + DateTimeOffset expectedOver = new DateTimeOffset(1900 + maxDate + 1, month, day, hour, minute, second, TimeSpan.Zero); |
| 79 | + Assert.Equal(expectedOver, formatter.Item.PublishDate); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments