2015-03-08 18:57:18 -04:00
|
|
|
package storage
|
2015-02-15 20:03:27 -05:00
|
|
|
|
|
|
|
import (
|
2015-03-15 22:40:30 -04:00
|
|
|
"errors"
|
2015-02-15 20:03:27 -05:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-03-05 22:41:28 -05:00
|
|
|
// Date - [0000-00-00]
|
2015-02-15 20:03:27 -05:00
|
|
|
type Date struct {
|
|
|
|
Year int16
|
|
|
|
Month byte
|
|
|
|
Day byte
|
|
|
|
}
|
|
|
|
|
2015-03-05 22:41:28 -05:00
|
|
|
// String output in yyyy-mm-dd format
|
2015-02-15 20:03:27 -05:00
|
|
|
func (d Date) String() string {
|
|
|
|
return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day)
|
|
|
|
}
|
|
|
|
|
2015-03-05 22:41:28 -05:00
|
|
|
// IsZero true if date is 0000-00-00
|
2015-02-15 20:03:27 -05:00
|
|
|
func (d Date) IsZero() bool {
|
|
|
|
return d.Day == 0 && d.Month == 0 && d.Year == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert string date in format YYYY-MM-DD to Date.
|
|
|
|
// Leading and trailing spaces are ignored. If format is invalid returns zero.
|
2015-02-17 16:23:13 -05:00
|
|
|
func parseDate(str string) (d Date, err error) {
|
2015-02-15 20:03:27 -05:00
|
|
|
str = strings.TrimSpace(str)
|
|
|
|
if str == "0000-00-00" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
y, m, n int
|
|
|
|
)
|
|
|
|
if len(str) != 10 || str[4] != '-' || str[7] != '-' {
|
2015-03-15 22:40:30 -04:00
|
|
|
err = errors.New("Invalid 0000-00-000 style DATE string: " + str)
|
2015-02-15 20:03:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if y, err = strconv.Atoi(str[0:4]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if m, err = strconv.Atoi(str[5:7]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if m < 1 || m > 12 {
|
2015-03-15 22:40:30 -04:00
|
|
|
err = errors.New("Invalid 0000-00-000 style DATE string: " + str)
|
2015-02-15 20:03:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if n, err = strconv.Atoi(str[8:10]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n < 1 || n > 31 {
|
2015-03-15 22:40:30 -04:00
|
|
|
err = errors.New("Invalid 0000-00-000 style DATE string: " + str)
|
2015-02-15 20:03:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
d.Year = int16(y)
|
|
|
|
d.Month = byte(m)
|
|
|
|
d.Day = byte(n)
|
|
|
|
return
|
|
|
|
}
|