civil

package module
v0.0.0-...-c3aeb77 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2018 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Civil date and time

GoDoc Build Status (Linux) Build status (Windows) Coverage Status GoReportCard License

Package civil provides types for representing civil dates, and times.

Sometimes it is useful to be able to represent a date or time without reference to an instance in time within a timezone.

For example, when recording a person's date of birth all that is needed is a date. There is no requirement to specify an instant in time within a timezone.

There are also circumstances where an event will be scheduled for a date and time in the local timezone, whatever that may be. And example of this might be a schedule for taking medication.

Like the standard library time package, the civil package uses the proleptic Gregorian calendar for all calculations. The civil package makes use of the time package for all of its date-time calculations. Because some of this code is based on the standard time package, it has the identical license to the Go project.

For usage examples, refer to the GoDoc documentation.

Documentation

Overview

Package civil provides types for representing civil dates, and times.

Sometimes it is useful to be able to represent a date or time without reference to an instance in time within a timezone.

For example, when recording a person's date of birth all that is needed is a date. There is no requirement to specify an instant in time within a timezone.

There are also circumstances where an event will be scheduled for a date and time in the local timezone, whatever that may be. And example of this might be a schedule for taking medication.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Date

type Date struct {
	// contains filtered or unexported fields
}

Date represents a date without a time or a timezone. Useful for representing date of birth, for example.

Calculations on Date are performed using the standard library's time.Time type. For these calculations the time is midnight and the timezone is UTC.

func DateFor

func DateFor(year int, month time.Month, day int) Date

DateFor returns the Date corresponding to year, month and date.

The month and day values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.

Example
package main

import (
	"fmt"
	"time"

	"github.com/jjeffery/civil"
)

func main() {
	d := civil.DateFor(2009, time.November, 10)
	fmt.Printf("Go launched on %s\n", d)
}
Output:

Go launched on 2009-11-10

func DateOf

func DateOf(t time.Time) Date

DateOf returns the Date corresponding to t in t's location.

func ParseDate

func ParseDate(s string) (Date, error)

ParseDate attempts to parse a string into a civil date. Leading and trailing space and quotation marks are ignored. The following date formats are recognized: yyyy-mm-dd, yyyymmdd, yyyy.mm.dd, yyyy/mm/dd, yyyy-ddd, yyyyddd.

ParseDate is used to parse dates where no layout is provided, for example when marshaling and unmarshaling JSON and XML.

func ParseDateLayout

func ParseDateLayout(layout, value string) (Date, error)

ParseDateLayout parses a formatted string and returns the date value it represents. The layout is based on the standard library time package and for civil dates the reference is

Mon Jan 2 2006

If the layout contains time or timezone fields, they are parsed and discarded.

func Today

func Today() Date

Today returns the current civil date.

Example
package main

import (
	"fmt"
	"time"

	"github.com/jjeffery/civil"
)

func main() {
	_, month, day := civil.Today().Date()
	if month == time.November && day == 10 {
		fmt.Println("Happy Go day!")
	}
}
Output:

func (Date) Add

func (d Date) Add(duration time.Duration) Date

Add returns the civil date d + duration.

func (Date) AddDate

func (d Date) AddDate(years int, months int, days int) Date

AddDate returns the civil date corresponding to adding the given number of years, months, and days to t. For example, AddDate(-1, 2, 3) applied to January 1, 2011 returns March 4, 2010.

AddDate normalizes its result in the same way that Date does, so, for example, adding one month to October 31 yields December 1, the normalized form for November 31.

func (Date) After

func (d Date) After(e Date) bool

After reports whether the civil date d is after e.

func (Date) Before

func (d Date) Before(e Date) bool

Before reports whether the civil date d is before e.

func (Date) Date

func (d Date) Date() (year int, month time.Month, day int)

Date returns the year, month and day on which d occurs.

func (Date) Day

func (d Date) Day() int

Day returns the day of the month specified by d.

func (Date) Equal

func (d Date) Equal(e Date) bool

Equal reports whether d and e represent the same civil date.

func (Date) Format

func (d Date) Format(layout string) string

Format returns a textual representation of the time value formatted according to layout, which takes the same form as the standard library time package. Note that with a Date the reference time is

Mon Jan 2 2006

func (Date) ISOWeek

func (d Date) ISOWeek() (year, week int)

ISOWeek returns the ISO 8601 year and week number in which d occurs. Week ranges from 1 to 53. Jan 01 to Jan 03 of year n might belong to week 52 or 53 of year n-1, and Dec 29 to Dec 31 might belong to week 1 of year n+1.

func (Date) IsZero

func (d Date) IsZero() bool

IsZero reports whether d represents the zero civil date, January 1, year 1.

func (Date) MarshalBinary

func (d Date) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. The date is a quoted string in an ISO 8601 format (yyyy-mm-dd).

func (Date) MarshalText

func (d Date) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaller interface. The date format is yyyy-mm-dd.

func (Date) Month

func (d Date) Month() time.Month

