modules/tmrec/README
bf692bcf
 TMREC Module
 
 Daniel-Constantin Mierla
 
    <miconda@gmail.com>
 
 Edited by
 
 Daniel-Constantin Mierla
 
    <miconda@gmail.com>
 
2efdcc87
 Edited by
 
 Alex Balashov
 
    <abalashov@evaristesys.com>
 
f5ccfeaa
    Copyright � 2012 asipto.com
bf692bcf
      __________________________________________________________________
 
    Table of Contents
 
    1. Admin Guide
 
         1. Overview
         2. Dependencies
 
               2.1. Kamailio Modules
               2.2. External Libraries or Applications
 
         3. Parameters
 
               3.1. separator (str)
 
         4. Functions
 
               4.1. tmrec_match(timerec [, timestamp])
               4.2. is_leap_year([year])
18107462
               4.3. time_period_match(period [, timestamp])
bf692bcf
 
    List of Examples
 
    1.1. Set separator parameter
    1.2. tmrec_match usage
    1.3. is_leap_year usage
18107462
    1.4. time_period_match usage
bf692bcf
 
 Chapter 1. Admin Guide
 
    Table of Contents
 
    1. Overview
    2. Dependencies
 
         2.1. Kamailio Modules
         2.2. External Libraries or Applications
 
    3. Parameters
 
         3.1. separator (str)
 
    4. Functions
 
         4.1. tmrec_match(timerec [, timestamp])
         4.2. is_leap_year([year])
18107462
         4.3. time_period_match(period [, timestamp])
bf692bcf
 
 1. Overview
 
2efdcc87
    This module provides time recurrence matching functions. The format of
    recurrence definitions is based on Internet Calendaring and Scheduling
18107462
    Core Object Specification (Calendar COS - RFC 2445). It also provides a
    port of the Perl Time::Period module for generic time period matching.
bf692bcf
 
 2. Dependencies
 
    2.1. Kamailio Modules
    2.2. External Libraries or Applications
 
 2.1. Kamailio Modules
 
    The following modules must be loaded before this module:
      * None.
 
 2.2. External Libraries or Applications
 
    The following libraries or applications must be installed before
    running Kamailio with this module loaded:
      * None
 
 3. Parameters
 
    3.1. separator (str)
 
 3.1. separator (str)
 
2efdcc87
    Separator character used to delimit attributes in time reccurence
bf692bcf
    definitions.
 
    Default value is '|'.
 
    Example 1.1. Set separator parameter
 ...
 modparam("tmrec", "separator", ";")
 ...
 
 4. Functions
 
    4.1. tmrec_match(timerec [, timestamp])
    4.2. is_leap_year([year])
18107462
    4.3. time_period_match(period [, timestamp])
bf692bcf
 
f5ccfeaa
 4.1. tmrec_match(timerec [, timestamp])
bf692bcf
 
2efdcc87
    Match a time recurrence rule against the timestamp (UNIX epoch format).
    If the timestamp parameter is missing, the current UNIX epoch time is
    used.
bf692bcf
 
    The parameters can include pseudo-variables.
 
    The timerec paramter is a list of attributes defined by RFC2445,
2efdcc87
    delimited by the 'separator' (module parameter) character. The format
    of timerec parameter, using '|' as the separator, is (all in one line
    without white spaces):
bf692bcf
 ...
 [startdate]|[duration]|[frequency]|[until]|[interval]|[byday]
   |[bymonthday]|[byyearday]|[byweekno]|[bymonth]
 ...
 
    When an attribute is not specified, the corresponding place must be
2efdcc87
    left empty, provided that one or more additional attributes follow.
bf692bcf
 
    Description of time recurrence attributes:
      * startdate - date for the start of the first period.
      * duration - the duration of the time period. For a recurring
f5ccfeaa
        interval, the "duration" parameter MUST be small enough such that
bf692bcf
        subsequent intervals do not overlap. For non-recurring intervals,
2efdcc87
        durations of any positive length are permitted. Zero-length
f5ccfeaa
        duration means "forever". Negative-length durations are not
99d11704
        allowed. See RFC 2445 for the format of duration.
f5ccfeaa
      * frequency - can be one of the following values: "daily" - specify
        repeating periods based on an interval of a day or more; "weekly" -
bf692bcf
        specify repeating periods based on an interval of a week or more;
