BACnet.Protocol.WeekNDay (bacstack v0.1.0-dev.1)

Copy Markdown View Source

A WeekNDay (Week And Day) is a compact 3-component pattern used inside BACnet.Protocol.CalendarEntry and BACnet.Protocol.SpecialEvent to describe recurring days without listing every date. Classic examples: "the third Tuesday of every month", "every even week on Friday", or "the last Sunday in odd months".

It is one of BACnet's most powerful yet bandwidth-efficient calendar primitives.

BACnet Specification References

  • ASN.1 (Clause 21):
    BACnetWeekNDay ::= SEQUENCE {
        month       (1..12 | 13 | 14 | 255),  -- 13=odd, 14=even, 255=unspecified
        weekOfMonth (1..6 | 255),             -- 6 = last 7 days, 255=unspecified
        dayOfWeek   (1..7 | 255)
    }
  • Primary usage (Clause 12.9 Calendar, 12.24 Schedule):

The week-of-month numbering (1 = days 1-7, 6 = last 7 days of month) is defined in the production comments and is independent of the actual calendar month length.

Examples

"Third Tuesday of every month"

iex> pattern = %WeekNDay{month: :unspecified, week_of_month: 3, weekday: 2}
iex> pattern.week_of_month
3

"Last Sunday in December"

iex> last_sunday_dec = %WeekNDay{month: 12, week_of_month: 6, weekday: 7}
iex> last_sunday_dec.week_of_month
6

Edge cases

Week 6 always means the last 7 days of the month, regardless of length:

iex> feb_last = %WeekNDay{month: 2, week_of_month: 6, weekday: 1}  # last Monday in Feb
iex> feb_last.week_of_month
6

When used inside a CalendarEntry or SpecialEvent, it can match the last few days even in short months (Feb 23-28 can be "week 6").

See Also

Summary

Types

t()

Compact recurring-day pattern (see BACnet.Protocol.WeekNDay module docs for full semantics and ASN.1 production). Month and week support the same even/odd/unspecified wildcards as BACnet.Protocol.BACnetDate.

Functions

Encodes a BACnet week and day into application tags encoding.

Parses a BACnet week and day from application tags encoding.

Validates whether the given BACnet week and day is in form valid.

Types

t()

@type t() :: %BACnet.Protocol.WeekNDay{
  month: 1..12 | :even | :odd | :unspecified,
  week_of_month: 1..6 | :unspecified,
  weekday: 1..7 | :unspecified
}

Compact recurring-day pattern (see BACnet.Protocol.WeekNDay module docs for full semantics and ASN.1 production). Month and week support the same even/odd/unspecified wildcards as BACnet.Protocol.BACnetDate.

Functions

encode(week, opts \\ [])

@spec encode(t(), Keyword.t()) ::
  {:ok, BACnet.Protocol.ApplicationTags.encoding_list()} | {:error, term()}

Encodes a BACnet week and day into application tags encoding.

parse(tags)

Parses a BACnet week and day from application tags encoding.

valid?(t)

@spec valid?(t()) :: boolean()

Validates whether the given BACnet week and day is in form valid.

It only validates the struct is valid as per type specification.