0

I've been tasked with creating some call routing rules that are based on the time of day. 3CX (the system we use) provide documentation on how to do this, and I have it working to a degree. However, to get the time they provide this C# snippet which is a method in the call flow designer:

return DateTime.Now.Hour < 9;

This returns true or false, and a condition reads this boolean output, and determines where to send calls based on that output.

I need to edit this code snippet to give me a boolean output based on whether or not is is between 8:00 and 16:30.

I am not a developer - I dabbled in Python ages ago but that is not helping me here.

I'm not even sure if this is valid C# code to begin with, but it does work as-is.

The line in the configuration looks like this:

<ns0:ExecuteCSharpCodeComponent ReturnsValue="True" Code="return DateTime.Now.Hour &lt; 9;" ParameterList="&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?&gt;&lt;ArrayOfScriptParameter xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; /&gt;" Description="IsTime0to9" MethodName="IsTime0to9" DebugModeActive="False" x:Name="timeFrom0To9" />

This config line checks if the hour is between 9 and 12:

<ns0:ExecuteCSharpCodeComponent ReturnsValue="True" Code="return DateTime.Now.Hour &gt;= 9 &amp;&amp; DateTime.Now.Hour &lt; 12;" ParameterList="&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?&gt;&lt;ArrayOfScriptParameter xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; /&gt;" MethodName="IsTime9to12" DebugModeActive="False" x:Name="timeFrom9To12" />

Any input would be welcome.

Thank you

1 Answers1

0

You can't really do this by comparing the amounts of minutes and hours, because in the end you can't write a valid condition, unless you really want to branch it out, which is not that efficient or nice.

return DateTime.Now.Hour >= 8 && (DateTime.Now.Hour <= 16 && DateTime.Now.Minute <= 30)

This will fail for every time that has more than 30 minutes. I'd recommend using TimeSpan, since you can call for the current time of day by calling DateTime.Now.TimeOfDay.

TimeSpan startTime = new TimeSpan(8, 0, 0); //sets the 8:00 time
TimeSpan endTime = new TimeSpan(16, 30, 0); //sets the 16:30 time

return startTime <= DateTime.Now.TimeOfDay && endTime >= DateTime.Now.TimeOfDay;

Or you can just rewrite it into one line

return new TimeSpan(8, 0, 0) <= DateTime.Now.TimeOfDay && new TimeSpan(16, 30, 0) >= DateTime.Now.TimeOfDay;
GameboySR
  • 1
  • 2
  • I actually understand that, thank you. I'd be curious to see if the system could use TimeSpan. I'm not at all sure of the limitations of this system - which doesn't help ;) Thanks again – Luke Davis Jan 15 '20 at 23:46