Upgrade 3CX to v18 and get it hosted free!

Asterisk cmd GotoIfTime

Author image

Synopsis:

Conditional goto on current time

Description:

GotoIfTime(<time range>,<days of week>,<days of month>,<months>?[labeliftrue][:labeliffalse])

If the current time matches the specified time, then branch to the specified priority label. Each of the elements may be specified either as ‘*’ (for always) or as a range. If the current time does not match the specified time, and no false target is defined, the next priority is executed.

Times before Asterisk 1.6.2 are only accurate down to the 2-minute interval. So 12:01 is treated the same as 12:00.
Starting with 1.6.2, times are accurate down to the minute.

How to specify time

The include syntax is defined in the sample extensions.conf like this:

<time range>,<days of week>,<days of month>,<months>

where:
<time range>= <hour>':'<minute>'-'<hour>':'<minute>
| "*"
<days of week> = <dayname>
| <dayname>'-'<dayname>
| "*"
<dayname> = "sun" | "mon" | "tue" | "wed" | "thu" | "fri" | "sat"
<days of month> = <daynum>
| <daynum>'-'<daynum>
| "*"
<daynum> = a number, 1 to 31, inclusive
<hour> = a number, 0 to 23, inclusive
<minute> = a number, 0 to 59, inclusive
<months> = <monthname>
| <monthname>'-'<monthname>
| "*"
<monthname> = "jan" | "feb" | "mar" | "apr" | "may" | "jun" | "jul" | "aug" | "sep" | "oct" | "nov" | "dec"

daynames and monthnames are not case-sensitive.

Examples

If you replace an option with *, it is ignored when matching. For instance:

exten => 3000,1,GotoIfTime(9:00-17:00,mon-fri,*,*?open)

would transfer to priority label “open” in the current extension if it’s between 9:00 and 17:00, Monday through Friday, not checking the day of month or month.

Another example:

exten => s,n,GotoIfTime(*,*,26-30,May?attendant)

would transfer to priority label “attendant” in the current extension at any time from May 26th though May 30th.

Holidays

If the comment about holidays is true, then here’s a list suitable in the United States:

  • Independence Day: *,*,4,jul
  • Christmas: *,*,25,dec
  • NewYear: *,*,1,jan
  • MartinLutherKing: *,mon,15-21,jan
  • Valentines: *,*,14,feb
  • StPatDay *,*,17,mar
  • Halloween *,*,31,oct
  • Thanksgiving *,thu,22-28,nov
  • MemorialDay *,mon,25-31,may
  • LaborDay *,mon,1-7,sep
  • Pres/WashBday *,mon,15-21,feb
  • MothersDay *,sun,8-14,may
  • FathersDay *,sun,15-21,jun

Easter: Good Luck! Paschal moons, etc).

  • 2007 *,*,8,apr
  • 2008 *,*,23,mar
  • 2009 *,*,12,apr
  • 2010 *,*,4,apr
  • 2011 *,*,24,apr
  • 2012 *,*,8,apr
  • 2013 *,*,31,mar
  • 2014 *,*,20,apr
  • 2015 *,*,5,apr
  • 2016 *,*,27,mar
  • 2017 *,*,16,apr
  • 2018 *,*,1,ap
  • 2019 *,*,21,apr
  • 2020 *,*,12,apr
  • 2021 *,*,4,apr
  • 2022 *,*,17,apr
  • 2023 *,*,9,apr
  • 2024 *,*,31,mar
  • 2025 *,*,20,apr
  • 2026 *,*,5,apr
  • 2027 *,*,28,mar
  • 2028 *,*,16,apr
  • 2029 *,*,1,apr
  • 2030 *,*,21,apr
  • 2031 *,*,13,apr
  • 2032 *,*,28,mar
  • 2033 *,*,17,apr
  • 2034 *,*,9,apr
  • 2035 *,*,25,mar
  • 2036 *,*,13,apr
  • 2037 *,*,5,apr
  • 2038 *,*,25,apr
  • 2039 *,*,10,apr

Here is the calculation of Easter dates: http://www.oremus.org/liturgy/etc/ktf/app/easter.html

