-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcasts.go
43 lines (37 loc) · 851 Bytes
/
casts.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
package goxpath
import (
"fmt"
"time"
)
var (
// ErrConversion is returned in case of an unsuccessful cast.
ErrConversion = fmt.Errorf("conversion failed")
)
// ToXSInteger converts the item to an xs:integer.
func ToXSInteger(itm Item) (int, error) {
switch t := itm.(type) {
case float64:
return int(t), nil
case int:
return t, nil
default:
panic(fmt.Sprintf("nyi xs:integer %T", itm))
}
}
func xsTime(ctx *Context, args []Sequence) (Sequence, error) {
firstarg, err := StringValue(args[0])
if err != nil {
return nil, err
}
t, err := time.Parse("15:04:05-07:00", firstarg)
if err != nil {
t, err = time.Parse("15:04:05", firstarg)
if err != nil {
return nil, err
}
}
return Sequence{XSTime(t)}, nil
}
func init() {
RegisterFunction(&Function{Name: "time", Namespace: nsXS, F: xsTime, MinArg: 1, MaxArg: 1})
}