Using Calends in Golang

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 calendars.TAI64NARUXTime 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 Golang section.

备注

Calends objects are immutable - all methods return a new Calends object where they might otherwise alter the current one. This is true even of the Calends.Set* methods. 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.

Calends

The main entry point to Calends and its functionality. Calends objects should only be created with the Create function, and never directly (especially given its values are all unexported ones).

Create

func Create(value interface, calendar string, format string) (Calends, error)
参数
  • value (interface{}) – The value 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.

返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Creates a new Calends object, using calendar to select a calendar system, and format to parse the contents of value into the Calends object’s internal instants. The type of value can vary based on the calendar system itself, but generally speaking can always be a string.

In any case, the value can always be a map[string]interface{}, where the keys are any two of start, end, and duration. If all three are provided, duration is ignored in favor of calculating it directly. If only one of the listed keys is provided, value is passed to the calendar system itself unchanged.

The calendar system then converts value to a calendars.TAI64NARUXTime instant, which the Calends object sets to the appropriate internal value.

Read

func (CalendsDate(calendar string, format string) (string, error)
参数
  • calendar (string) – The calendar system to format the date/time with.

  • format (string) – The format the date/time is expected to be in.

返回

The start date of the Calends object

返回类型

string

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Retrieves the start date of the Calends object as a string. The value is generated by the calendar system given in calendar, according to the format string in format.

func (CalendsEndDate(calendar string, format string) (string, error)
参数
  • calendar (string) – The calendar system to format the date/time with.

  • format (string) – The format the date/time is expected to be in.

返回

The end date of the Calends object

返回类型

string

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Retrieves the end date of the Calends object as a string. The value is generated by the calendar system given in calendar, according to the format string in format.

func (CalendsDuration() Float
返回

The duration of the Calends object

返回类型

math/big.(*Float)

Retrieves the duration of the Calends object as an arbitrary-precision floating point number. This value will be 0 if the Calends object is an instant.

Update

func (CalendsSetDate(stamp interface, calendar string, format string) (Calends, error)
参数
  • value (interface{}) – 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.

返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Sets the start date of a Calends object, based on the Calends object’s current value. The inputs are the same as for Create, above, except the string → value map option isn’t available, since you’re already specifically setting the start value explicitly.

func (CalendsSetEndDate(stamp interface, calendar string, format string) (Calends, error)
参数
  • value (interface{}) – 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.

返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Sets the end date of a Calends object, based on the Calends object’s current value. The inputs are the same as for Create, above, except the string → value map option isn’t available, since you’re already specifically setting the end value explicitly.

func (CalendsSetDuration(duration interface, calendar string) (Calends, error)
参数
  • duration (interface{}) – The value to parse the new duration from.

  • calendar (string) – The calendar system to parse the date/time with.

返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Sets the duration of a Calends object, by adjusting its end point, and using the current start point as an anchor. The duration value is interpreted by the calendar system given in calendar, so is subject to any of its rules.

func (CalendsSetDurationFromEnd(duration interface, calendar string) (Calends, error)
参数
  • duration (interface{}) – The value to parse the new duration from.

  • calendar (string) – The calendar system to parse the date/time with.

返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Sets the duration of a Calends object, by adjusting its start point, and using the current end point as an anchor. The duration value is interpreted by the calendar system given in calendar, so is subject to any of its rules.

Manipulate

func (CalendsAdd(offset interface, calendar string) (Calends, error)
参数
  • offset (interface{}) – The value to parse the offset from.

  • calendar (string) – The calendar system to parse the date/time with.

返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Increases the end date of the Calends object’s current value by offset, as interpreted by the calendar system given in calendar.

func (CalendsAddFromEnd(offset interface, calendar string) (Calends, error)
参数
  • offset (interface{}) – The value to parse the offset from.

  • calendar (string) – The calendar system to parse the date/time with.

返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Increases the start date of the Calends object’s current value by offset, as interpreted by the calendar system given in calendar.

func (CalendsSubtract(offset interface, calendar string) (Calends, error)
参数
  • offset (interface{}) – The value to parse the offset from.

  • calendar (string) – The calendar system to parse the date/time with.

返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Works the same as Add, except it decreases the start date, rather than increasing it.

func (CalendsSubtractFromEnd(offset interface, calendar string) (Calends, error)
参数
  • offset (interface{}) – The value to parse the offset from.

  • calendar (string) – The calendar system to parse the date/time with.

返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Works the same as AddFromEnd, except it decreases the end date, rather than increasing it.

func (CalendsNext(offset interface, calendar string) (Calends, error)
参数
  • offset (interface{}) – The value to parse the offset from.

  • calendar (string) – The calendar system to parse the date/time with.

返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Returns a Calends object of offset duration (as interpreted by the calendar system given in calendar), which abuts the Calends object’s current value. If offset is empty, calendar is ignored, and the current object’s duration is used instead.

func (CalendsPrevious(offset interface, calendar string) (Calends, error)
参数
  • offset (interface{}) – The value to parse the offset from.

  • calendar (string) – The calendar system to parse the date/time with.

返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Returns a Calends object of offset duration (as interpreted by the calendar system given in calendar), which abuts the Calends object’s current value. If offset is empty, calendar is ignored, and the current object’s duration is used instead.

Combine

func (CalendsMerge(c2 Calends) (Calends, error)
参数
返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Returns a Calends object spanning from the earliest start date to the latest end date between the current Calends object and c2.

func (CalendsIntersect(c2 Calends) (Calends, error)
参数
返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Returns a Calends object spanning the overlap between the current Calends object and c2. If the current object and c2 don’t overlap, returns an error.

func (CalendsGap(c2 Calends) (Calends, error)
参数
返回

A new Calends object

返回类型

Calends

返回

error when an error occurs; nil otherwise

返回类型

error or nil

Returns a Calends object spanning the gap between the current Calends object and c2. If the current object and c2 overlap (and there is, therefore, no gap to return), returns an error.

Compare

func (CalendsDifference(c2 Calends, mode string) Float
参数
  • c2 (Calends) – The Calends object to compare.

  • mode (string) – The comparison mode.

返回

The difference, as an arbitrary-precision floating point number

返回类型

math/big.Float

Returns the difference of the current Calends object minus c2, using mode to select which values to use in the calculation. Valid modes include:

  • start - use the start date of both the current object and c2

  • duration - use the duration of both the current object and c2

  • end - use the end date of both the current object and c2

  • start-end - use the start of the current object, and the end of c2

  • end-start - use the end of the current object, and the start of c2

func (CalendsCompare(c2 Calends, mode string) int
参数
  • c2 (Calends) – The Calends object to compare.

  • mode (string) – The comparison mode.

返回

A standard comparison result

返回类型

int

Returns -1 if the current Calends object is less than c2, 0 if they are equal, and 1 if the current object is greater than c2, using mode to select which values to use in the comparison. Valid modes are the same as for (Calends) Difference, above.

func (CalendsContains(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Checks whether the current Calends object contains all of c2.

func (CalendsOverlaps(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Checks whether the current Calends object overlaps with c2.

func (CalendsAbuts(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Checks whether the current Calends object abuts c2 (that is, whether one begins at the same instant the other ends).

func (CalendsIsSame(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Checks whether the current Calends object covers the same span of time as c2.

func (CalendsIsShorter(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Compares the duration of the current Calends object and c2.

func (CalendsIsSameDuration(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Compares the duration of the current Calends object and c2.

func (CalendsIsLonger(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Compares the duration of the current Calends object and c2.

func (CalendsIsBefore(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Compares the entirety of the current Calends object with the start date of c2.

func (CalendsStartsBefore(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Compares the start date of the current Calends object with the start date of c2.

func (CalendsEndsBefore(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Compares the end date of the current Calends object with the start date of c2.

func (CalendsIsDuring(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Checks whether the entirety of the current Calends object lies between the start and end dates of c2.

func (CalendsStartsDuring(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Checks whether the start date of the current Calends object lies between the start and end dates of c2.

func (CalendsEndsDuring(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Checks whether the end date of the current Calends object lies between the start and end dates of c2.

func (CalendsIsAfter(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Compares the entirety of the current Calends object with the end date of c2.

func (CalendsStartsAfter(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Compares the start date of the current Calends object with the end date of c2.

func (CalendsEndsAfter(c2 Calends) bool
参数
返回

The result of the comparison

返回类型

bool

Compares the end date of the current Calends object with the end date of c2.

Export

func (CalendsString() string
返回

The string representation of the current value.

返回类型

string

Implements the fmt.Stringer interface.

func (CalendsMarshalText(([]byte, error)
返回

A byte slice containing the marshalled text.

返回类型

[]byte

返回

Any error that occurs.

返回类型

error

Implements the encoding.TextMarshaler interface.

func (*CalendsUnmarshalText(in []byte) error
参数
  • in ([]byte) – A byte slice containing the marshalled text.

返回

Any error that occurs.

返回类型

error

Implements the encoding.TextUnmarshaler interface.

func (CalendsMarshalJSON(([]byte, error)
返回

A byte slice containing the marshalled JSON.

返回类型

[]byte

返回

Any error that occurs.

返回类型

error

Implements the encoding/json.Marshaler interface.

func (*CalendsUnmarshalJSON(in []byte) error
参数
  • in ([]byte) – A byte slice containing the marshalled JSON.

返回

Any error that occurs.

返回类型

error

Implements the encoding/json.Unmarshaler interface.