fixed crash when rt tag is missing, but realtime is set to 1

This commit is contained in:
Johannes Loher 2017-07-12 15:42:23 +02:00
parent e4f2bc1f3f
commit 4b0102c85d

View file

@ -1,7 +1,7 @@
module fahrplanparser; module fahrplanparser;
import std.algorithm : map; import std.algorithm : map;
import std.array : front; import std.array : empty, front;
import std.conv : to; import std.conv : to;
import std.datetime : dur, TimeOfDay, DateTimeException; import std.datetime : dur, TimeOfDay, DateTimeException;
import std.string : format; import std.string : format;
@ -74,6 +74,14 @@ class UnexpectedValueException(T) : Exception
} }
} }
class CouldNotFindeNodeException : Exception
{
this(string node) @safe pure
{
super(`Could not find node "%s"`.format(node));
}
}
auto departureTime(string _timeNodeName = timeNodeName)(XmlNode dp) auto departureTime(string _timeNodeName = timeNodeName)(XmlNode dp)
in in
{ {
@ -81,7 +89,11 @@ in
} }
body body
{ {
return TimeOfDay.fromISOString(dp.parseXPath(timeXPath!_timeNodeName).front.getCData ~ "00"); auto timeNodes = dp.parseXPath(timeXPath!_timeNodeName);
if(timeNodes.empty)
throw new CouldNotFindeNodeException(_timeNodeName);
return TimeOfDay.fromISOString(timeNodes.front.getCData ~ "00");
} }
@system unittest @system unittest
@ -135,12 +147,19 @@ body
return dur!"minutes"(0); return dur!"minutes"(0);
else if (useRealtimeString == "1") else if (useRealtimeString == "1")
{ {
immutable expectedTime = dp.departureTime; try
immutable realTime = dp.departureTime!realTimeNodeName; {
auto timeDiff = realTime - expectedTime; immutable expectedTime = dp.departureTime;
if (timeDiff < dur!"minutes"(0)) immutable realTime = dp.departureTime!realTimeNodeName;
timeDiff = dur!"hours"(24) + timeDiff; auto timeDiff = realTime - expectedTime;
return timeDiff; if (timeDiff < dur!"minutes"(0))
timeDiff = dur!"hours"(24) + timeDiff;
return timeDiff;
}
catch (CouldNotFindeNodeException e)
{
return dur!"minutes"(0);
}
} }
else else
throw new UnexpectedValueException!string(useRealtimeString, "realtime"); throw new UnexpectedValueException!string(useRealtimeString, "realtime");