Source

xmlm / iso8601.go

package xmlm

import (
	"time"
	"strconv"
)

const ISO8601 = "2006-01-02T15:04:05.000Z07:00"

//ISO8601time is here both as an an example and because I needed it (and the time package 
//doesn't seem to like 1000ths of seconds) 
type ISO8601time struct {
	time.Time
	thousandths int
}
func (o ISO8601time) Value() (string, bool) {return o.String(), true}
func (o *ISO8601time) Parse(xml string) bool {
	if len(xml) < 24 {
		return false
	}
	b := []byte(xml)
	o.thousandths, _ = strconv.Atoi(string(b[20:23]))
	copy(b[20:23], []byte("000"))
	if temp, err := time.Parse(ISO8601, string(b)); err == nil {
		o.Time = *temp
		return true
	}
	return false
}
func (o *ISO8601time) String() string {
	b := []byte(o.Time.Format(ISO8601))
	thou := strconv.Itoa(o.thousandths)
	copy(b[23-len(thou):23], []byte(thou))
	return string(b)
}
func (o *ISO8601time) Nanoseconds() int64 {
	return int64((&o.Time).Seconds()) * 1000 * 1000 * 1000 + int64(o.thousandths) * 1000 * 1000
}