Merge branch '002_fix_crash_when_missing_rt' into 'master'

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

fixes #2

See merge request !4
This commit is contained in:
Johannes Loher 2017-07-12 15:43:16 +02:00
commit 7a76d12ec2
1 changed files with 27 additions and 8 deletions

View File

@ -1,7 +1,7 @@
module fahrplanparser;
import std.algorithm : map;
import std.array : front;
import std.array : empty, front;
import std.conv : to;
import std.datetime : dur, TimeOfDay, DateTimeException;
import std.string : format;
@ -80,6 +80,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)
in
{
@ -87,7 +95,11 @@ in
}
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
@ -141,12 +153,19 @@ body
return dur!"minutes"(0);
else if (useRealtimeString == "1")
{
immutable expectedTime = dp.departureTime;
immutable realTime = dp.departureTime!realTimeNodeName;
auto timeDiff = realTime - expectedTime;
if (timeDiff < dur!"minutes"(0))
timeDiff = dur!"hours"(24) + timeDiff;
return timeDiff;
try
{
immutable expectedTime = dp.departureTime;
immutable realTime = dp.departureTime!realTimeNodeName;
auto timeDiff = realTime - expectedTime;
if (timeDiff < dur!"minutes"(0))
timeDiff = dur!"hours"(24) + timeDiff;
return timeDiff;
}
catch (CouldNotFindeNodeException e)
{
return dur!"minutes"(0);
}
}
else
throw new UnexpectedValueException!string(useRealtimeString, "realtime");