dates

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const ErrNotDate = domain.Error("Value is not a date")

ErrNotDate is used to communicate that a value is not able to be parsed as a date

Variables

This section is empty.

Functions

func IsValid

func IsValid(date string) bool

IsValid checks whether or not the input string is in "YYYY-MM-DD [BC]" format. For example, the following strings are all valid: "1769-08-15", "1769-8-15", "1769-8", "1769" and "1457-04-16 BC"

Types

type Historic

type Historic struct {
	Year  int  `json:"year"`
	Month int  `json:"month,omitempty"`
	Day   int  `json:"day,omitempty"`
	IsBCE bool `json:"isBCE"`
}

Historic represents a specific date from history. It stores the year, month, day and if whether or not the underlying date was BCE. Both month and day are optional, because some dates in history do not have that level of precision

func New

func New(s string) (Historic, error)

New attempts to create a Historic date from its input string. At the moment this string has to be in YYYY-MM-DD [BC] format. For example, the following string dates are all valid inputs for this function: "1769-08-15", "1769-8-15", "1769-8", "1769" and "1457-04-16 BC"

func Parse

func Parse(t string) ([]Historic, error)

Parse receives text containing a potential date or range of dates, and translates them into a slice of Historic, where the first element is the earliest date detected in the text, and the second element is the latest date detected in the text. If there is just one date detected, then the slice will just have one element

func (Historic) IsValid

func (h Historic) IsValid() bool

IsValid applies the IsValid algorithm to check if the underlying date is valid or not

func (Historic) String

func (h Historic) String() string

func (Historic) ToBeginning

func (h Historic) ToBeginning() Historic

ToBeginning fills the missing month and day of a partial Historic by setting them to 1 if they are missing. For example, the underlying date "1769" is converted into "1769-01-01", "1769-08" is converted into "1769-08-01", and "1769-08-15" stays the same

func (Historic) ToEnd

func (h Historic) ToEnd() Historic

ToEnd fills the missing month and day of a partial Historic by setting them to the last month of the year (unless specified), and to the last day of that month (unless specified). For example, the underlying date "1769" is converted into "1769-12-31", "1769-08" is converted into "1769-08-31", and "1769-08-15" stays the same

func (Historic) ToNum

func (h Historic) ToNum() float64

ToNum converts the Historic to a floating-point number, where the magnitude of the result will be directly proportional to how recent the underlying date was. For this purpose, if the Month or Day values of the Historic are zero, they will not be considered in this calculaiton. Following this logic, "1769-08-15" will be greater than "1769-08", which will in turn be greater than "1769". Whether or not the date is BCE is also considered, so "1769" will be greater than "1769 BC", in the same way that "31-09-02 BC" will be greater than "52-09 BC"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL