Modified:
/src/Jayrock.Json/Json/Conversion/Converters/DateTimeImporter.cs
/src/Jayrock.Json/UnixTime.cs
/tests/Jayrock/Json/Conversion/Converters/TestDateTimeImporter.cs
/tests/Jayrock/TestUnixTime.cs
=======================================
--- /src/Jayrock.Json/Json/Conversion/Converters/DateTimeImporter.cs Sun
Apr 17 16:20:24 2011
+++ /src/Jayrock.Json/Json/Conversion/Converters/DateTimeImporter.cs Mon
Apr 18 15:42:09 2011
@@ -66,7 +66,7 @@
{
GroupCollection groups = match.Groups;
- long ms = long.Parse(groups[1].Value,
NumberStyles.None, CultureInfo.InvariantCulture);
+ long ms = long.Parse(groups[1].Value,
NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture);
time = UnixTime.ToDateTime(ms / 1000.0);
}
catch (OverflowException e)
=======================================
--- /src/Jayrock.Json/UnixTime.cs Fri Apr 15 16:09:54 2011
+++ /src/Jayrock.Json/UnixTime.cs Mon Apr 18 15:42:09 2011
@@ -76,8 +76,21 @@
public static DateTime ToDateTime(long time, int ms)
{
- if (ms < 0 || ms > 999)
- throw new ArgumentOutOfRangeException("ms");
+ if (time > 0)
+ {
+ if (ms < 0 || ms > 999)
+ throw new ArgumentOutOfRangeException("ms");
+ }
+ else if (time < 0)
+ {
+ if (ms < -999 || ms > 0)
+ throw new ArgumentOutOfRangeException("ms");
+ }
+ else
+ {
+ if (ms < -999 || ms > 999)
+ throw new ArgumentOutOfRangeException("ms");
+ }
return
EpochUtc.AddSeconds(time).AddMilliseconds(ms).ToLocalTime();
}
=======================================
--- /tests/Jayrock/Json/Conversion/Converters/TestDateTimeImporter.cs Fri
Apr 15 16:09:54 2011
+++ /tests/Jayrock/Json/Conversion/Converters/TestDateTimeImporter.cs Mon
Apr 18 15:42:09 2011
@@ -51,12 +51,24 @@
{
AssertImport(new DateTime(2006, 7, 17, 10, 56,
56), "1153133816", true);
}
+
+ [ Test ]
+ public void ImportNegativeNumber()
+ {
+ AssertImport(new DateTime(1098, 7, 6, 5, 43,
21), "-27501531399", true);
+ }
[ Test ]
public void ImportFractionalNumber()
{
AssertImport(new DateTime(2006, 7, 17, 10, 56, 56,
456), "1153133816.456", true);
}
+
+ [ Test ]
+ public void ImportNegativeFractionalNumber()
+ {
+ AssertImport(new DateTime(1098, 7, 6, 5, 43, 21,
234), "-27501531398.766", true);
+ }
[ Test, ExpectedException(typeof(JsonException)) ]
public void CannotImportTrue()
@@ -98,12 +110,14 @@
public void ImportMicrosoftAjaxFormat()
{
AssertImport(new DateTime(2006, 7, 17, 10, 56, 56, 456),
@"'\/Date(1153133816456)\/'", true);
+ AssertImport(new DateTime(1098, 7, 06, 05, 43, 21, 234),
@"'\/Date(-27501531398766)\/'", true);
}
[ Test ]
public void ImportMicrosoftAjaxFormatWithUpperCase()
{
AssertImport(new DateTime(2006, 7, 17, 10, 56, 56, 456),
@"'\/DATE(1153133816456)\/'", true);
+ AssertImport(new DateTime(1098, 7, 06, 05, 43, 21, 234),
@"'\/DATE(-27501531398766)\/'", true);
}
[ Test, ExpectedException(typeof(JsonException)) ]
=======================================
--- /tests/Jayrock/TestUnixTime.cs Fri Apr 15 16:09:54 2011
+++ /tests/Jayrock/TestUnixTime.cs Mon Apr 18 15:42:09 2011
@@ -67,21 +67,43 @@
Assert.AreEqual(u1, UnixTime.ToDouble(t1.ToLocalTime()),
0.0001);
Assert.AreEqual(u2, UnixTime.ToDouble(t2.ToLocalTime()),
0.0001);
+
+ Assert.AreEqual(new DateTime(1098, 7, 6, 5, 43, 21, 234),
UnixTime.ToDateTime(-27501531398.766).ToUniversalTime());
+ Assert.AreEqual(UnixTime.EpochUtc.AddMilliseconds(+123),
UnixTime.ToDateTime(0, +123).ToUniversalTime());
+ Assert.AreEqual(UnixTime.EpochUtc.AddMilliseconds(-123),
UnixTime.ToDateTime(0, -123).ToUniversalTime());
}
[ Test, ExpectedException(typeof(ArgumentOutOfRangeException)) ]
- public void CannotSpecifyNegativeMilliseconds()
- {
- UnixTime.ToDateTime(0, -1);
- }
-
+ public void CannotSpecifyPositiveTimeWithNegativeMilliseconds()
+ {
+ UnixTime.ToDateTime(123, -456);
+ }
+
[ Test, ExpectedException(typeof(ArgumentOutOfRangeException)) ]
+ public void CannotSpecifyNegativeTimeWithPositiveMilliseconds()
+ {
+ UnixTime.ToDateTime(-123, 456);
+ }
+
+ [ Test ]
+ public void Negative()
+ {
+ Assert.AreEqual(new DateTime(1098, 7, 6, 5, 43, 21, 234),
UnixTime.ToDateTime(-27501531398, -766).ToUniversalTime());
+ }
+
+ [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
public void CannotOverflowMilliseconds()
{
UnixTime.ToDateTime(0, 1000);
}
- [ Test ]
+ [ Test, ExpectedException(typeof(ArgumentOutOfRangeException)) ]
+ public void CannotOverflowNegativeMilliseconds()
+ {
+ UnixTime.ToDateTime(0, -1000);
+ }
+
+ [Test]
public void Maximum()
{
Assert.AreEqual(new DateTime(3000, 12, 31, 23, 59, 59),