12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package main
- import (
- "fmt"
- "strings"
- "time"
- "github.com/beanzilla/gonode"
- )
- type Date struct {
- N *gonode.Node `json:"-"`
- Day int `json:"day"`
- Month int `json:"month"`
- Year int `json:"year"`
- }
- func (d *Date) String() string {
- month := time.Month(d.Month)
- return fmt.Sprintf("%s %d, %d", month.String(), d.Day, d.Year)
- }
- func (d *Date) Parse(n *gonode.Node) error {
- txt := fmt.Sprintf("%v", n.Data())
- if !strings.HasPrefix(txt, "date") {
- return fmt.Errorf("node doesn't contain correct information, expected 'date ## ## ####' got '%s'", txt)
- }
- parts := strings.Split(txt, " ")
- if len(parts) < 4 {
- return fmt.Errorf("expected no less than 4 parts, got %d %v", len(parts), parts)
- }
- // 'date' parts[0] (Ignore this)
- // day parts[1]
- if !IsInt(parts[1]) {
- return fmt.Errorf("day is invalid, '%s'", parts[1])
- }
- d.Day = ValueInt(parts[1])
- // month parts[2]
- if !IsInt(parts[2]) {
- return fmt.Errorf("month is invalid, '%s'", parts[2])
- }
- d.Month = ValueInt(parts[2])
- // year parts[3]
- if !IsInt(parts[3]) {
- return fmt.Errorf("year is invalid, '%s'", parts[3])
- }
- d.Year = ValueInt(parts[3])
- d.N = n
- return nil
- }
- func (d *Date) Update() error {
- if d.N == nil {
- return fmt.Errorf("no node associated, can't update")
- }
- d.N.SetData(fmt.Sprintf("date %d %d %d", d.Day, d.Month, d.Year))
- return nil
- }
|