-
Notifications
You must be signed in to change notification settings - Fork 6
/
datetime_span.go
66 lines (56 loc) · 1.45 KB
/
datetime_span.go
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
60
61
62
63
64
65
66
package control
import (
"github.com/goradd/goradd/pkg/config"
"github.com/goradd/goradd/pkg/page"
"bytes"
"context"
"github.com/goradd/goradd/pkg/datetime"
"time"
)
// DateTimeSpan is a span that displays a datetime value as static text. This is a typical default control to use
// for a timestamp in the database.
type DateTimeSpan struct {
Span
format string
value datetime.DateTime
}
func NewDateTimeSpan(parent page.ControlI, id string) *DateTimeSpan {
s := new(DateTimeSpan)
s.Init(s, parent, id)
return s
}
func (s *DateTimeSpan) Init(self page.ControlI, parent page.ControlI, id string) {
s.Span.Init(self, parent, id)
s.format = config.DefaultDateTimeFormat
}
func (s *DateTimeSpan) SetValue(v interface{}) {
switch v2 := v.(type) {
case datetime.DateTime:
s.SetDateTime(v2)
case time.Time:
s.SetDateTime(datetime.NewDateTime(v2))
case string:
d,err := datetime.Parse(s.format, v2)
if err != nil {
panic(err)
}
s.SetDateTime(d)
}
}
func (s *DateTimeSpan) SetDateTime(d datetime.DateTime){
s.value = d
s.Refresh()
}
func (s *DateTimeSpan) Value() datetime.DateTime {
return s.value
}
func (s *DateTimeSpan) SetFormat(format string) *DateTimeSpan {
s.format = format
s.Refresh()
return s
}
func (s *DateTimeSpan) DrawInnerHtml(ctx context.Context, buf *bytes.Buffer) error {
buf.WriteString(s.value.Format(s.format))
// TODO: Internationalize this. Golang does not currently have date/time localization support
return nil
}