f5ccfeaa
        "monthly" - specify repeating periods based on an interval of a
        month or more; "yearly" - specify repeating periods based on an
2efdcc87
        interval of a year or more. These values are case-insensitive.
bf692bcf
      * until - defines an iCalendar COS DATE or DATE-TIME value which
        bounds the recurrence rule in an inclusive manner. If the value
f5ccfeaa
        specified by "until" is synchronized with the specified recurrence,
bf692bcf
        this date or date-time becomes the last instance of the recurrence.
2efdcc87
        If it is not present, the recurrence is considered to repeat
        forever.
bf692bcf
      * interval - a positive integer representing how often the recurrence
f5ccfeaa
        rule repeats. The default value is "1", meaning every day for a
        "daily" rule, every week for a "weekly" rule, every month for a
        "monthly" rule and every year for a "yearly" rule.
bf692bcf
      * byday - a comma-separated list short codes of days of the week. The
f5ccfeaa
        days are specified as: "MO" for Monday; "TU" for Tuesday; "WE" for
        Wednesday; "TH" for Thursday; "FR" for Friday; "SA" for Saturday;
        "SU" for Sunday. These values are case insensitive.
        Each "byday" value can also be prefixed by a positive (+n) or
bf692bcf
        negative (-n) integer. If present, this indicates the n-th
f5ccfeaa
        occurrence of the specific day within the "monthly" or "yearly"
        recurrence. For example, within a "monthly" rule, +1MO (or simply
bf692bcf
        1MO) represents the first Monday within the month, whereas -1MO
        represents the last Monday of the month. If an integer modifier is
        not present, it means all days of this type within the specified
f5ccfeaa
        frequency. For example, within a "monthly" rule, MO represents all
bf692bcf
        Mondays within the month.
      * bymonthday - a comma-separated list of days of the month. Valid
        values are 1 to 31 or -31 to -1. For example, -10 represents the
        tenth to the last day of the month.
      * byyearday - a comma-separated list of days of the year. Valid
        values are 1 to 366 or -366 to -1. For example, -1 represents the
        last day of the year (December 31st) and -306 represents the 306th
        day before the last day of the year (March 1st).
      * byweekno - a comma-separated list of ordinals specifying weeks of
        the year. Valid values are 1 to 53 or -53 to -1.
      * bymonth - parameter specifies a comma-separated list of months of
        the year. Valid values are 1 to 12.
 
f5ccfeaa
    A recurrence is specified by including the "frequency" parameter, which
2efdcc87
    indicates the type of the recurrence rule. Parameters other than
f5ccfeaa
    "startdate" and "duration" SHOULD NOT be specified unless "frequency"
bf692bcf
    is set.
 
f5ccfeaa
    If invalid byxxx parameter values are found (ie, bymonthday="30" in
2efdcc87
    February), they are simply ignored.
bf692bcf
 
2efdcc87
    Byxxx parameters modify the recurrence rule matching. The Byxxx rule,
    as an attribute for a period of time which is greater than or equal to
    the frequency, generally reduces or limits the number of occurrences
f5ccfeaa
    for the recurrence definition. For example, frequency="daily"
    bymonth="3" reduces the number of recurrence instances from all days
    (if the "bymonth" parameter is not present) to all days in March. Byxxx
bf692bcf
    parameters for a period of time less than the frequency generally
    increases or expands the number of occurrences of the recurrence. For
f5ccfeaa
    example, frequency="yearly" bymonth="8,9" increases the number of days
    within the yearly recurrence set from 1 (if "bymonth" parameter is not
bf692bcf
    present) to 2.
 
    If multiple Byxxx parameters are specified, then after evaluating the
f5ccfeaa
    specified "frequency" and "interval" parameters, the Byxxx parameters
bf692bcf
    are applied to the current set of evaluated occurrences in the
f5ccfeaa
    following order: "bymonth", "byweekno", "byyearday", "bymonthday",
    "byday"; then "until" is evaluated.
bf692bcf
 
    Next is an example of evaluating multiple Byxxx parameters.
 
f5ccfeaa
    startdate="20100101T093000" duration="10H30M" frequency="yearly"
    interval="4" bymonth="3" byday="SU"
bf692bcf
 
f5ccfeaa
    First, the interval="4" would be applied to frequency="yearly" to match
    on "every 4th year" . Then, bymonth="1" would be applied to match on
    "every March, every 4th year". Then, byday="SU" would be applied to
    match on "every Sunday in March, every 4th year, from 9:30 to 20:00 ".
