Documentation ¶
Overview ¶
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.
Index ¶
- func ParseTime(format string, t string) (d time.Time, err error)
- type Date
- 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)
Examples ¶
- Date
- Date.MarshalBinary
- Date.MarshalJSON
- Date.MarshalText
- Date.UnmarshalBinary
- Date.UnmarshalJSON
- Date.UnmarshalText
- ParseDate
- ParseTime
- Time.MarshalBinary
- Time.MarshalJSON
- Time.MarshalText
- Time.UnmarshalBinary
- Time.UnmarshalJSON
- Time.UnmarshalText
- TimeRfc1123.MarshalBinary
- TimeRfc1123.MarshalJSON
- TimeRfc1123.MarshalText
- TimeRfc1123.UnmarshalBinary
- TimeRfc1123.UnmarshalJSON
- TimeRfc1123.UnmarshalText
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Date ¶
Date defines a type similar to time.Time but assumes a layout of RFC3339 full-date (i.e., 2006-01-02).
Example ¶
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) } // Date acts as time.Time when the receiver if d.Before(t) { fmt.Printf("Before ") } else { fmt.Printf("After ") } // Convert Date when needing a time.Time if t.After(d.ToTime()) { fmt.Printf("After") } else { fmt.Printf("Before") }
Output: Before After
func ParseDate ¶
ParseDate create a new Date from the passed string.
Example ¶
d, err := ParseDate("2001-02-03") if err != nil { fmt.Println(err) } fmt.Println(d)
Output: 2001-02-03
func (Date) MarshalBinary ¶
MarshalBinary preserves the Date as a byte array conforming to RFC3339 full-date (i.e., 2006-01-02).
Example ¶
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 (Date) MarshalJSON ¶
MarshalJSON preserves the Date as a JSON string conforming to RFC3339 full-date (i.e., 2006-01-02).
Example ¶
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 (Date) MarshalText ¶
MarshalText preserves the Date as a byte array conforming to RFC3339 full-date (i.e., 2006-01-02).
Example ¶
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 (Date) String ¶
String returns the Date formatted as an RFC3339 full-date string (i.e., 2006-01-02).
func (*Date) UnmarshalBinary ¶
UnmarshalBinary reconstitutes a Date saved as a byte array conforming to RFC3339 full-date (i.e., 2006-01-02).
Example ¶
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 (*Date) UnmarshalJSON ¶
UnmarshalJSON reconstitutes the Date from a JSON string conforming to RFC3339 full-date (i.e., 2006-01-02).
Example ¶
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 (*Date) UnmarshalText ¶
UnmarshalText reconstitutes a Date saved as a byte array conforming to RFC3339 full-date (i.e., 2006-01-02).
Example ¶
d := Date{} t := "2001-02-03" if err := d.UnmarshalText([]byte(t)); err != nil { fmt.Println(err) } fmt.Println(d)
Output: 2001-02-03
type Time ¶
Time defines a type similar to time.Time but assumes a layout of RFC3339 date-time (i.e., 2006-01-02T15:04:05Z).
func (Time) MarshalBinary ¶
MarshalBinary preserves the Time as a byte array conforming to RFC3339 date-time (i.e., 2006-01-02T15:04:05Z).
Example ¶
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 (Time) MarshalJSON ¶
MarshalJSON preserves the Time as a JSON string conforming to RFC3339 date-time (i.e., 2006-01-02T15:04:05Z).
Example ¶
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 (Time) MarshalText ¶
MarshalText preserves the Time as a byte array conforming to RFC3339 date-time (i.e., 2006-01-02T15:04:05Z).
Example ¶
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 (Time) String ¶
String returns the Time formatted as an RFC3339 date-time string (i.e., 2006-01-02T15:04:05Z).
func (*Time) UnmarshalBinary ¶
UnmarshalBinary reconstitutes a Time saved as a byte array conforming to RFC3339 date-time (i.e., 2006-01-02T15:04:05Z).
Example ¶
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 (*Time) UnmarshalJSON ¶
UnmarshalJSON reconstitutes the Time from a JSON string conforming to RFC3339 date-time (i.e., 2006-01-02T15:04:05Z).
Example ¶
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 (*Time) UnmarshalText ¶
UnmarshalText reconstitutes a Time saved as a byte array conforming to RFC3339 date-time (i.e., 2006-01-02T15:04:05Z).
Example ¶
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
type TimeRfc1123 ¶
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).
func (TimeRfc1123) MarshalBinary ¶
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 ¶
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 (TimeRfc1123) MarshalJSON ¶
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 ¶
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 (TimeRfc1123) MarshalText ¶
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 ¶
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 (*TimeRfc1123) UnmarshalBinary ¶
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 ¶
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 (*TimeRfc1123) UnmarshalJSON ¶
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 ¶
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 (*TimeRfc1123) UnmarshalText ¶
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 ¶
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