summaryrefslogtreecommitdiff
path: root/srv/src/post/date.go
blob: 34fe109e182b8413eaf1b746065bb2ccdd1fe414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package post

import (
	"database/sql/driver"
	"fmt"
	"time"
)

// Date represents a calendar date with no timezone information attached.
type Date struct {
	Year  int
	Month time.Month
	Day   int
}

// DateFromTime converts a Time into a Date, truncating all non-date
// information.
func DateFromTime(t time.Time) Date {
	t = t.UTC()
	return Date{
		Year:  t.Year(),
		Month: t.Month(),
		Day:   t.Day(),
	}
}

// ToTime converts a Date into a Time. The returned time will be UTC midnight of
// the Date.
func (d *Date) ToTime() time.Time {
	return time.Date(d.Year, d.Month, d.Day, 0, 0, 0, 0, time.UTC)
}

// Scan implements the sql.Scanner interface.
func (d *Date) Scan(src interface{}) error {

	if src == nil {
		*d = Date{}
		return nil
	}

	ts, ok := src.(int64)

	if !ok {
		return fmt.Errorf("cannot scan value %#v into Date", src)
	}

	*d = DateFromTime(time.Unix(ts, 0))
	return nil
}

// Value implements the driver.Valuer interface.
func (d Date) Value() (driver.Value, error) {

	if d == (Date{}) {
		return nil, nil
	}

	return d.ToTime().Unix(), nil
}