Month returns the month of the year specified by d.

func (*Date) Scan

func (d *Date) Scan(src interface{}) error

Scan implements the sql.Scanner interface.

func (Date) String

func (d Date) String() string

String returns a string representation of d. The date format returned is compatible with ISO 8601: yyyy-mm-dd.

func (Date) Sub

func (d Date) Sub(e Date) time.Duration

Sub returns the duration d-e, which will be an integral number of days. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, the maximum (or minimum) duration will be returned. To compute d-duration, use d.Add(-duration).

func (Date) Unix

func (d Date) Unix() int64

Unix returns d as a Unix time, the number of seconds elapsed since January 1, 1970 UTC to midnight of the date UTC.

func (*Date) UnmarshalBinary

func (d *Date) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface. The date is expected to be a quoted string in an ISO 8601 format (calendar or ordinal).

func (*Date) UnmarshalText

func (d *Date) UnmarshalText(data []byte) (err error)

UnmarshalText implements the encoding.TextUnmarshaller interface. The date is expected to an ISO 8601 format (calendar or ordinal).

func (Date) Value

func (d Date) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

func (Date) Weekday

func (d Date) Weekday() time.Weekday

Weekday returns the day of the week specified by d.

func (Date) Year

func (d Date) Year() int

Year returns the year in which d occurs.

func (Date) YearDay

func (d Date) YearDay() int

YearDay returns the day of the year specified by D, in the range [1,365] for non-leap years, and [1,366] in leap years.

type DateTime

type DateTime struct {
	// contains filtered or unexported fields
}

DateTime represents a date-time without a timezone. Calculations on DateTime are performed using the standard library's time.Time type. For these calculations the timezone is UTC.

DateTime is useful in situations where a date and time are specified, without reference to a timezone. Although not common, it can be useful. For example, a dose of medication may be scheduled for a particular date and time, regardless of the timezone that the patient is residing in at the time.

Because DateTime does not specify a unique instant in time, it has never been necessary to specify to sub-second accuracy. For this reason DateTime only specifies the time to second accuracy. In actual fact, DateTime would probably be fine if it only specified to minute accuracy.

func DateTimeFor

func DateTimeFor(year int, month time.Month, day int, hour int, minute int, second int) DateTime

DateTimeFor returns the DateTime corresponding to year, month, day, hour, minute and second.

The month and day values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.

func DateTimeOf

func DateTimeOf(t time.Time) DateTime

DateTimeOf returns the DateTime corresponding to t in t's location.

func Now

func Now() DateTime

Now returns the current civil date-time.

func ParseDateTime

func ParseDateTime(s string) (DateTime, error)

ParseDateTime attempts to parse a string into a civil date-time. Leading and trailing space and quotation marks are ignored. The following date formats are recognized: yyyy-mm-dd, yyyymmdd, yyyy.mm.dd, yyyy/mm/dd, yyyy-ddd, yyyyddd. The following time formats are recognized: HH:MM:SS, HH:MM, HHMMSS, HHMM.

func ParseDateTimeLayout

func ParseDateTimeLayout(layout, value string) (DateTime, error)

ParseDateTimeLayout parses a formatted string and returns the date value it represents. The layout is based on the standard library time package and for civil date-times the reference is

Mon Jan 2 2006 15:04:05

If the layout contains a timezone field, it is parsed and discarded.

func (DateTime) Add

func (dt DateTime) Add(duration time.Duration) DateTime

Add returns the civil date-time d + duration.

func (DateTime) AddDate

func (dt DateTime) AddDate(years int, months int, days int) DateTime

AddDate returns the civil date-time corresponding to adding the given number of years, months, and days to t. For example, AddDate(-1, 2, 3) applied to January 1, 2011 returns March 4, 2010.

AddDate normalizes its result in the same way that Date does, so, for example, adding one month to October 31 yields December 1, the normalized form for November 31.

func (DateTime) After

func (dt DateTime) After(e DateTime) bool

After reports whether the civil date-time d is after e

func (DateTime) Before

func (dt DateTime) Before(e DateTime) bool

Before reports whether the civil date-time d is before e

func (DateTime) Clock

func (dt DateTime) Clock() (hour int, minute int, second int)

Clock returns the hour, minute and second on which dt occurs.

func (DateTime) Date

func (dt DateTime) Date() (year int, month time.Month, day int)

Date returns the year, month and day on which dt occurs.

func (DateTime) DateTime

func (dt DateTime) DateTime() (year int, month time.Month, day int, hour int, minute int, second int)

DateTime returns the year, month, day, hour minute, second and nanosecond on which dt occurs.

func (DateTime) Day

func (dt DateTime) Day() int

Day returns the day of the month specified by dt.

func (DateTime) Equal

func (dt DateTime) Equal(e DateTime) bool

Equal reports whether dt and e represent the same civil date-time.

func (DateTime) Format

func (dt DateTime) Format(layout string) string

Format returns a textual representation of the time value formatted according to layout, which takes the same form as the standard library time package. Note that with a Date the reference time is

