Module Core__.Schedule_v5

Latest version of Schedule, which defines a type for schedules like "every 5 min after the hour" or "every weekday at 3pm." More expressive than V4, for example by adding the Zoned_between variant, which behaves like Between but allows expressing the start and end times in different zones.

Overview

A Schedule.t describes a (potentially repeating) schedule by selecting a subset of all seconds using the set operations in t. For example:

Zones and Tags

On top of this selection language there are two labeling branches of the variant that are important.

In_zone (zone, t) expresses that all of t should be evaluated relative to the time zone given.

Tag (tag, t) tags anything matching t with tag.

Combining these we can express something complex like the on-call groups across three offices:

      let weekdays         = Weekdays Day_of_week.weekdays in
      let working_hours    = Between Time.Ofday.((create ~hr:8 (), create ~hr:18 ())) in
      let working_schedule = And [ weekdays; working_hours ] in
      let offices =
        let (!!) = Time.Zone.find_exn in
        Location.Abbrev.([
          tot, !!"America/New_York"
        ; hkg, !!"Asia/Hong_Kong"
        ; ldn, !!"Europe/London" ])
      in
      List.map offices ~f:(fun (office, zone) ->
        In_zone (zone, Tag (office, working_schedule)))

after which we can use the tags function to extract the groups on call at any moment.

Daylight Savings Time

Schedules are expressed in terms of wall clock time, and as such have interesting behavior around daylight savings time boundaries. There are two circumstances that might affect a schedule. The first is a repeated time, which occurs when time jumps back (e.g. 2:30 may happen twice in one day). The second is a skipped time, which occurs when time jumps forward by an hour.

In both cases Schedule does the naive thing. If the time happens twice and is included in the schedule it is included twice. If it never happens Schedule makes no special attempt to artificially include it.

Interface

type zoned =
| Zoned

these phantom types are concrete and exposed to help the compiler understand that zoned and unzoned cannot be the same type (which it could not know if they were abstract), which helps it infer the injectivity of the type t below.

include sig ... end
val compare_zoned : zoned ‑> zoned ‑> int
type unzoned =
| Unzoned
include sig ... end
val compare_unzoned : unzoned ‑> unzoned ‑> int

'a indicates whether the schedule currently has an established zone.

'b is the type of the tag used in this schedule. In many cases it can be unspecified. See tags for more.

Items that take int lists silently ignore ints outside of the viable range. E.g. Days [32] will never occur.

