Module TimezoneSource

Historically this module was outside of Core as it did not support javascript well. Now that that's resolved it's been moved to be Core.Timezone. This library exists for backwards compatibility.

include module type of struct include Core.Timezone end

Where are the timezone data located ?

The location of the timezone files can be set using the environment variable TZDIR. If not set, Timezone will fallback to /usr/share/zoneinfo/.

What is the local timezone ?

The local timezone can be set using the environment variable TZ. If not set, Timezone will fallback to /etc/localtime. In a JavaScript context, we automatically set the environment variable TZ to Intl.DateTimeFormat().resolvedOptions().timeZone.

Compatibility with JavaScript.

The Timezone library can be used when constructing JavaScript applications with Js_of_ocaml.

In the past, web browsers needed to be provided with a copy of the timezone database, but this is no longer required.

Sourcemodule type Extend_zone = Core.Timezone.Extend_zone

User-friendly interface

Sourcetype t : sync_data

The type of a time-zone.

bin_io and sexp representations of Zone.t are the name of the zone, and not the full data that is read from disk when Zone.find is called. The full Zone.t is reconstructed on the receiving/reading side by reloading the zone file from disk. Any zone name that is accepted by find is acceptable in the bin_io and sexp representations.

Sourceval sexp_of_t__stack : t @ local -> Sexplib0.Sexp.t @ local
include Ppx_compare_lib.Comparable.S with type t := t
Sourceval input_tz_file : zonename:Base.String.t -> filename:Base.String.t -> t

input_tz_file ~zonename ~filename read in filename and return t with name t = zonename

Sourceval likely_machine_zones : Base.String.t Base.List.t Core.Atomic.t

likely_machine_zones is a list of zone names that will be searched first when trying to determine the machine zone of a box. Setting this to a likely set of zones for your application will speed the very first use of the local timezone.

Sourcemodule Time_in_seconds = Core.Timezone.Time_in_seconds
Sourcemodule Earlier_or_later = Core.Timezone.Earlier_or_later
Sourceval of_utc_offset : hours:Base.Int.t -> t

of_utc_offset offset returns a timezone with a static UTC offset (given in hours).

Sourceval of_utc_offset_explicit_name : name:Base.String.t -> hours:Base.Int.t -> t

Like of_utc_offset, but overriding the default name. These zones can only be reliably transferred over sexp or bin-io using Stable.Full_data; see below.

Sourceval of_utc_offset_in_seconds_round_down : ?name:Base.String.t -> Time_in_seconds.Span.t -> t

Returns a timezone with a static UTC offset in units of seconds. Rounds input to the next lower unit of seconds if necessary. These zones can only be reliably transferred over sexp or bin-io using Stable.Full_data; see below.

Sourceval add_offset_in_seconds_round_down : t -> name:Base.String.t -> span:Time_in_seconds.Span.t -> t

Returns a timezone with a fixed offset relative to the given t in units of seconds. This time zone doesn't represent any real place, but may be convenient for testing or for other non-real-time purposes. Rounds span to the next lower unit of seconds if necessary. These zones can only be reliably transferred over sexp or bin-io using Stable.Full_data; see below.

Sourceval utc : t

utc the UTC time zone. Included for convenience

Sourceval name : t -> Base.String.t
Sourceval original_filename : t -> Base.String.t Base.Option.t

original_filename t return the filename t was loaded from (if any)

digest t return the MD5 digest of the file the t was created from (if any)

Sourceval reset_transition_cache : t -> Base.Unit.t

For performance testing only; reset_transition_cache t resets an internal cache in t used to speed up repeated lookups of the same clock shift transition.

A time zone index refers to a range of times delimited by DST transitions at one or both ends. Every time belongs to exactly one such range. The times of DST transitions themselves belong to the range for which they are the lower bound.

Gets the index of a time.

Sourceval index_of_date_and_ofday : ?prefer:Earlier_or_later.t -> t -> Time_in_seconds.Date_and_ofday.t -> Index.t

Gets the index of an date and time of day in this zone. When there are two occurrences, the result is determined by prefer.

Sourceval index_offset_from_utc_exn : t -> Index.t -> Time_in_seconds.Span.t

Gets the UTC offset of times in a specific range.

This can raise if you use an Index.t that is out of bounds for this t.

Sourceval index_abbreviation_exn : t -> Index.t -> Base.String.t

index_abbreviation_exn t index returns the abbreviation name (such as EDT, EST, JST) of given zone t for the range of index. This string conversion is one-way only, and cannot reliably be turned back into a t. This function reads and writes the zone's cached index. Raises if index is out of bounds for t.

Sourceval index_has_prev_clock_shift : t -> Index.t -> Base.Bool.t

Accessors for the DST transitions delimiting the start and end of a range, if any. The _exn accessors raise if there is no such transition. These accessors are split up to increase performance and improve allocation; they are intended as a low-level back-end for commonly-used time conversion functions. See Time.Zone and Time_ns.Zone for higher-level accessors that return an optional tuple for clock shifts in either direction.

Sourceval index_prev_clock_shift_time_exn : t -> Index.t -> Time_in_seconds.t
Sourceval index_prev_clock_shift_amount_exn : t -> Index.t -> Time_in_seconds.Span.t
Sourceval index_has_next_clock_shift : t -> Index.t -> Base.Bool.t
Sourceval index_next_clock_shift_time_exn : t -> Index.t -> Time_in_seconds.t
Sourceval index_next_clock_shift_amount_exn : t -> Index.t -> Time_in_seconds.Span.t
include Extend_zone with type t := t
Sourceval t_sexp_grammar : t Sexplib0.Sexp_grammar.t @@ portable
include Core.Identifiable.S__local__portable with type t := t
include Bin_prot.Binable.S__local with type t := t
include Bin_prot.Binable.S_only_functions__local with type t := t
include sig ... end
Sourceval bin_size_t : t Bin_prot.Size.sizer
Sourceval bin_write_t : t Bin_prot.Write.writer
Sourceval bin_read_t : t Bin_prot.Read.reader
Sourceval __bin_read_t__ : t Bin_prot.Read.vtag_reader

