Skip to content

intervalDivideEquallyUnix

intervalDivideEquallyUnix(start: string | number, end: string | number, n: number, options?: { maxPieces?: number; }): { start: number; end: number; }[]

Split a Unix epoch interval into n equal-length sub-intervals.

  • Returns an array of n half-open [start, end) records that tile the original interval: each record’s end is the next record’s start and belongs only to that next record, so the pieces share no value and together cover [start, end) exactly once.
  • Each boundary is start + round((end - start) · i / n) in integers of the arguments’ own epoch unit (there is no epochUnit option: seconds in give seconds out, milliseconds give milliseconds), taken in bigint so no product past 2^53 loses a unit: the split is exact whenever the total divides evenly by n, and within half a unit of the exact cut otherwise (an exact half rounds up). No time zone is involved.
  • When n is larger than the number of whole epoch units in the interval, some pieces are empty (their start equals their end): the result always has exactly n pieces.
  • n === 1 returns the original interval unchanged, as a single-element array.
  • A zero-length interval (start === end) returns n identical zero-length sub-intervals, each an empty [start, start) that holds no instant.
  • Returns [] when n is not a positive integer, or on invalid input (start/end that is not a safe integer or numeric string of one — fractions, empty strings and values beyond ±(2^53 − 1) are invalid — or start > end).
  • Returns [] before building any piece when n exceeds options.maxPieces or the longest possible array (2^32 - 1).
ParameterTypeDescription
startstring | numberUnix epoch value, in the one unit all epoch arguments share — interval start
endstring | numberUnix epoch value, in the one unit all epoch arguments share — interval end
nnumbernumber of equal sub-intervals to produce (positive integer)

options

optional: the limit on the number of pieces

OptionTypeDefaultDescription
maxPieces?number1_000_000The most records the result may hold. A result that would hold more, or more than the longest possible array (2^32 - 1 elements), returns [] and none is built. A value that is not a positive safe integer also returns [].

array of n { start, end } records, or [] on invalid input

Members of each item

MemberTypeDescription
startnumberThe instant the interval begins at, as a Unix epoch number in the unit the arguments share.
endnumberThe first instant after the interval, in the same unit as start. It is exclusive: the interval holds everything from start up to but not including this value. It can equal start, which makes the interval empty.
import { intervalDivideEquallyUnix } from "@northguild/gmt/unix/interval";
intervalDivideEquallyUnix(0, 90000000, 3) // [{ start: 0, end: 30000000 }, { start: 30000000, end: 60000000 }, { start: 60000000, end: 90000000 }]
output 

unix/interval/intervalDivideEquallyUnix.ts