Chronos is one of the many Smalltalk-related blogs syndicated on Planet Smalltalk
χρόνος

Discussion of the Essence# programming language, and related issues and technologies.

Blog Timezone: America/Los_Angeles [Winter: -0800 hhmm | Summer: -0700 hhmm] 
Your local time:  

2007-01-15

Chronos 101: Intervals of Time

This is the third article in a series of articles that conceptually discuss the Chronos model of time. The first one, Chronos 101: "Points" in Time, explains the way Chronos models "points" in time. The second one, Chronos 101: Durational Values, explains the way Chronos models durations of time.

Time Intervals in Chronos



An interval of time has a beginning, and end, and a duration. Unlike a duration, a time interval has a specific location on the timeline: It starts at a specific moment, and it ends at a specific moment.

The duration of a time interval is the amount of elapsed time from the beginning of the interval up to its end. For example, the first quarter of 2007 is a 3-month time interval starting at the first moment of January 1, 2007 and ending as of the last moment of March 31, 2007. The temporal extent (size, length, duration) of the interval is 3 months. The interval begins as of the first moment of January 1, 2007, and ends as of the last moment of March 31, 2007.

Mathematical Analogy



As used in mathematics, an interval is an ordered set of values on a number line, all of which are greater than one value but less than another. Ignoring advanced mathematics and/or degenerate cases, there are four types of interval:
  • Closed: A closed interval contains both its endpoints, one of which is the starting point and the other the ending point. Example: All the numbers between 3 and 4, including both 3 and 4. More formally: [3, 4] is the set of all x such that 3 <= x <= 4.

  • Open: An open interval contains neither its leftmost (starting) point nor its rightmost (ending) point. Example: All the numbers between 3 and 4, including neither 3 nor 4. More formally: (3, 4) is the set of all x such that 3 < x < 4.

  • Left-closed, right-open: A left-closed, right-open interval contains its leftmost (starting) point, but does not contain its rightmost (ending) point. Example: All the numbers between 3 and 4, including 3 but not including 4. More formally: [3, 4) is the set of all x such that 3 <= x < 4.

  • Left-open, right-closed: A left-open, right-closed interval does not contain its leftmost (starting) point, but does contain its rightmost (ending) point. Example: All the numbers between 3 and 4, excluding 3 but including 4. More formally: (3, 4] is the set of all x such that 3 < x <= 4.


Time intervals in Chronos are always left-closed, right-open intervals. A Chronos interval contains its starting/leftmost point in time, but does not contain its ending/rightmost point in time. In Chronos, a time interval of three months duration that starts as of the first moment of January contains every moment of January 1, and contains every moment of March 31, but does not contain any moments of April 1--not even the very first moment.

Time Intervals with Negative Durations



However, a Chronos time interval may have a negative duration. Example: The interval starting on the initial moment of 2007 and extending backwards in time one calendar year (more formally, the set of moments in time such that, for all x that are members of the set, 2006-01-01T00:00:00 < x <= 2007-01-01T00:00:00.) In the case of Chronos time intervals whose duration is negative, the starting moment occurs temporally later than the ending moment, because a negative duration involves traveling backwards in time. In other words, Chronos time intervals have temporal directionality (forwards or backwards.) Why? Because an algorithm might traverse the timeline either forwards or backwards, even though physical objects can only traverse the timeline in the forward direction. This behavior for Chronos time intervals was chosen so that the semantics of a Chronos time interval depends only on the interval itself, with the effect that algorithms that use a Chronos interval can be independent of whether the interval's directionality is forwards or backwards.

So Chronos intervals with positive duration are "earliest-endpoint-closed, latest-endpoint-open" intervals, but Chronos intervals with negative duration are "earliest-endpoint-open, latest-endpoint-closed" intervals. Consequently, Chronos time intervals preserve the invariant that the starting endpoint (from the perspective of the direction in which the timeline is being traversed) are always included in the interval, but the last endpoint (from the perspective of the direction in which the timeline is being traversed) are never included in the interval. Also, from the perspective of real-world objects that traverse the timeline going forward, this means that earliest-open, latest-closed intervals can be represented by simply making the duration negative (and specifying the temporally-latest point-in-time, instead of the temporally-first point-in-time, as the interval's starting/left-most endpoint.)

It should also be noted that ISO 86O1 does not allow time intervals with negative durations (so "2007-01-01T00:00:00/P-1Y" is not legal ISO 86O1--although Chronos supports it anyway.) But ISO 86O1 does allow the first element of a time interval to be a duration. For example, P1Y/2007-01-01T00:00:00 is a legal IS0 86O1 designation of a time interval, where the starting/rightmost moment is computed by subtracting the duration from the rightmost/ending moment. So P1Y/2007-01-01T00:00:00 designates the time interval containing all moments of the year 2006, but not containing any moments of any other year (more formally, P1Y/2007-01-01T00:00:00 specifies the set of moments in time such that, for all x that are members of the set, 2006-01-01T00:00:00 <= x < 2007-01-01T00:00:00.)

Points-in-Time as Special-Case Time Intervals



Even the Chronos "point-in-time" values (instances of YearMonthDay, Timepoint or InfiniteTimepoint) are implemented as time intervals in Chronos. However, the duration of YearMonthDay, Timepoint and InfiniteTimepoint instances is static, is the same for all instances, and no instance variable is used to specify the duration of a YearMonthDay, Timepoint or InfiniteTimepoint (as discussed in the first essay of this series, Chronos 101: "Points" in Time.)

The duration of a YearMonthDay is one calendar day. The duration of a Timepoint is one nanosecond. The duration of an InfiniteTimepoint is "InfiniteDuration positive."

Time Intervals: The General Case



The Chronos class Timeperiod implements time intervals that can have any duration representable using any of the Chronos durational classes (ScientificDuration, CalendarDuration, CivilDuration, InfiniteDuration--as discussed in the essay Chronos 101: Durational Values.)

Timeperiod is not an abstract class, and has no subclasses. By unifying "points-in-time" with time intervals, by delegating as much responsibility as possible to the Chronos durational values, and by leveraging Smalltalk's dynamic typing, a Timeperiod instance can be composed of any combination of one Chronos "point-in-time" value and one Chronos durational value. So there is no need for time-interval classes such as Year, Month or Week, as can be found in Squeak's Chronology date/time library.


No comments: