simple dial plan with various options
I wrote my first line in Ruby about 2 months ago, so I'm not qualified for a guru.
Probably it was a bad choice of words on my side. Your "t.hour + offset" approach is correct and works, but only while time of day is the only thing you need to know in order to make a decision ("to ring or not to ring", for example) Once you need to know the day of week (of month, etc) as well, you should adjust TIME object to local time before calculation of the day.I don't know what I messed up last time
In addition: if you need to be more precise (say, accept calls from 11:23 to 23:20), here is your lines:
Code: Select all
if (1123..2320) === (t.hour*100 + t.min)
... process incoming call ...
end
This thread has morphed a bit. I had tired the original (days don't matter to me) with Mike's suggested format. IIRC, I used the following and voicemail started kicking in at 16:00 (00:00 GMT) because 0 - 8 = -8.
Code: Select all
t = Time.now
localtime = t.hour - 8 # GMT-8
if (9..22) === localtime # 9am-10pm
sys.Dial("#{sys.Username}@local",15) if sys.IsAvailable()
end
sys.Respond(480, "Not available")
dalrun,
yep, that's right! We overlooked this thing...
Anyway, my recent suggestion should work fine. Try this:
yep, that's right! We overlooked this thing...
Anyway, my recent suggestion should work fine. Try this:
Code: Select all
t = Time.now - (8*60*60) # current local time GMT-8
sys.Log(t.strftime('Local time: %c')) # make sure our calculations work!
sys.Dial("#{sys.Username}@local",15) if (9..22) === t.hour && sys.IsAvailable()
sys.Respond(480, "Not available")
I decided to dig a bit deeper and check how Time strftime method is implemented in IronRuby. Here is a simple program I wrote for this occasion:
It produced the following:
As you can see, the most important things are already implemented. One that's missing is %p (AM/PM) but it's not very significant. Time.isdst method also works
Code: Select all
#Ruby
def prt(time,chr)
s = time.strftime("%#{chr}")
sys.Log("%#{chr}: <#{s}>") unless s.empty? || s == chr
end
t = Time.now
sys.Log('========= strftime format directives ===========')
20.upto(127) {|x| prt(t,x.chr)}
Code: Select all
DialPlan 20:54:39:622: ========= strftime format directives ===========
DialPlan 20:54:39:669: %A: <Wednesday>
DialPlan 20:54:39:669: %B: <September>
DialPlan 20:54:39:669: %H: <20>
DialPlan 20:54:39:669: %I: <08>
DialPlan 20:54:39:669: %M: <54>
DialPlan 20:54:39:669: %S: <39>
DialPlan 20:54:39:669: %U: <36>
DialPlan 20:54:39:669: %W: <36>
DialPlan 20:54:39:669: %X: <20:54>
DialPlan 20:54:39:669: %Y: <2008>
DialPlan 20:54:39:669: %Z: <+01:00>
DialPlan 20:54:39:669: %a: <Wed>
DialPlan 20:54:39:669: %b: <Sep>
DialPlan 20:54:39:669: %c: <10/09/2008 20:54>
DialPlan 20:54:39:669: %d: <10>
DialPlan 20:54:39:669: %j: <254>
DialPlan 20:54:39:669: %m: <09>
DialPlan 20:54:39:669: %w: <3>
DialPlan 20:54:39:669: %x: <10/09/2008>
DialPlan 20:54:39:669: %y: <08>
Another thing...
I think the above calculations work only because MSS servers are coincidentally are in GMT time zone. To make it independent of the server's time zone, it could be changed like this:
Unfortunately this is not perfect either, and can cause 1 hr discrepancy if DST rules in server's time zone are different from local time zone rules. This is because it can only determine if DST is in effect in the server's time zone. But the discrepancy only occurs during the short period when DST is in effect in one zone and not in effect in another zone. If you're in Europe where the DST rules are the same across all zones, this should cause no issues for you.
I think the above calculations work only because MSS servers are coincidentally are in GMT time zone. To make it independent of the server's time zone, it could be changed like this:
Code: Select all
offset = -5 # My timezone offset - GMT-5
t = Time.now.utc + (offset * 3600) # adjusting GMT to local time
t = t + (1 * 3600) if Time.now.localtime.isdst # add 1 hr if DST is in effect
sys.Log(t.strftime('Local time: %c'))
Exactly! I think that a perfect solution would be in getting Time.local to work with user's time zone instead of local computer's time zone. Each MSS user would have associated time zone settings and Time.local would take these settings and use them as a base for local time calculations.Unfortunately this is not perfect either...
In the meantime, we need to stick to some temporary workarounds. I prefer to hardcode offset rather than calculate it each time. Yes, I rely on fact that MSS servers are in UTC, but so what?
You only have to watch out for one thing, the end time in (14..17),dalrun wrote:This thread has morphed a bit. I had tired the original (days don't matter to me) with Mike's suggested format. IIRC, I used the following and voicemail started kicking in at 16:00 (00:00 GMT) because 0 - 8 = -8.
Code: Select all
t = Time.now localtime = t.hour - 8 # GMT-8 if (9..22) === localtime # 9am-10pm sys.Dial("#{sys.Username}@local",15) if sys.IsAvailable() end sys.Respond(480, "Not available")
means that the time period is from 14:00 to 17:59, because the hour digit changes one minute before it becomes 18:00 hours.
I 've found an somewhat cleaner version, with the help of jvwelzen,
Code: Select all
t = Time.now + (1*60*60) # Get current time and adjust to local time GMT+1 no DST needed
sys.Log(t.strftime('local time time: %c'))
tijd = t.strftime('%H:%M')
sys.Log("Tijd : #{tijd}")
if (1..5) === t.wday && ('06:35'..'17:15') === tijd # next lines will be executed when this condition is valid
sys.Dial("900316xxxxxxxx@TPAD" ,25)
Thanks, The Fug.
gear: my ISP's Zyxel Modem/Router in bridge, Sitecom WL309 Router, Siemens Gigaset 301D
gear: my ISP's Zyxel Modem/Router in bridge, Sitecom WL309 Router, Siemens Gigaset 301D