@@ 1,6 1,7 @@
package mtime
import (
+ "errors"
"fmt"
"html/template"
"strings"
@@ 39,6 40,7 @@ type Date struct {
Day int
}
+// Fill values in from JSON.
func (date *Date) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
t, err := time.Parse("2006-01-02", s)
@@ 50,14 52,24 @@ func (date *Date) UnmarshalJSON(b []byte) error {
return nil
}
+// Fill values in from a database.
+func (date *Date) Scan(value interface{}) error {
+ if vtime, ok := value.(time.Time); ok {
+ date.FillFromTime(vtime)
+ return nil
+ } else {
+ return errors.New("expected time.Time")
+ }
+}
+
// ToTime converts the date to a Go time.Time, at midnight UTC.
-func (date Date) ToTime() time.Time {
+func (date *Date) ToTime() time.Time {
return time.Date(date.Year, date.Month, date.Day, 0, 0, 0, 0, time.UTC)
}
// ToTextShort converts the date to a snippet of text, optionally including the
// year if it’s the current year.
-func (date Date) ToTextShort() string {
+func (date *Date) ToTextShort() string {
s := fmt.Sprintf("%d%s %s",
date.Day,
Ordinal(date.Day),
@@ 71,7 83,7 @@ func (date Date) ToTextShort() string {
}
// ToTextFull converts the date to a snippet of text, always including the year.
-func (date Date) ToTextFull() string {
+func (date *Date) ToTextFull() string {
s := fmt.Sprintf("%d%s %s",
date.Day,
Ordinal(date.Day),
@@ 84,7 96,7 @@ func (date Date) ToTextFull() string {
// ToHtmlShort converts the date to a snippet of HTML with the ordinal in
// superscript, optionally including the year if it’s the current year.
-func (date Date) ToHtmlShort() template.HTML {
+func (date *Date) ToHtmlShort() template.HTML {
s := fmt.Sprintf("%d<sup>%s</sup> %s",
date.Day,
Ordinal(date.Day),
@@ 99,7 111,7 @@ func (date Date) ToHtmlShort() template.HTML {
// ToHtmlFull converts the date to a snippet of HTML with the ordinal in
// superscript, always including the year.
-func (date Date) ToHtmlFull() template.HTML {
+func (date *Date) ToHtmlFull() template.HTML {
s := fmt.Sprintf("%d<sup>%s</sup> %s",
date.Day,
Ordinal(date.Day),
@@ 111,7 123,7 @@ func (date Date) ToHtmlFull() template.HTML {
}
// ToIso formats this date as an ISO string.
-func (date Date) ToIso() string {
+func (date *Date) ToIso() string {
return IsoDatestamp(date.ToTime())
}