~ogham/doom

8462ce6a2cb3bb729a5b0042fc9e2e313719850f — Benjamin Sago 1 year, 10 months ago 3068fd6
Dates from SQL
1 files changed, 18 insertions(+), 6 deletions(-)

M mtime/date.go
M mtime/date.go => mtime/date.go +18 -6
@@ 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())
}