18-application-libraries/01-dates-and-durations.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
48078fca2ebf45ff8df6eaeec1965035ce1121a6
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.

// Dates, times and durations with std:datetime/*.
//
// Civil values (dates, times, local date-times) carry no time zone. An offset
// date-time pairs a local date-time with a fixed offset. An instant is a
// position on the timeline. A duration is an exact number of nanoseconds; a
// period is calendar years, months and days. Nothing here reads a clock or a
// time-zone database.

Date: import("std:datetime/date")
Time: import("std:datetime/time")
LocalDateTime: import("std:datetime/local_date_time")
Offset: import("std:datetime/offset")
OffsetDateTime: import("std:datetime/offset_date_time")
Instant: import("std:datetime/instant")
Duration: import("std:datetime/duration")
Period: import("std:datetime/period")
ISO8601: import("std:datetime/iso8601")
RFC3339: import("std:datetime/rfc3339")

// Civil dates are validated, never clamped.
due: Date(2026, 1, 31)
print(ISO8601.formatDate(due))                              // 2026-01-31
print(Error.handle(() => { Date(2026, 2, 30); "accepted" }, (error) => { "invalid date rejected" }))

// A period applies calendar arithmetic: one month after January 31 is clamped
// to the last day of February.
print(ISO8601.formatDate(Period.addToDate(Period(0, 1, 0), due)))   // 2026-02-28
print(Date.compare(due, ISO8601.parseDate("2026-02-01")))   // -1

// Durations are exact nanosecond amounts.
second: 1000000000
meeting: Duration(90 * 60 * second)
pause: Duration(15 * 60 * second)
total: Duration.add(meeting, pause)
print(total.nanoseconds / (60 * second))                   // 105
print(Duration.compare(meeting, pause))                     // 1

// An offset date-time identifies one instant; changing its offset keeps it.
departure: ISO8601.parseOffsetDateTime("2026-12-03T18:30:00-03:00")
print(RFC3339.formatOffsetDateTime(departure))              // 2026-12-03T18:30:00-03:00
instant: OffsetDateTime.toInstant(departure)
print(ISO8601.formatInstant(instant))                       // 2026-12-03T21:30:00Z

// Instants and durations compose on the timeline.
arrival: Instant.addDuration(instant, Duration(11 * 3600 * second))
print(ISO8601.formatInstant(arrival))                       // 2026-12-04T08:30:00Z
print(Instant.durationBetween(instant, arrival).nanoseconds == 11 * 3600 * second)  // true

// The same instant written with another fixed offset.
utc: OffsetDateTime.fromInstant(arrival, Offset(0))
print(OffsetDateTime.sameInstant(utc, OffsetDateTime.fromInstant(arrival, Offset(3600))))  // true

// Strict formats reject partial or ambiguous text.
print(Error.handle(() => { ISO8601.parseDate("2026-1-5"); "accepted" }, (error) => { "non-canonical date rejected" }))