bf692bcf
    The start and end hours:minutes have been retrieved from the
f5ccfeaa
    "startdate" and "duration" parameters.
bf692bcf
 
    This function can be used in ANY_ROUTE.
 
    Example 1.2. tmrec_match usage
 ...
99d11704
         if(tmrec_match("20120101T000000|PT24H|weekly|||SA,SU")
bf692bcf
         xdbg("it is weekend!\n");
99d11704
     if(tmrec_match("20120101T083000|PT10H|weekly|||MO,TU,WE,TH,FR")
bf692bcf
         xdbg("it is with working hours\n");
 ...
 
f5ccfeaa
 4.2. is_leap_year([year])
bf692bcf
 
    Return true if the value from parameter is a leap year. If the
2efdcc87
    parameter is missing, then the year from the current time is taken.
bf692bcf
 
    The parameter can be pseudo-variable.
 
    This function can be used in ANY_ROUTE.
 
    Example 1.3. is_leap_year usage
 ...
 if(is_leap_year("2010"))
 ...
18107462
 
f5ccfeaa
 4.3. time_period_match(period [, timestamp])
18107462
 
    Matches the point in time specified by the timestamp parameter, or the
    current time if the parameter is missing, against the given period
    specification. Returns 1 if it matches, -1 if it doesn't, and a value
    <= -2 if there was an error.
 
    The time period specification follows the Perl Time::Period module. It
    is a string and consists of zero or more sub-period specifications,
    separated by commas. The period matches if at least one of the
    sub-periods matches, or if no sub-periods were given at all (an empty
    string).
 
    Each sub-period is a list of one or more scale definitions, optionally
    separated by space characters. The sub-period matches if all of the
    given scales within that sub-period match. For each scale given, a
    single value or a range of values (which is two values separated by a
    hyphen) can be specified. Multiple values or multiple ranges (or a
    combination thereof) within a single scale is also possible. If a
    certain scale is specified more than once, the previous values or
    ranges for that scale are extended by the newly given ones. If a
    particular scale is not mentioned at all within a sub-period, then no
    matching is performed for that scale.
 
    The following scales are supported and understood. Each scale also has
    a respective short code, either one can be used. A single scale
    definition consists of the scale name, followed by an opening brace,
    followed by the list of values, followed by a closing brace.
      * year or yr - Either given as a full 4-digit number >= 1970, or as a
        2-digit number, in which case it will be understood to be within
        the current century.
      * month or mo - Month of the year, either a number between 1 and 12,
        or at least the first 3 letters of a spelled out month name, e.g.
f5ccfeaa
        "jan", "janua" or "january" will all work.
18107462
      * week or wk - Week of the month, a number between 1 and 6. The first
        day of the week is Sunday.
      * yday or yd - Day of the year, a number between 1 and 366.
      * mday or md - Day of the month, a number between 1 and 31.
      * wday or wd - Day of the week, either a number between 1 and 7, or
        at least the first 2 letters of a spelled out weekday name
f5ccfeaa
        (analogous to the "month" scale). Sunday is the first day of the
18107462
        week.
      * hour or hr - A number between 0 and 23. Unlike the Perl
f5ccfeaa
        Time::Period module, "am" or "pm" specifications are not supported.
18107462
      * minute or min - A number between 0 and 59.
      * second or sec - A number between 0 and 60 (to allow for leap
        seconds).
 
    The parameters can include pseudo-variables. Whitespace (more
    precisely, the space character only) can occur anywhere, but is
f5ccfeaa
    optional. Ranges in all scales (with the exception of the "year" scale)
    are allowed to wrap-around, e.g. a weekday scale of "{fri-tue}" is
    equivalent to "{fri-sat,sun-tue}".
18107462
 
    Example 1.4. time_period_match usage
 ...
99d11704
 if(time_period_match("wd{2-6} hr{8-16}, wd{1-5} hr{17} min{0-29}"))
18107462
         xdbg("Monday to Friday, 8:00 to 17:30\n");
 
 if(time_period_match("weekday { sat sun }, weekday {mo-fr} hr {17-8},wd{mo-wed}h
 r{15 16 9}"))
         xdbg("We're closed - open only Monday to Wednesday 10:00-15:00, Thursday
  and Friday 9:00-17:00");
 ...