aboutsummaryrefslogtreecommitdiff
path: root/page/time.go
blob: cd419c8c5437394bc004d1e3088d8d60efb43ccc (plain) (blame)
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
package page

import (
	"fmt"
	"time"

	"gopkg.in/yaml.v3"
)

// PageTime allows us to slip in a different time format for loading/unloading
// the yaml from disk than the default.
type PageTime struct {
	time.Time
}

// TimeFormat is the format string used when unmarshaling the time
// from the yaml information.
var TimeFormat = "01.02.2006 15:04:05 MST"

// UnmarshalYAML override the default and parse our time with the global time
// format.
func (pt *PageTime) UnmarshalYAML(n *yaml.Node) error {
	t, err := time.Parse(TimeFormat, n.Value)
	if err != nil {
		return fmt.Errorf("pagetime: %w", err)
	}

	pt.Time = t

	return nil
}

// MarshalYAML override the default with our own time format.
func (pt PageTime) MarshalYAML() (interface{}, error) {
	s := pt.Time.Format(TimeFormat)

	return s, nil
}