datetime/local_date_time/module.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
963b4c02972a2c2fda8e201d29f9470be39d7bc7- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
//! Local civil date-times: exactly a civil date from `std:datetime/date`
//! combined with a local time from `std:datetime/time`. A local date-time is a
//! frozen ordinary object with `date` and `time` slots holding those frozen
//! values. It does not identify an instant and carries no offset, zone, or
//! clock state.
//!
//! Every local date-time's immediate delegation parent is this module, the
//! canonical local date-time factory and prototype. `recognizes` accepts
//! exactly the local date-times of this family: objects whose immediate parent
//! is this module, whose local slots are exactly the local date-time state and
//! behavior slots, and whose components are a recognized date and time.
//! Matching slots under another parent are not a local date-time.
//!
//! Local date-times answer semantic equality through `==`: two are equal when
//! both are local date-times and their dates and their times are equal. `hash` is coherent with that
//! equality, so independently constructed equal local date-times work as the
//! same Map key. `===` remains ordinary object identity. `compare` is the
//! natural order, dates first and then times, and answers `0` exactly for
//! equal local date-times.
/// Answers a fresh frozen local date-time composing `date`, a value recognized
/// by `std:datetime/date`, and `time`, a value recognized by
/// `std:datetime/time`. The components are stored as given. Any other argument
/// signals an Error.
this.construct: (date, time) => {
LocalDateTime: import("std:datetime/local_date_time")
// Hashes are folded modulo a fixed prime so the accumulator stays bounded.
mix: (accumulated, value) => {
(accumulated * 31 + value) % 2305843009213693951
}
behavior: {
equals: (other) => {
dateTime: this
LocalDateTime.recognizes(other) &&
(dateTime.date == other.date) &&
(dateTime.time == other.time)
}
hash: () => {
dateTime: this
mix(mix(17, dateTime.date.hash()), dateTime.time.hash())
}
}
dateTime: (LocalDateTime) {
date: date
time: time
...behavior.alias("equals", "==")
}
dateTime.freeze()
LocalDateTime.recognizes(dateTime).ifFalse(() => {
Error().signal()
})
dateTime
}
/// Answers `true` exactly when `value` is a local date-time of this family:
/// its immediate delegation parent is this module, its local slots are exactly
/// `date`, `time`, `equals`, `==`, and `hash`, `date` is recognized by
/// `std:datetime/date`, and `time` is recognized by `std:datetime/time`.
/// Answers `false` for any other value.
this.recognizes: (value) => {
LocalDateTime: import("std:datetime/local_date_time")
Date: import("std:datetime/date")
Time: import("std:datetime/time")
(value !== null) &&
(value.parent() === LocalDateTime) &&
(value.slotNames().size() == 5) &&
value.hasSlot("date") &&
value.hasSlot("time") &&
value.hasSlot("equals") &&
value.hasSlot("==") &&
value.hasSlot("hash") &&
Date.recognizes(value.date) &&
Time.recognizes(value.time)
}
/// Answers `-1`, `0`, or `1` as local date-time `left` is before, equal to, or
/// after local date-time `right`, ordering by `Date.compare` of the dates and
/// then `Time.compare` of the times. The result is `0` exactly when
/// `left == right`. Arguments must be local date-times recognized by this
/// module; otherwise an Error is signalled.
this.compare: (left, right) => {
LocalDateTime: import("std:datetime/local_date_time")
Date: import("std:datetime/date")
Time: import("std:datetime/time")
(LocalDateTime.recognizes(left) && LocalDateTime.recognizes(right)).ifFalse(() => {
Error().signal()
})
result: Date.compare(left.date, right.date)
if(result == 0) {
result = Time.compare(left.time, right.time)
}
result
}