-
Notifications
You must be signed in to change notification settings - Fork 441
/
time.go
52 lines (43 loc) · 1.25 KB
/
time.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package saml
import "time"
// RelaxedTime is a version of time.Time that supports the time format
// found in SAML documents.
type RelaxedTime time.Time
const timeFormat = "2006-01-02T15:04:05.999Z07:00"
// MarshalText implements encoding.TextMarshaler
func (m RelaxedTime) MarshalText() ([]byte, error) {
// According to section 1.2.2 of the OASIS SAML 1.1 spec, we can't trust
// other applications to handle time resolution finer than a millisecond.
//
// The time MUST be expressed in UTC.
return []byte(m.String()), nil
}
func (m RelaxedTime) String() string {
return time.Time(m).Round(time.Millisecond).UTC().Format(timeFormat)
}
// UnmarshalText implements encoding.TextUnmarshaler
func (m *RelaxedTime) UnmarshalText(text []byte) error {
if len(text) == 0 {
*m = RelaxedTime(time.Time{})
return nil
}
t, err1 := time.Parse(time.RFC3339, string(text))
if err1 == nil {
t = t.Round(time.Millisecond)
*m = RelaxedTime(t)
return nil
}
t, err2 := time.Parse(time.RFC3339Nano, string(text))
if err2 == nil {
t = t.Round(time.Millisecond)
*m = RelaxedTime(t)
return nil
}
t, err2 = time.Parse("2006-01-02T15:04:05.999999999", string(text))
if err2 == nil {
t = t.Round(time.Millisecond)
*m = RelaxedTime(t)
return nil
}
return err1
}