diff --git a/CHANGELOG.txt b/CHANGELOG.txt index f77e45d..8b03e41 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,10 @@ +------------------------------------------------------------------------------ + qSharp 2.0.3 [2014.06.10] +------------------------------------------------------------------------------ + + - Fix: behaviour of toString and fromString methods in QTime, QSecond + and QMinute types for dates outside of 0-24 hours scope + ------------------------------------------------------------------------------ qSharp 2.0.2 [2014.06.06] ------------------------------------------------------------------------------ diff --git a/qSharp/src/QMinute.cs b/qSharp/src/QMinute.cs index 3122f0e..5b96acd 100644 --- a/qSharp/src/QMinute.cs +++ b/qSharp/src/QMinute.cs @@ -24,7 +24,6 @@ namespace qSharp /// public struct QMinute : IDateTime { - private const string DateFormat = "HH:mm"; private const string NullRepresentation = "0Nu"; private DateTime datetime; @@ -79,7 +78,10 @@ public override string ToString() { if (Value != int.MinValue) { - return ToDateTime().ToString(DateFormat); + int minutes = Math.Abs(Value); + int hours = minutes / 60; + + return String.Format("{0}{1:00}:{2:00}", Value < 0 ? "-" : "", hours, minutes % 60); } return NullRepresentation; } @@ -91,9 +93,18 @@ public override string ToString() /// a QMinute instance public static QMinute FromString(string date) { + if (date == null || date.Length == 0 || date.Equals(NullRepresentation)) + { + return new QMinute(int.MinValue); + } + try { - return date == null || date.Length == 0 || date.Equals(NullRepresentation) ? new QMinute(int.MinValue) : new QMinute(DateTime.ParseExact(date, DateFormat, CultureInfo.InvariantCulture)); + String[] parts = date.Split(':'); + int hours = int.Parse(parts[0]); + int minutes = int.Parse(parts[1]); + + return new QMinute((minutes + 60 * Math.Abs(hours)) * (hours > 0 ? 1 : -1)); } catch (Exception e) { diff --git a/qSharp/src/QSecond.cs b/qSharp/src/QSecond.cs index 8451fad..69c5850 100644 --- a/qSharp/src/QSecond.cs +++ b/qSharp/src/QSecond.cs @@ -24,7 +24,6 @@ namespace qSharp /// public struct QSecond : IDateTime { - private const string DateFormat = "HH:mm:ss"; private const string NullRepresentation = "0Nv"; private DateTime datetime; @@ -79,7 +78,11 @@ public override string ToString() { if (Value != int.MinValue) { - return ToDateTime().ToString(DateFormat); + int seconds = Math.Abs(Value); + int minutes = seconds / 60; + int hours = minutes / 60; + + return String.Format("{0}{1:00}:{2:00}:{3:00}", Value < 0 ? "-" : "", hours, minutes % 60, seconds % 60); } return NullRepresentation; } @@ -91,9 +94,19 @@ public override string ToString() /// a QSecond instance public static QSecond FromString(string date) { + if (date == null || date.Length == 0 || date.Equals(NullRepresentation)) + { + return new QSecond(int.MinValue); + } + try { - return date == null || date.Length == 0 || date.Equals(NullRepresentation) ? new QSecond(int.MinValue) : new QSecond(DateTime.ParseExact(date, DateFormat, CultureInfo.InvariantCulture)); + String[] parts = date.Split(':'); + int hours = int.Parse(parts[0]); + int minutes = int.Parse(parts[1]); + int seconds = int.Parse(parts[2]); + + return new QSecond((seconds + 60 * minutes + 3600 * Math.Abs(hours)) * (hours > 0 ? 1 : -1)); } catch (Exception e) { diff --git a/qSharp/src/QTime.cs b/qSharp/src/QTime.cs index c40204e..8eaff61 100644 --- a/qSharp/src/QTime.cs +++ b/qSharp/src/QTime.cs @@ -24,7 +24,6 @@ namespace qSharp /// public struct QTime : IDateTime { - private const string DateFormat = "HH:mm:ss.fff"; private const string NullRepresentation = "0Nt"; private DateTime datetime; @@ -79,7 +78,12 @@ public override string ToString() { if (Value != int.MinValue) { - return ToDateTime().ToString(DateFormat); + int millis = Math.Abs(Value); + int seconds = millis / 1000; + int minutes = seconds / 60; + int hours = minutes / 60; + + return String.Format("{0}{1:00}:{2:00}:{3:00}.{4:000}", Value < 0 ? "-" : "", hours, minutes % 60, seconds % 60, millis % 1000); } return NullRepresentation; } @@ -91,9 +95,19 @@ public override string ToString() /// a QTime instance public static QTime FromString(string date) { + if (date == null || date.Length == 0 || date.Equals(NullRepresentation)) + { + return new QTime(int.MinValue); + } + try { - return date == null || date.Length == 0 || date.Equals(NullRepresentation) ? new QTime(int.MinValue) : new QTime(DateTime.ParseExact(date, DateFormat, CultureInfo.InvariantCulture)); + String[] parts = date.Split(new char[]{ ':' , '.'}); + int hours = int.Parse(parts[0]); + int minutes = int.Parse(parts[1]); + int seconds = int.Parse(parts[2]); + int millis = int.Parse(parts[3]); + return new QTime((millis + 1000 * seconds + 60000 * minutes + 3600000 * Math.Abs(hours)) * (hours > 0 ? 1 : -1)); } catch (Exception e) { diff --git a/qSharp/test/DateTimeTest.cs b/qSharp/test/DateTimeTest.cs index b4154b8..fcb10bb 100644 --- a/qSharp/test/DateTimeTest.cs +++ b/qSharp/test/DateTimeTest.cs @@ -106,6 +106,9 @@ public void testQMinuteToString() Assert.AreEqual("13:30", new QMinute(810).ToString()); Assert.AreEqual("23:59", new QMinute(1439).ToString()); + Assert.AreEqual("-13:30", new QMinute(-810).ToString()); + Assert.AreEqual("52:23", new QMinute(3143).ToString()); + Assert.AreEqual("0Nu", new QMinute(int.MinValue).ToString()); } @@ -116,6 +119,9 @@ public void testQMinuteFromString() Assert.AreEqual(new QMinute(810), QMinute.FromString("13:30")); Assert.AreEqual(new QMinute(1439), QMinute.FromString("23:59")); + Assert.AreEqual(new QMinute(-810), QMinute.FromString("-13:30")); + Assert.AreEqual(new QMinute(3143), QMinute.FromString("52:23")); + Assert.AreEqual(new QMinute(int.MinValue), QMinute.FromString(null)); Assert.AreEqual(new QMinute(int.MinValue), QMinute.FromString("")); Assert.AreEqual(new QMinute(int.MinValue), QMinute.FromString("0Nu")); @@ -172,6 +178,9 @@ public void testQSecondToString() Assert.AreEqual("13:30:13", new QSecond(48613).ToString()); Assert.AreEqual("23:59:59", new QSecond(86399).ToString()); + Assert.AreEqual("51:46:39", new QSecond(186399).ToString()); + Assert.AreEqual("-23:59:59", new QSecond(-86399).ToString()); + Assert.AreEqual("0Nv", new QSecond(int.MinValue).ToString()); } @@ -182,6 +191,9 @@ public void testQSecondFromString() Assert.AreEqual(new QSecond(48613), QSecond.FromString("13:30:13")); Assert.AreEqual(new QSecond(86399), QSecond.FromString("23:59:59")); + Assert.AreEqual(new QSecond(186399), QSecond.FromString("51:46:39")); + Assert.AreEqual(new QSecond(-86399), QSecond.FromString("-23:59:59")); + Assert.AreEqual(new QSecond(int.MinValue), QSecond.FromString(null)); Assert.AreEqual(new QSecond(int.MinValue), QSecond.FromString("")); Assert.AreEqual(new QSecond(int.MinValue), QSecond.FromString("0Nv")); @@ -202,6 +214,9 @@ public void testQTimeToString() Assert.AreEqual("13:30:13.000", new QTime(48613000).ToString()); Assert.AreEqual("23:59:59.000", new QTime(86399000).ToString()); + Assert.AreEqual("51:46:39.050", new QTime(186399050).ToString()); + Assert.AreEqual("-23:59:59.100", new QTime(-86399100).ToString()); + Assert.AreEqual("0Nt", new QTime(int.MinValue).ToString()); } @@ -212,6 +227,9 @@ public void testQTimeFromString() Assert.AreEqual(new QTime(0), QTime.FromString("00:00:00.000")); Assert.AreEqual(new QTime(86399000), QTime.FromString("23:59:59.000")); + Assert.AreEqual(new QTime(186399001), QTime.FromString("51:46:39.001")); + Assert.AreEqual(new QTime(-86399000), QTime.FromString("-23:59:59.000")); + Assert.AreEqual(new QTime(int.MinValue), QTime.FromString(null)); Assert.AreEqual(new QTime(int.MinValue), QTime.FromString("")); Assert.AreEqual(new QTime(int.MinValue), QTime.FromString("0Nt"));