This function only needs implementation if t exposed to be a polymorphic variant. Despite what the type reads, this does *not* produce a function after reading; instead it takes the constructor tag (int) before reading and reads the rest of the variant t afterwards.

Sourceval bin_shape_t : Bin_prot.Shape.t
include Ppx_hash_lib.Hashable.S with type t := t
include Sexplib0.Sexpable.S with type t := t
include Sexplib0.Sexpable.Of_sexp with type t := t
Sourceval t_of_sexp : Sexplib0.Sexp.t -> t
include Sexplib0.Sexpable.Sexp_of with type t := t
include Ppx_compare_lib.Comparable.S with type t := t
include Ppx_hash_lib.Hashable.S with type t := t
Sourceval sexp_of_t : t -> Sexplib0.Sexp.t
include Base.Stringable.S with type t := t
include Base.Stringable.Of_stringable with type t := t
Sourceval of_string : string -> t
include Base.Stringable.To_stringable with type t := t
Sourceval to_string : t -> string
include Base.Pretty_printer.S with type t := t
Sourceval pp : Base.Formatter.t -> t -> unit
include Base.Comparable.S__local__portable with type t := t
include Base.Comparisons.S__local with type t := t
include Base.Comparisons.Infix with type t := t
Sourceval (>=) : t -> t -> bool
Sourceval (<=) : t -> t -> bool
Sourceval (=) : t -> t -> bool
Sourceval (>) : t -> t -> bool
Sourceval (<) : t -> t -> bool
Sourceval (<>) : t -> t -> bool
Sourceval equal : t -> t -> bool
Sourceval compare : t -> t -> int

compare t1 t2 returns 0 if t1 is equal to t2, a negative integer if t1 is less than t2, and a positive integer if t1 is greater than t2.

Sourceval min : t -> t -> t
Sourceval max : t -> t -> t
Sourceval ascending : t -> t -> int @@ portable

ascending is identical to compare. descending x y = ascending y x. These are intended to be mnemonic when used like List.sort ~compare:ascending and List.sort ~cmp:descending, since they cause the list to be sorted in ascending or descending order, respectively.

Sourceval descending : t -> t -> int @@ portable
Sourceval between : t -> low:t -> high:t -> bool @@ portable

between t ~low ~high means low <= t <= high

Sourceval clamp_exn : t -> min:t -> max:t -> t @@ portable

clamp_exn t ~min ~max returns t', the closest value to t such that between t' ~low:min ~high:max is true.

Raises if not (min <= max).

Sourceval clamp : t -> min:t -> max:t -> t Base.Or_error.t @@ portable
include Base.Comparator.S__portable with type t := t
Sourcetype comparator_witness : value mod portable = Core.Timezone.comparator_witness
Sourceval validate_lbound : min:t Core.Maybe_bound.t -> t Validate.check
Sourceval validate_ubound : max:t Core.Maybe_bound.t -> t Validate.check
Sourceval validate_bound : min:t Core.Maybe_bound.t -> max:t Core.Maybe_bound.t -> t Validate.check
Sourcemodule Replace_polymorphic_compare = Core.Timezone.Replace_polymorphic_compare
include Core.Hashable.S_binable with type t := t
Sourceval hashable : t Base.Hashable.t
Sourcemodule Hash_queue = Core.Timezone.Hash_queue
include Diffable.S_atomic with type t := t
Sourceval find : Base.String.t -> t Base.Option.t @@ portable

find name looks up a t by its name and returns it. This also accepts some aliases, including:

  • chi -> America/Chicago
  • nyc -> America/New_York
  • hkg -> Asia/Hong_Kong
  • ldn -> Europe/London
  • lon -> Europe/London
  • tyo -> Asia/Tokyo
  • syd -> Australia/Sydney
Sourceval find_exn : Base.String.t -> t @@ portable
Sourceval local : t Core.Lazy.t @@ portable

local is the machine's local timezone, as determined from the TZ environment variable or the /etc/localtime file. It is computed from the state of the process environment and on-disk tzdata database at some unspecified moment prior to its first use, so its value may be unpredictable if that state changes during program operation. Arguably, changing the timezone of a running program is a problematic operation anyway -- most people write code assuming the clock doesn't suddenly jump several hours without warning.

Note that any function using this timezone can throw an exception if the TZ environment variable is misconfigured or if the appropriate timezone files can't be found because of the way the box is configured. We don't sprinkle _exn all over all the names in this module because such misconfiguration is quite rare.

Sourceval local_portable : t Core.Portable_lazy.t @@ portable

Like local, but a Portable_lazy.t.

Sourceval arg_type : t Command.Arg_type.t @@ portable
Sourceval initialized_zones : Base.Unit.t -> (Base.String.t * t) Base.List.t @@ portable

initialized_zones () returns a sorted list of time zone names that have been loaded from disk thus far.

Low-level functions

The functions below are lower level and should be used more rarely.

Sourceval init : Base.Unit.t -> Base.Unit.t @@ portable

init () pre-load all available time zones from disk, this function has no effect if it is called multiple times. Time zones will otherwise be loaded at need from the disk on the first call to find/find_exn.