Skip to content

Commit

Permalink
fix #3: behaviour of toString and fromString for values outside of 0-…
Browse files Browse the repository at this point in the history
…24 hours scope
  • Loading branch information
maciejlach committed Jun 10, 2014
1 parent a315b8a commit b51fc4e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -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]
------------------------------------------------------------------------------
Expand Down
17 changes: 14 additions & 3 deletions qSharp/src/QMinute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace qSharp
/// </summary>
public struct QMinute : IDateTime
{
private const string DateFormat = "HH:mm";
private const string NullRepresentation = "0Nu";

private DateTime datetime;
Expand Down Expand Up @@ -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;
}
Expand All @@ -91,9 +93,18 @@ public override string ToString()
/// <returns>a QMinute instance</returns>
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)
{
Expand Down
19 changes: 16 additions & 3 deletions qSharp/src/QSecond.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace qSharp
/// </summary>
public struct QSecond : IDateTime
{
private const string DateFormat = "HH:mm:ss";
private const string NullRepresentation = "0Nv";

private DateTime datetime;
Expand Down Expand Up @@ -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;
}
Expand All @@ -91,9 +94,19 @@ public override string ToString()
/// <returns>a QSecond instance</returns>
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)
{
Expand Down
20 changes: 17 additions & 3 deletions qSharp/src/QTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace qSharp
/// </summary>
public struct QTime : IDateTime
{
private const string DateFormat = "HH:mm:ss.fff";
private const string NullRepresentation = "0Nt";

private DateTime datetime;
Expand Down Expand Up @@ -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;
}
Expand All @@ -91,9 +95,19 @@ public override string ToString()
/// <returns>a QTime instance</returns>
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)
{
Expand Down
18 changes: 18 additions & 0 deletions qSharp/test/DateTimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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"));
Expand Down Expand Up @@ -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());
}

Expand All @@ -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"));
Expand All @@ -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());
}

Expand All @@ -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"));
Expand Down

0 comments on commit b51fc4e

Please sign in to comment.