github.com/crewjam/saml@v0.4.14/time.go (about) 1 package saml 2 3 import "time" 4 5 // RelaxedTime is a version of time.Time that supports the time format 6 // found in SAML documents. 7 type RelaxedTime time.Time 8 9 const timeFormat = "2006-01-02T15:04:05.999Z07:00" 10 11 // MarshalText implements encoding.TextMarshaler 12 func (m RelaxedTime) MarshalText() ([]byte, error) { 13 // According to section 1.2.2 of the OASIS SAML 1.1 spec, we can't trust 14 // other applications to handle time resolution finer than a millisecond. 15 // 16 // The time MUST be expressed in UTC. 17 return []byte(m.String()), nil 18 } 19 20 func (m RelaxedTime) String() string { 21 return time.Time(m).Round(time.Millisecond).UTC().Format(timeFormat) 22 } 23 24 // UnmarshalText implements encoding.TextUnmarshaler 25 func (m *RelaxedTime) UnmarshalText(text []byte) error { 26 if len(text) == 0 { 27 *m = RelaxedTime(time.Time{}) 28 return nil 29 } 30 t, err1 := time.Parse(time.RFC3339, string(text)) 31 if err1 == nil { 32 t = t.Round(time.Millisecond) 33 *m = RelaxedTime(t) 34 return nil 35 } 36 37 t, err2 := time.Parse(time.RFC3339Nano, string(text)) 38 if err2 == nil { 39 t = t.Round(time.Millisecond) 40 *m = RelaxedTime(t) 41 return nil 42 } 43 44 t, err2 = time.Parse("2006-01-02T15:04:05.999999999", string(text)) 45 if err2 == nil { 46 t = t.Round(time.Millisecond) 47 *m = RelaxedTime(t) 48 return nil 49 } 50 51 return err1 52 }