module Inclusive_exclusive : sig ... end
type ('a, 'b) t =
| In_zone : Core__.Import_time.Time.Zone.t * (unzoned'bt ‑> (zoned'bt
| Tag : 'b * ('a'bt ‑> ('a'bt
| And : ('a'bt list ‑> ('a'bt
| Or : ('a'bt list ‑> ('a'bt
| Not : ('a'bt ‑> ('a'bt
| If_then_else : (('a'bt * ('a'bt * ('a'bt) ‑> ('a'bt
| Shift : Core__.Import_time.Time.Span.t * ('a'bt ‑> ('a'bt
| Between : (Inclusive_exclusive.t * Core__.Import_time.Time.Ofday.t) * (Inclusive_exclusive.t * Core__.Import_time.Time.Ofday.t) ‑> (unzoned'bt
| Zoned_between : (Inclusive_exclusive.t * Core__.Import_time.Time.Zone.t * Core__.Import_time.Time.Ofday.t) * (Inclusive_exclusive.t * Core__.Import_time.Time.Zone.t * Core__.Import_time.Time.Ofday.t) ‑> (zoned'bt
| At : Core__.Import_time.Time.Ofday.t list ‑> (unzoned'bt
| Secs : int list ‑> (unzoned'bt
| Mins : int list ‑> (unzoned'bt
| Hours : int list ‑> (unzoned'bt
| Weekdays : Core__.Import.Day_of_week.t list ‑> (unzoned'bt
| Days : int list ‑> (unzoned'bt
| Weeks : int list ‑> (unzoned'bt
| Months : Core__.Import.Month.t list ‑> (unzoned'bt
| On : Core__.Import.Date.t list ‑> (unzoned'bt
| Before : (Inclusive_exclusive.t * (Core__.Import.Date.t * Core__.Import_time.Time.Ofday.t)) ‑> (unzoned'bt
| After : (Inclusive_exclusive.t * (Core__.Import.Date.t * Core__.Import_time.Time.Ofday.t)) ‑> (unzoned'bt
| Always : ('a'bt
| Never : ('a'bt
include sig ... end
val compare : ('a ‑> 'a ‑> int) ‑> ('b ‑> 'b ‑> int) ‑> ('a'bt ‑> ('a'bt ‑> int
type 'b zoned_t = (zoned'bt
include sig ... end
val sexp_of_zoned_t : ('b ‑> Sexplib.Sexp.t) ‑> 'b zoned_t ‑> Sexplib.Sexp.t
val to_string_zoned : (zoned'bt ‑> string_of_tag:('b ‑> string) ‑> string

Return a string suitable for debugging purposes.

module Stable : sig ... end
val includes : (zoned'bt ‑> Core__.Import_time.Time.t ‑> bool

includes t time is true if the second represented by time falls within the schedule t.

val tags : (zoned'tagt ‑> Core__.Import_time.Time.t ‑> [ `Not_included | `Included of 'tag list ]

tags t time = `Not_included iff not (includes t time). Otherwise, tags t time = `Included lst , where lst includes all tags of a schedule such that includes t' time is true where t' is a tagged branch of the schedule. E.g. for some t equal to Tag some_tag t', tags t time will return some_tag if and only if includes t' time returns true. For a more interesting use case, consider the per-office on-call schedule example given in the beginning of this module. Note that a schdeule may have no tags, and therefore, lst can be empty.

val all_tags : (zoned'tagt ‑> tag_comparator:('tag'cmpCore__.Import.Comparator.t ‑> ('tag'cmpCore__.Import.Set.t
val fold_tags : (zoned'tagt ‑> init:'m ‑> f:('m ‑> 'tag ‑> 'm) ‑> Core__.Import_time.Time.t ‑> 'm option

fold_tags t ~init ~f time is nearly behaviorally equivalent to (but more efficient than) List.fold ~init ~f (tags t time), with the exception that it returns None if includes t time is false. It is important that f be pure, as its results may be discarded.

val map_tags : ('a'bt ‑> f:('b ‑> 'c) ‑> ('a'ct

Return a sequence of schedule changes over time that will never end.

If your schedules ends, you will continue to receive `No_change_until_at_least with increasing times forever.

The return type indicates whether includes t start_time is true and delivers a sequence of subsequent changes over time.

The times returned by the sequence are strictly increasing and never less than start_time. That is, `No_change_until_at_least x can never be followed by `Enter x, only by (at least) `Enter (x + 1s).

if emit is set to Transitions_and_tag_changes then all changes in tags will be present in the resulting sequence. Otherwise only the tags in effect when a schedule is entered are available.

The `In_range | `Out_of_range flag in `No_change_until_at_least indicates whether the covered range is entirely within, or outside of the time covered by the schedule and is only there to help with bookkeeping for the caller. `In_range | `Out_of_range will never disagree with what could be inferred from the `Enter and `Leave events.

The sequence takes care to do only a small amount of work between each element, so that pulling the next element of the sequence is always cheap. This is the primary motivation behind including `No_change_until_at_least.

The Time.t returned by `No_change_until_at_least is guaranteed to be a reasonable amount of time in the future (at least 1 hour).

module Event : sig ... end
type ('tag, 'a) emit =
| Transitions : ('tag, [ Event.no_change | 'tag Event.transition ]) emit
| Transitions_and_tag_changes : ('tag ‑> 'tag ‑> bool) ‑> ('tag, [ Event.no_change | 'tag Event.transition | 'tag Event.tag_change ]) emit

in Transitions_and_tag_changes equality for the tag type must be given

val to_endless_sequence : (zoned'tagt ‑> start_time:Core__.Import_time.Time.t ‑> emit:('tag'aemit ‑> [ `Started_in_range of 'tag list * 'a Core__.Import.Sequence.t | `Started_out_of_range of 'a Core__.Import.Sequence.t ]
val next_enter_between : (zoned'tagt ‑> Core__.Import_time.Time.t ‑> Core__.Import_time.Time.t ‑> Core__.Import_time.Time.t option

next_enter_between t start end The given startend range is inclusive on both ends. This function is useful for one-off events during the run of a program. If you want to track changes to a schedule over time it is better to call to_endless_sequence

val next_leave_between : (zoned'tagt ‑> Core__.Import_time.Time.t ‑> Core__.Import_time.Time.t ‑> Core__.Import_time.Time.t option

as next_enter_between but for leave events