From 1ff1bcde3b6cde7016f8b759a64becda53e19168 Mon Sep 17 00:00:00 2001 From: Frederik Carlier Date: Wed, 25 May 2016 00:29:11 +0200 Subject: [PATCH 1/3] Parse dates as UTC dates. --- plist-cil.test/NSDateTests.cs | 22 ++++++++++++++++++++++ plist-cil.test/plist-cil.test.csproj | 1 + plist-cil/NSDate.cs | 4 +++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 plist-cil.test/NSDateTests.cs diff --git a/plist-cil.test/NSDateTests.cs b/plist-cil.test/NSDateTests.cs new file mode 100644 index 0000000..24a73b0 --- /dev/null +++ b/plist-cil.test/NSDateTests.cs @@ -0,0 +1,22 @@ +using Claunia.PropertyList; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace plistcil.test +{ + [TestFixture] + public class NSDateTests + { + [Test] + public static void ConstructorTest() + { + var actual = new NSDate("2000-01-01T00:00:00Z"); + var expected = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); + Assert.AreEqual(expected, actual.Date.ToUniversalTime()); + } + } +} diff --git a/plist-cil.test/plist-cil.test.csproj b/plist-cil.test/plist-cil.test.csproj index 52b15f1..733f535 100644 --- a/plist-cil.test/plist-cil.test.csproj +++ b/plist-cil.test/plist-cil.test.csproj @@ -37,6 +37,7 @@ + diff --git a/plist-cil/NSDate.cs b/plist-cil/NSDate.cs index 1a62b1c..68254dd 100644 --- a/plist-cil/NSDate.cs +++ b/plist-cil/NSDate.cs @@ -38,7 +38,9 @@ namespace Claunia.PropertyList static readonly DateTime EPOCH = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc); - static readonly string sdfDefault = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + // The datetime ends with 'Z', which indicates UTC time. To make sure .NET + // understands the 'Z' character as a timezone, specify the 'K' format string. + static readonly string sdfDefault = "yyyy-MM-dd'T'HH:mm:ssK"; static readonly string sdfGnuStep = "yyyy-MM-dd HH:mm:ss zzz"; static readonly System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture; From dfee77b7241c9265cccb88cac3509e2dbf39342d Mon Sep 17 00:00:00 2001 From: Frederik Carlier Date: Wed, 25 May 2016 00:34:23 +0200 Subject: [PATCH 2/3] When converting dates to strings, always convert them to UTC first. --- plist-cil.test/NSDateTests.cs | 10 ++++++++++ plist-cil/NSDate.cs | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/plist-cil.test/NSDateTests.cs b/plist-cil.test/NSDateTests.cs index 24a73b0..c39ab46 100644 --- a/plist-cil.test/NSDateTests.cs +++ b/plist-cil.test/NSDateTests.cs @@ -18,5 +18,15 @@ namespace plistcil.test var expected = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); Assert.AreEqual(expected, actual.Date.ToUniversalTime()); } + + [Test] + public static void MakeDateStringTest() + { + var date = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc); + var expected = "2000-01-01T00:00:00Z"; + var actual = NSDate.MakeDateString(date); + + Assert.AreEqual(expected, actual); + } } } diff --git a/plist-cil/NSDate.cs b/plist-cil/NSDate.cs index 68254dd..fa6c46b 100644 --- a/plist-cil/NSDate.cs +++ b/plist-cil/NSDate.cs @@ -69,9 +69,9 @@ namespace Claunia.PropertyList /// /// The date which should be represented. /// The string representation of the date. - static string MakeDateString(DateTime date) + public static string MakeDateString(DateTime date) { - return date.ToString(sdfDefault); + return date.ToUniversalTime().ToString(sdfDefault); } /// From c2f15567c7795950dbc2a98755f691d52edcedc6 Mon Sep 17 00:00:00 2001 From: Frederik Carlier Date: Fri, 3 Jun 2016 16:02:52 +0200 Subject: [PATCH 3/3] Fix the datetime test (use the correct UTC value) --- plist-cil.test/ParseTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plist-cil.test/ParseTest.cs b/plist-cil.test/ParseTest.cs index ddbf860..b3225a4 100644 --- a/plist-cil.test/ParseTest.cs +++ b/plist-cil.test/ParseTest.cs @@ -47,7 +47,7 @@ namespace plistcil.test Assert.True(d.Count == 5); Assert.True(((NSString)d.ObjectForKey("keyA")).ToString().Equals("valueA")); Assert.True(((NSString)d.ObjectForKey("key&B")).ToString().Equals("value&B")); - Assert.True(((NSDate)d.ObjectForKey("date")).Date.Equals(new DateTime(2011, 11, 28, 9, 21, 30, DateTimeKind.Utc))); + Assert.True(((NSDate)d.ObjectForKey("date")).Date.Equals(new DateTime(2011, 11, 28, 10, 21, 30, DateTimeKind.Utc))); Assert.True(ArrayEquals(((NSData)d.ObjectForKey("data")).Bytes, new byte[]{ 0x00, 0x00, 0x00, 0x04, 0x10, 0x41, 0x08, 0x20, (byte)0x82 })); NSArray a = (NSArray)d.ObjectForKey("array");