Mon Jan 2 2006 15:04:05.

func (DateTime) Hour

func (dt DateTime) Hour() int

Hour returns the hour specified by dt.

func (DateTime) ISOWeek

func (dt DateTime) ISOWeek() (year, week int)

ISOWeek returns the ISO 8601 year and week number in which d occurs. Week ranges from 1 to 53. Jan 01 to Jan 03 of year n might belong to week 52 or 53 of year n-1, and Dec 29 to Dec 31 might belong to week 1 of year n+1.

func (DateTime) IsZero

func (dt DateTime) IsZero() bool

IsZero reports whether dt represents the zero civil date-time, Midnight, January 1, year 1.

func (DateTime) MarshalBinary

func (dt DateTime) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (DateTime) MarshalJSON

func (dt DateTime) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. The date is a quoted string in an ISO 8601 format (yyyy-mm-ddTHH:MM:SS).

func (DateTime) MarshalText

func (dt DateTime) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaller interface. The date format is yyyy-mm-dd.

func (DateTime) Minute

func (dt DateTime) Minute() int

Minute returns the minute specified by dt.

func (DateTime) Month

func (dt DateTime) Month() time.Month

Month returns the month of the year specified by dt.

func (*DateTime) Scan

func (dt *DateTime) Scan(src interface{}) error

Scan implements the sql.Scanner interface.

func (DateTime) Second

func (dt DateTime) Second() int

Second returns the second specified by dt.

func (DateTime) String

func (dt DateTime) String() string

String returns a string representation of d. The date format returned is compatible with ISO 8601: yyyy-mm-ddTHH:MM:SS.

func (DateTime) Sub

func (dt DateTime) Sub(e DateTime) time.Duration

Sub returns the duration dt-e, which will be an integral number of seconds. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, the maximum (or minimum) duration will be returned. To compute dt-duration, use dt.Add(-duration).

func (DateTime) Unix

func (dt DateTime) Unix() int64

Unix returns d as a Unix time, the number of seconds elapsed since January 1, 1970 UTC to midnight of the date-time UTC.

func (*DateTime) UnmarshalBinary

func (dt *DateTime) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

func (*DateTime) UnmarshalJSON

func (dt *DateTime) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface. The date is expected to be a quoted string in an ISO 8601 format (calendar or ordinal).

func (*DateTime) UnmarshalText

func (dt *DateTime) UnmarshalText(data []byte) (err error)

UnmarshalText implements the encoding.TextUnmarshaller interface. The date is expected to an ISO 8601 format (calendar or ordinal).

func (DateTime) Value

func (dt DateTime) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

func (DateTime) Weekday

func (dt DateTime) Weekday() time.Weekday

Weekday returns the day of the week specified by d.

func (DateTime) Year

func (dt DateTime) Year() int

Year returns the year in which dt occurs.

func (DateTime) YearDay

func (dt DateTime) YearDay() int

YearDay returns the day of the year specified by D, in the range [1,365] for non-leap years, and [1,366] in leap years.

type NullDate

type NullDate struct {
	Date  Date
	Valid bool // Valid is true if Date is not NULL
}

NullDate represents a Date that may be null. NullDate implements the sql Scanner interface so it can be used as a scan destination, similar to sql.NullString.

func NullDateFrom

func NullDateFrom(ptr *Date) NullDate

NullDateFrom returns a NullDate whose value is obtained from the pointer.

func (NullDate) MarshalJSON

func (n NullDate) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (NullDate) Ptr

func (n NullDate) Ptr() *Date

Ptr returns a pointer to Date. The pointer will be nil if Valid is false.

func (*NullDate) Scan

func (n *NullDate) Scan(value interface{}) error

Scan implements the sql Scanner interface

func (*NullDate) UnmarshalJSON

func (n *NullDate) UnmarshalJSON(p []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (NullDate) Value

func (n NullDate) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullDateTime

type NullDateTime struct {
	DateTime DateTime
	Valid    bool // Valid is true if Date is not NULL
}

NullDateTime represents a DateTime that may be null. NullDateTime implements the sql Scanner interface so it can be used as a scan destination, similar to sql.NullString.

func NullDateTimeFrom

func NullDateTimeFrom(ptr *DateTime) NullDateTime

NullDateTimeFrom returns a NullDateTime whose value is obtained from the pointer.

func (NullDateTime) MarshalJSON

func (n NullDateTime) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (NullDateTime) Ptr

func (n NullDateTime) Ptr() *DateTime

Ptr returns a pointer to DateTime. The pointer will be nil if Valid is false.

func (*NullDateTime) Scan

func (n *NullDateTime) Scan(value interface{}) error

Scan implements the sql Scanner interface

func (*NullDateTime) UnmarshalJSON

func (n *NullDateTime) UnmarshalJSON(p []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (NullDateTime) Value

func (n NullDateTime) Value() (driver.Value, error)

Value implements the driver Valuer interface.

Jump to

Keyboard shortcuts

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