No years – How do I do this without reprogramming every year?

If the day in question is always in the same month, and moves not more than 7 days (e.g., in New Zealand, Labor day is “the fourth Monday in October”), you can specify a perpetual version; work out the earliest and the latest it can be, and specify the necessary weekday. The fourth Monday can not be earlier than the 22nd and not later than the 28th. So:

GotoIfTime(*,mon,22-28,oct?closed);

Similarly, if it is the sort of holiday that moves to Monday if it falls on the weekend, you can do it with a couple of rules. For example, in New Zealand, “New Year’s Day” is the 1st of January (if the first of January is a weekday) or the first weekday thereafter:


GotoIfTime(*,mon-fri,1,jan?closed);
GotoIfTime(*,mon,2-3,jan?closed);

– that is, the 1st of January if it’s a weekday, or the Monday on the second or third of January.

For Veterans’ Day, the same method can be used. Veterans’ Day is on November 11. If the 11th is a weekday, it is observed on the 11th. If the 11th falls on Saturday, the holiday will be observed on the Friday before (the 10th); if the 11th falls on Sunday, the holiday will be observed on the following Monday (the 12th):

GotoIfTime(*,fri,10,nov?closed);
GotoIfTime(*,mon-fri,11,nov?closed);
GotoIfTime(*,mon,12,nov?closed);

Easter is a lot more complicated.

Using GotoIfTime for non-static dates (for example, UK bank holidays in my case) means that the system must be updated every year. If the person who knows that this must be done forgets (or is no longer associated with the phone system) then things can go horribly wrong.

So…. For UK bank holidays, run the following script (which contains all UK bank holidays up to the end of 2009):

  1. !/bin/sh

asterisk -rx ‘database deltree bankholiday’
asterisk -rx ‘database put bankholiday 20060828 1’
asterisk -rx ‘database put bankholiday 20061225 1’
asterisk -rx ‘database put bankholiday 20061226 1’
asterisk -rx ‘database put bankholiday 20070101 1’
asterisk -rx ‘database put bankholiday 20070406 1’
asterisk -rx ‘database put bankholiday 20070409 1’
asterisk -rx ‘database put bankholiday 20070507 1’
asterisk -rx ‘database put bankholiday 20070528 1’
asterisk -rx ‘database put bankholiday 20070827 1’
asterisk -rx ‘database put bankholiday 20071225 1’
asterisk -rx ‘database put bankholiday 20071226 1’
asterisk -rx ‘database put bankholiday 20080101 1’
asterisk -rx ‘database put bankholiday 20080321 1’
asterisk -rx ‘database put bankholiday 20080324 1’
asterisk -rx ‘database put bankholiday 20080505 1’
asterisk -rx ‘database put bankholiday 20080526 1’
asterisk -rx ‘database put bankholiday 20080825 1’
asterisk -rx ‘database put bankholiday 20081225 1’
asterisk -rx ‘database put bankholiday 20081226 1’
asterisk -rx ‘database put bankholiday 20090101 1’
asterisk -rx ‘database put bankholiday 20090410 1’
asterisk -rx ‘database put bankholiday 20090413 1’
asterisk -rx ‘database put bankholiday 20090504 1’
asterisk -rx ‘database put bankholiday 20090525 1’
asterisk -rx ‘database put bankholiday 20090831 1’
asterisk -rx ‘database put bankholiday 20091225 1’
asterisk -rx ‘database put bankholiday 20091228 1’
exit

and then use the following in your dialplan:

exten => s,n,Set(BANKHOLIDAY=${DB_EXISTS(bankholiday/${TIMESTAMP:0:8})})

Same as above for asterisk >1.4 because ${TIMESTAMP} is deprecated

exten => s,n,Set(BANKHOLIDAY=${DB_EXISTS(bankholiday/${STRFTIME(${EPOCH},,%Y%m%d)})})

${BANKHOLIDAY} will now be either ‘0’ or ‘1’ and this can be used to do your GotoIfs!

See also


Asterisk | Applications | Functions | Variables | Expressions | Asterisk FAQ

Related Posts:

Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.