Package date
Package date provides time.Time derivatives that conform to the Swagger.io (https://swagger.io/)
defined date formats: Date and DateTime. Both types may, in most cases, be used in lieu of
time.Time types. And both convert to time.Time through a ToTime method.
- func ParseTime(format string, t string) (d time.Time, err error)
- func UnixEpoch() time.Time
- type Date
- func ParseDate(date string) (d Date, err error)
- func (d Date) MarshalBinary() ([]byte, error)
- func (d Date) MarshalJSON() (json []byte, err error)
- func (d Date) MarshalText() (text []byte, err error)
- func (d Date) String() string
- func (d Date) ToTime() time.Time
- func (d *Date) UnmarshalBinary(data []byte) error
- func (d *Date) UnmarshalJSON(data []byte) (err error)
- func (d *Date) UnmarshalText(data []byte) (err error)
- type Time
- func (t Time) MarshalBinary() ([]byte, error)
- func (t Time) MarshalJSON() (json []byte, err error)
- func (t Time) MarshalText() (text []byte, err error)
- func (t Time) String() string
- func (t Time) ToTime() time.Time
- func (t *Time) UnmarshalBinary(data []byte) error
- func (t *Time) UnmarshalJSON(data []byte) (err error)
- func (t *Time) UnmarshalText(data []byte) (err error)
- type TimeRFC1123
- func (t TimeRFC1123) MarshalBinary() ([]byte, error)
- func (t TimeRFC1123) MarshalJSON() ([]byte, error)
- func (t TimeRFC1123) MarshalText() ([]byte, error)
- func (t TimeRFC1123) String() string
- func (t TimeRFC1123) ToTime() time.Time
- func (t *TimeRFC1123) UnmarshalBinary(data []byte) error
- func (t *TimeRFC1123) UnmarshalJSON(data []byte) (err error)
- func (t *TimeRFC1123) UnmarshalText(data []byte) (err error)
- type UnixTime
- func NewUnixTimeFromDuration(dur time.Duration) UnixTime
- func NewUnixTimeFromNanoseconds(nanoseconds int64) UnixTime
- func NewUnixTimeFromSeconds(seconds float64) UnixTime
- func (t UnixTime) Duration() time.Duration
- func (t UnixTime) MarshalBinary() ([]byte, error)
- func (t UnixTime) MarshalJSON() ([]byte, error)
- func (t UnixTime) MarshalText() ([]byte, error)
- func (t *UnixTime) UnmarshalBinary(raw []byte) error
- func (t *UnixTime) UnmarshalJSON(text []byte) error
- func (t *UnixTime) UnmarshalText(raw []byte) error
Package files
date.go
time.go
timerfc1123.go
unixtime.go
utility.go
func ParseTime(format string, t string) (d time.Time, err error)
ParseTime to parse Time string to specified format.
▾ Example
Code:
d, _ := ParseTime(rfc3339, "2001-02-03T04:05:06Z")
fmt.Println(d)
Output:
2001-02-03 04:05:06 +0000 UTC
func UnixEpoch() time.Time
UnixEpoch retreives the moment considered the Unix Epoch. I.e. The time represented by '0'
Date defines a type similar to time.Time but assumes a layout of RFC3339 full-date (i.e.,
2006-01-02).
type Date struct {
time.Time
}
▾ Example
Code:
d, err := ParseDate("2001-02-03")
if err != nil {
fmt.Println(err)
}
t, err := time.Parse(time.RFC3339, "2001-02-04T00:00:00Z")
if err != nil {
fmt.Println(err)
}
if d.Before(t) {
fmt.Printf("Before ")
} else {
fmt.Printf("After ")
}
if t.After(d.ToTime()) {
fmt.Printf("After")
} else {
fmt.Printf("Before")
}
Output:
Before After
func ParseDate(date string) (d Date, err error)
ParseDate create a new Date from the passed string.
▾ Example
Code:
d, err := ParseDate("2001-02-03")
if err != nil {
fmt.Println(err)
}
fmt.Println(d)
Output:
2001-02-03
func (d Date) MarshalBinary() ([]byte, error)
MarshalBinary preserves the Date as a byte array conforming to RFC3339 full-date (i.e.,
2006-01-02).
▾ Example
Code:
d, err := ParseDate("2001-02-03")
if err != nil {
fmt.Println(err)
}
t, err := d.MarshalBinary()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(t))
Output:
2001-02-03
func (d Date) MarshalJSON() (json []byte, err error)
MarshalJSON preserves the Date as a JSON string conforming to RFC3339 full-date (i.e.,
2006-01-02).
▾ Example
Code:
d, err := ParseDate("2001-02-03")
if err != nil {
fmt.Println(err)
}
j, err := json.Marshal(d)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(j))
Output:
"2001-02-03"
func (d Date) MarshalText() (text []byte, err error)
MarshalText preserves the Date as a byte array conforming to RFC3339 full-date (i.e.,
2006-01-02).
▾ Example
Code:
d, err := ParseDate("2001-02-03")
if err != nil {
fmt.Println(err)
}
t, err := d.MarshalText()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(t))
Output:
2001-02-03
func (d Date) String() string
String returns the Date formatted as an RFC3339 full-date string (i.e., 2006-01-02).
func (d Date) ToTime() time.Time
ToTime returns a Date as a time.Time
func (d *Date) UnmarshalBinary(data []byte) error
UnmarshalBinary reconstitutes a Date saved as a byte array conforming to RFC3339 full-date (i.e.,
2006-01-02).
▾ Example
Code:
d := Date{}
t := "2001-02-03"
if err := d.UnmarshalBinary([]byte(t)); err != nil {
fmt.Println(err)
}
fmt.Println(d)
Output:
2001-02-03
func (d *Date) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON reconstitutes the Date from a JSON string conforming to RFC3339 full-date (i.e.,
2006-01-02).
▾ Example
Code:
var d struct {
Date Date `json:"date"`
}
j := `{"date" : "2001-02-03"}`
if err := json.Unmarshal([]byte(j), &d); err != nil {
fmt.Println(err)
}
fmt.Println(d.Date)
Output:
2001-02-03
func (d *Date) UnmarshalText(data []byte) (err error)
UnmarshalText reconstitutes a Date saved as a byte array conforming to RFC3339 full-date (i.e.,
2006-01-02).
▾ Example
Code:
d := Date{}
t := "2001-02-03"
if err := d.UnmarshalText([]byte(t)); err != nil {
fmt.Println(err)
}
fmt.Println(d)
Output:
2001-02-03
Time defines a type similar to time.Time but assumes a layout of RFC3339 date-time (i.e.,
2006-01-02T15:04:05Z).
type Time struct {
time.Time
}
func (t Time) MarshalBinary() ([]byte, error)
MarshalBinary preserves the Time as a byte array conforming to RFC3339 date-time (i.e.,
2006-01-02T15:04:05Z).
▾ Example
Code:
ti, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z")
if err != nil {
fmt.Println(err)
}
d := Time{ti}
t, err := d.MarshalBinary()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(t))
Output:
2001-02-03T04:05:06Z
func (t Time) MarshalJSON() (json []byte, err error)
MarshalJSON preserves the Time as a JSON string conforming to RFC3339 date-time (i.e.,
2006-01-02T15:04:05Z).
▾ Example
Code:
d, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z")
if err != nil {
fmt.Println(err)
}
j, err := json.Marshal(d)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(j))
Output:
"2001-02-03T04:05:06Z"
func (t Time) MarshalText() (text []byte, err error)
MarshalText preserves the Time as a byte array conforming to RFC3339 date-time (i.e.,
2006-01-02T15:04:05Z).
▾ Example
Code:
d, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z")
if err != nil {
fmt.Println(err)
}
t, err := d.MarshalText()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(t))
Output:
2001-02-03T04:05:06Z
func (t Time) String() string
String returns the Time formatted as an RFC3339 date-time string (i.e.,
2006-01-02T15:04:05Z).
func (t Time) ToTime() time.Time
ToTime returns a Time as a time.Time
func (t *Time) UnmarshalBinary(data []byte) error
UnmarshalBinary reconstitutes a Time saved as a byte array conforming to RFC3339 date-time
(i.e., 2006-01-02T15:04:05Z).
▾ Example
Code:
d := Time{}
t := "2001-02-03T04:05:06Z"
if err := d.UnmarshalBinary([]byte(t)); err != nil {
fmt.Println(err)
}
fmt.Println(d)
Output:
2001-02-03T04:05:06Z
func (t *Time) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON reconstitutes the Time from a JSON string conforming to RFC3339 date-time
(i.e., 2006-01-02T15:04:05Z).
▾ Example
Code:
var d struct {
Time Time `json:"datetime"`
}
j := `{"datetime" : "2001-02-03T04:05:06Z"}`
if err := json.Unmarshal([]byte(j), &d); err != nil {
fmt.Println(err)
}
fmt.Println(d.Time)
Output:
2001-02-03T04:05:06Z
func (t *Time) UnmarshalText(data []byte) (err error)
UnmarshalText reconstitutes a Time saved as a byte array conforming to RFC3339 date-time
(i.e., 2006-01-02T15:04:05Z).
▾ Example
Code:
d := Time{}
t := "2001-02-03T04:05:06Z"
if err := d.UnmarshalText([]byte(t)); err != nil {
fmt.Println(err)
}
fmt.Println(d)
Output:
2001-02-03T04:05:06Z
TimeRFC1123 defines a type similar to time.Time but assumes a layout of RFC1123 date-time (i.e.,
Mon, 02 Jan 2006 15:04:05 MST).
type TimeRFC1123 struct {
time.Time
}
▾ Example
Code:
d, err := ParseTime(rfc1123, "Mon, 02 Jan 2006 15:04:05 MST")
if err != nil {
fmt.Println(err)
}
fmt.Println(d)
Output:
2006-01-02 15:04:05 +0000 MST
func (t TimeRFC1123) MarshalBinary() ([]byte, error)
MarshalBinary preserves the Time as a byte array conforming to RFC1123 date-time (i.e.,
Mon, 02 Jan 2006 15:04:05 MST).
▾ Example
Code:
ti, err := ParseTime(rfc1123, "Mon, 02 Jan 2006 15:04:05 MST")
if err != nil {
fmt.Println(err)
}
d := TimeRFC1123{ti}
b, err := d.MarshalBinary()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(b))
Output:
Mon, 02 Jan 2006 15:04:05 MST
func (t TimeRFC1123) MarshalJSON() ([]byte, error)
MarshalJSON preserves the Time as a JSON string conforming to RFC1123 date-time (i.e.,
Mon, 02 Jan 2006 15:04:05 MST).
▾ Example
Code:
ti, err := ParseTime(rfc1123, "Mon, 02 Jan 2006 15:04:05 MST")
if err != nil {
fmt.Println(err)
}
d := TimeRFC1123{ti}
j, err := json.Marshal(d)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(j))
Output:
"Mon, 02 Jan 2006 15:04:05 MST"
func (t TimeRFC1123) MarshalText() ([]byte, error)
MarshalText preserves the Time as a byte array conforming to RFC1123 date-time (i.e.,
Mon, 02 Jan 2006 15:04:05 MST).
▾ Example
Code:
ti, err := ParseTime(rfc3339, "2001-02-03T04:05:06Z")
if err != nil {
fmt.Println(err)
}
d := TimeRFC1123{ti}
t, err := d.MarshalText()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(t))
Output:
Sat, 03 Feb 2001 04:05:06 UTC
func (TimeRFC1123) String
¶
func (t TimeRFC1123) String() string
String returns the Time formatted as an RFC1123 date-time string (i.e.,
Mon, 02 Jan 2006 15:04:05 MST).
func (TimeRFC1123) ToTime
¶
func (t TimeRFC1123) ToTime() time.Time
ToTime returns a Time as a time.Time
func (t *TimeRFC1123) UnmarshalBinary(data []byte) error
UnmarshalBinary reconstitutes a Time saved as a byte array conforming to RFC1123 date-time
(i.e., Mon, 02 Jan 2006 15:04:05 MST).
▾ Example
Code:
d := TimeRFC1123{}
t := "Mon, 02 Jan 2006 15:04:05 MST"
if err := d.UnmarshalBinary([]byte(t)); err != nil {
fmt.Println(err)
}
fmt.Println(d)
Output:
Mon, 02 Jan 2006 15:04:05 MST
func (t *TimeRFC1123) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON reconstitutes the Time from a JSON string conforming to RFC1123 date-time
(i.e., Mon, 02 Jan 2006 15:04:05 MST).
▾ Example
Code:
var d struct {
Time TimeRFC1123 `json:"datetime"`
}
j := `{"datetime" : "Mon, 02 Jan 2006 15:04:05 MST"}`
if err := json.Unmarshal([]byte(j), &d); err != nil {
fmt.Println(err)
}
fmt.Println(d.Time)
Output:
Mon, 02 Jan 2006 15:04:05 MST
func (t *TimeRFC1123) UnmarshalText(data []byte) (err error)
UnmarshalText reconstitutes a Time saved as a byte array conforming to RFC1123 date-time
(i.e., Mon, 02 Jan 2006 15:04:05 MST).
▾ Example
Code:
d := TimeRFC1123{}
t := "Sat, 03 Feb 2001 04:05:06 UTC"
if err := d.UnmarshalText([]byte(t)); err != nil {
fmt.Println(err)
}
fmt.Println(d)
Output:
Sat, 03 Feb 2001 04:05:06 UTC
UnixTime marshals and unmarshals a time that is represented as the number
of seconds (ignoring skip-seconds) since the Unix Epoch.
type UnixTime time.Time
func NewUnixTimeFromDuration(dur time.Duration) UnixTime
NewUnixTimeFromDuration creates a UnixTime as a duration of time since the UnixEpoch.
func NewUnixTimeFromNanoseconds(nanoseconds int64) UnixTime
NewUnixTimeFromNanoseconds creates a UnixTime as a number of nanoseconds from the UnixEpoch.
func NewUnixTimeFromSeconds(seconds float64) UnixTime
NewUnixTimeFromSeconds creates a UnixTime as a number of seconds from the UnixEpoch.
func (t UnixTime) Duration() time.Duration
Duration returns the time as a Duration since the UnixEpoch.
func (t UnixTime) MarshalBinary() ([]byte, error)
MarshalBinary converts a UnixTime into a binary.LittleEndian float64 of nanoseconds since the epoch.
func (t UnixTime) MarshalJSON() ([]byte, error)
MarshalJSON preserves the UnixTime as a JSON number conforming to Unix Timestamp requirements.
(i.e. the number of seconds since midnight January 1st, 1970 not considering leap seconds.)
▾ Example
Code:
epoch := UnixTime(UnixEpoch())
text, _ := json.Marshal(epoch)
fmt.Print(string(text))
Output:
0
func (t UnixTime) MarshalText() ([]byte, error)
MarshalText stores the number of seconds since the Unix Epoch as a textual floating point number.
func (t *UnixTime) UnmarshalBinary(raw []byte) error
UnmarshalBinary converts a from a binary.LittleEndian float64 of nanoseconds since the epoch into a UnixTime.
func (t *UnixTime) UnmarshalJSON(text []byte) error
UnmarshalJSON reconstitures a UnixTime saved as a JSON number of the number of seconds since
midnight January 1st, 1970.
▾ Example
Code:
var myTime UnixTime
json.Unmarshal([]byte("1.3e2"), &myTime)
fmt.Printf("%v", time.Time(myTime))
Output:
1970-01-01 00:02:10 +0000 UTC
func (t *UnixTime) UnmarshalText(raw []byte) error
UnmarshalText populates a UnixTime with a value stored textually as a floating point number of seconds since the Unix Epoch.