Skip to content
39 changes: 30 additions & 9 deletions CSharp/ZgTimeTable/ZgTimeTable/TimeTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public long nextChange(long time)
if (currentExpection != null)
{
long exRemaining = currentExpection.remaining(time);
foreach (Session exception in Exceptions)
{
if (isInSession(exception, time + exRemaining))
{
exRemaining += exception.remaining(time + exRemaining);
}
}
if (findCurrentSession(time + exRemaining) != null)
return exRemaining;
}
Expand All @@ -75,7 +82,7 @@ private long findNearest(Session[] Sessions, Session[] Exceptions, long time)
{
foreach (Session session in Sessions)
{
if (!session.isInPeriod(time))
if (session.isFinished(time))
continue;

long s;
Expand All @@ -92,7 +99,7 @@ private long findNearest(Session[] Sessions, Session[] Exceptions, long time)
}

long dif = s - t;
if (dif < 0)
if (dif <= 0)
{
dif += session.Cycle;
}
Expand All @@ -105,15 +112,20 @@ private long findNearest(Session[] Sessions, Session[] Exceptions, long time)
{
long future = time + dif;
long exDif = long.MaxValue;
bool isException = false;

foreach (Session ex in Exceptions)
{
if (isInSession(ex, future))
{
exDif = Math.Min(nextChange(time + ex.remaining(future)) + dif, exDif);
isException = true;
exDif = Math.Min(nextChange(future), exDif);
}
}
dif = Math.Min(exDif, dif);
if (isException)
dif = exDif + dif;
else
dif = Math.Min(exDif, dif);
}
}
else
Expand All @@ -128,7 +140,7 @@ private long findNearest(Session[] Sessions, Session[] Exceptions, long time)

private static bool isInSession(Session session, long time)
{
if (time < session.Start || (session.End > 0 && session.End < time))
if (!session.isInPeriod(time))
{
return false;
}
Expand Down Expand Up @@ -171,7 +183,7 @@ public class Session

public long remaining(long time)
{
if (isInPeriod(time))
if (!isInPeriod(time))
return -1;

long t;
Expand All @@ -194,17 +206,26 @@ public long remaining(long time)
long r = e - t;
return r > 0 ? r : 0;
}
public bool isInPeriod(long time)
public bool isFinished(long time)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we changed isInPeriod to isFinished?

{
long e;
if (Cycle == 0 && End == 0)
{
e = Start + Duration;
}
else if (End == 0)
{
e = long.MaxValue;
}
else
{
e = End;

return time >= Start && time < e;
}
return time >= e;
}
public bool isInPeriod(long time)
{
return time >= Start && !isFinished(time);
}
}
}