Using Calends in Dart
Calends exposes a very small handful of things for use outside the library
itself. One is the Calends class, documented here, which should
be the only interface users of the library ever need to touch.
Another thing Calends exposes is the TAI64Time class, used to
store and manipulate the instants of time which make up a Calends
instance. The rest are interfaces for extending the library’s functionality.
These are covered in the Custom Calendars in Dart
section.
Notă
Calends objects are immutable - all methods return a new
Calends object where they might otherwise alter the current
one. This has the side effect of using more memory to perform manipulations
than updating values on an existing object would. It makes many operations
safer, though, than mutable objects would allow.
- class calends.Calends
The main entry point to Calends and its functionality.
- Calends.initialize()
Sets up the error handling and otherwise preps the library for use. Run this before doing anything else!
Create
- Calends.(stamp, calendar, format)
- Argumente
stamp (
dynamic()) – The timestamp to parse the date(s)/time(s) from.calendar (
String()) – The calendar system to parse the date(s)/time(s) with.format (
String()) – The format the date(s)/time(s) is/are expected to use.- Generează
CalendsException()– when an error occursCreates a new
Calendsobject, usingcalendarto select a calendar system, andformatto parse the contents ofstampinto theCalendsobject’s internal instants. The type ofstampcan vary based on the calendar system itself, but generally speaking can always be a string.In any case, the stamp can also be a
Map, where theStringkeys are any two ofstart,end, andduration. If all three are provided,durationis ignored in favor of calculating it directly.The calendar system then converts
stampto a pair ofTAI64Timeinstants, which theCalendsobject sets to the appropriate internal value.
Read
- Calends.date(calendar, format)
- Argumente
calendar (
String()) – The calendar system to format the date/time with.format (
String()) – The format the date/time is expected to be in.- Întoarce
The start date of the
Calendsobject- Tipul întors
string
- Generează
CalendsException()– when an error occursRetrieves the start date of the
Calendsobject as a string. The value is generated by the calendar system given incalendar, according to the format string informat.
- Calends.endDate(calendar, format)
- Argumente
calendar (
String()) – The calendar system to format the date/time with.format (
String()) – The format the date/time is expected to be in.- Întoarce
The end date of the
Calendsobject- Tipul întors
string
- Generează
CalendsException()– when an error occursRetrieves the end date of the
Calendsobject as a string. The value is generated by the calendar system given incalendar, according to the format string informat.
Update
- Calends.withDate(stamp, calendar, format)
- Argumente
stamp (
dynamic()) – The value to parse the date/time from.calendar (
String()) – The calendar system to parse the date/time with.format (
String()) – The format the date/time is expected to use.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursReturns a
Calendsobject with a start date based on the currentCalendsobject’s value. The inputs are the same as forCalends.(), above, except theString→ value map option isn’t available, since you’re already specifically setting the start value explicitly.
- Calends.withEndDate(value, calendar, format)
- Argumente
stamp (
dynamic()) – The value to parse the date/time from.calendar (
String()) – The calendar system to parse the date/time with.format (
String()) – The format the date/time is expected to use.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursReturns a
Calendsobject with an end date based on the currentCalendsobject’s value. The inputs are the same as forCalends.(), above, except theString→ value map option isn’t available, since you’re already specifically setting the end value explicitly.
- Calends.withDuration(duration, calendar)
- Argumente
duration (
dynamic()) – The value to parse the new duration from.calendar (
String()) – The calendar system to parse the date/time with.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursReturns a
Calendsobject with a duration set by adjusting the currentCalendsobject’s end point, and using its start point as an anchor. Thedurationvalue is interpreted by the calendar system given incalendar, so is subject to any of its rules.
- Calends.withDurationFromEnd(duration, calendar)
- Argumente
duration (
dynamic()) – The value to parse the new duration from.calendar (
String()) – The calendar system to parse the date/time with.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursReturns a
Calendsobject with a duration set by adjusting the currentCalendsobject’s start point, and using its end point as an anchor. Thedurationvalue is interpreted by the calendar system given incalendar, so is subject to any of its rules.
Manipulate
- Calends.add(offset, calendar)
- Argumente
offset (
dynamic()) – The value to parse the offset from.calendar (
String()) – The calendar system to parse the date/time with.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursIncreases the end date of the
Calendsobject’s current value byoffset, as interpreted by the calendar system given incalendar, and returns a newCalendsobject with the result.
- Calends.addFromEnd(offset, calendar)
- Argumente
offset (
dynamic()) – The value to parse the offset from.calendar (
String()) – The calendar system to parse the date/time with.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursIncreases the start date of the
Calendsobject’s current value byoffset, as interpreted by the calendar system given incalendar, and returns a newCalendsobject with the result.
- Calends.subtract(offset, calendar)
- Argumente
offset (
dynamic()) – The value to parse the offset from.calendar (
String()) – The calendar system to parse the date/time with.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursWorks the same as
add(), except it decreases the start date, rather than increasing it.
- Calends.subtractFromEnd(offset, calendar)
- Argumente
offset (
dynamic()) – The value to parse the offset from.calendar (
String()) – The calendar system to parse the date/time with.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursWorks the same as
addFromEnd(), except it decreases the end date, rather than increasing it.
- Calends.next(offset, calendar)
- Argumente
offset (
dynamic()) – The value to parse the offset from.calendar (
String()) – The calendar system to parse the date/time with.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursReturns a
Calendsobject ofoffsetduration (as interpreted by the calendar system given incalendar), which abuts the currentCalendsobject’s value. Ifoffsetis empty,calendaris ignored, and the current object’s duration is used instead.
- Calends.previous(offset, calendar)
- Argumente
offset (
dynamic()) – The value to parse the offset from.calendar (
String()) – The calendar system to parse the date/time with.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursReturns a
Calendsobject ofoffsetduration (as interpreted by the calendar system given incalendar), which abuts the currentCalendsobject’s value. Ifoffsetis empty,calendaris ignored, and the current object’s duration is used instead.
Combine
- Calends.merge(c2)
- Argumente
- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursReturns a
Calendsobject spanning from the earliest start date to the latest end date between the currentCalendsobject andc2.
- Calends.intersect(c2)
- Argumente
- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursReturns a
Calendsobject spanning the overlap between the currentCalendsobject andc2. If the current object andc2don’t overlap, throws an error.
- Calends.gap(c2)
- Argumente
- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occursReturns a
Calendsobject spanning the gap between the currentCalendsobject andc2. If the current object andc2overlap (and there is, therefore, no gap to return), throws an error.
Compare
- Calends.difference(c2, mode)
- Argumente
- Întoarce
The difference, as a decimal string
- Tipul întors
string
Returns the difference of the current
Calendsobject minusc2, usingmodeto select which values to use in the calculation. Validmodes include:
start- use the start date of both the current object andc2
duration- use the duration of both the current object andc2
end- use the end date of both the current object andc2
start-end- use the start of the current object, and the end ofc2
end-start- use the end of the current object, and the start ofc2
- Calends.compare(c2, mode)
- Argumente
- Întoarce
A standard comparison result
- Tipul întors
int
Returns
-1if the currentCalendsobject is less thanc2,0if they are equal, and1if the current object is greater thanc2, usingmodeto select which values to use in the comparison. Validmodes are the same as fordifference(), above.
- Calends.contains(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Checks whether the current
Calendsobject contains all ofc2.
- Calends.overlaps(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Checks whether the current
Calendsobject overlaps withc2.
- Calends.abuts(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Checks whether the current
Calendsobject abutsc2(that is, whether one begins at the same instant the other ends).
- Calends.isSame(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Checks whether the current
Calendsobject covers the same span of time asc2. Also available via the==operator.
- Calends.isShorter(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Compares the duration of the current
Calendsobject andc2.
- Calends.isSameDuration(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Compares the duration of the current
Calendsobject andc2.
- Calends.isLonger(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Compares the duration of the current
Calendsobject andc2.
- Calends.isBefore(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Compares the entirety of the current
Calendsobject with the start date ofc2. Also available as the<operator.
- Calends.startsBefore(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Compares the start date of the current
Calendsobject with the start date ofc2.
- Calends.endsBefore(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Compares the end date of the current
Calendsobject with the start date ofc2.
- Calends.isDuring(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Checks whether the entirety of the current
Calendsobject lies between the start and end dates ofc2.
- Calends.startsDuring(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Checks whether the start date of the current
Calendsobject lies between the start and end dates ofc2.
- Calends.endsDuring(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Checks whether the end date of the current
Calendsobject lies between the start and end dates ofc2.
- Calends.isAfter(c2)
- Argumente
- Întoarce
The result of the comparison
- Tipul întors
bool
Compares the entirety of the current
Calendsobject with the end date ofc2. Also available as the>operator.
Export
It’s possible to export Calends values in a couple of ways, and
then re-import them later/elsewhere. The text encoding is the less portable
option, though, so we strongly recommend using JSON instead.
- Calends.encodeText()
- Întoarce
The text-encoded value of the
Calendsinstance.- Tipul întors
String- Generează
CalendsException()– when an error occurs
- Calends.decodeText(encoded)
- Argumente
encoded (
String()) – The text-encoded value to import.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occurs
- Calends.encodeJson()
- Întoarce
The JSON-encoded value of the
Calendsinstance.- Tipul întors
String- Generează
CalendsException()– when an error occurs
- Calends.decodeJson(encoded)
- Argumente
encoded (
String()) – The JSON-encoded value to import.- Întoarce
A new
Calendsobject- Tipul întors
- Generează
CalendsException()– when an error occurs
If you just need a String value for display purposes, and can’t use the
date() method to format it to a given calendar system, there is a
fallback mechanism in the form of the toString() method. This value
is neither human-readable nor machine-importable, so it should only be used for
debugging.
- Calends.toString()
- Întoarce
A non-portable representation of the
Calendsinstance.- Tipul întors
String- Generează
CalendsException()– when an error occurs
Error Handling
- class calends.CalendsException
A very simple exception class, directly extending
Exception. It is thrown by the library for all encountered errors.