2013-10-08 22:00:49 +00:00
|
|
|
package soap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-10-09 18:43:33 +00:00
|
|
|
"regexp"
|
2013-10-08 22:00:49 +00:00
|
|
|
"strconv"
|
2013-10-09 21:19:01 +00:00
|
|
|
"strings"
|
2013-10-08 22:00:49 +00:00
|
|
|
"time"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
2013-10-09 21:23:57 +00:00
|
|
|
var (
|
|
|
|
// localLoc acts like time.Local for this package, but is faked out by the
|
|
|
|
// unit tests to ensure that things stay constant (especially when running
|
|
|
|
// this test in a place where local time is UTC which might mask bugs).
|
|
|
|
localLoc = time.Local
|
|
|
|
)
|
|
|
|
|
2013-10-09 20:39:55 +00:00
|
|
|
// MarshalFixed14_4 marshals float64 to SOAP "fixed.14.4" type.
|
2013-10-08 22:00:49 +00:00
|
|
|
func MarshalFixed14_4(v float64) (string, error) {
|
|
|
|
if v >= 1e14 || v <= -1e14 {
|
|
|
|
return "", fmt.Errorf("soap fixed14.4: value %v out of bounds", v)
|
|
|
|
}
|
|
|
|
return strconv.FormatFloat(v, 'f', 4, 64), nil
|
|
|
|
}
|
|
|
|
|
2013-10-09 20:39:55 +00:00
|
|
|
// UnmarshalFixed14_4 unmarshals float64 from SOAP "fixed.14.4" type.
|
2013-10-08 22:00:49 +00:00
|
|
|
func UnmarshalFixed14_4(s string) (float64, error) {
|
|
|
|
v, err := strconv.ParseFloat(s, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if v >= 1e14 || v <= -1e14 {
|
|
|
|
return 0, fmt.Errorf("soap fixed14.4: value %q out of bounds", s)
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2013-10-09 20:39:55 +00:00
|
|
|
// MarshalChar marshals rune to SOAP "char" type.
|
2013-10-08 22:00:49 +00:00
|
|
|
func MarshalChar(v rune) (string, error) {
|
|
|
|
if v == 0 {
|
|
|
|
return "", errors.New("soap char: rune 0 is not allowed")
|
|
|
|
}
|
|
|
|
return string(v), nil
|
|
|
|
}
|
|
|
|
|
2013-10-09 20:39:55 +00:00
|
|
|
// UnmarshalChar unmarshals rune from SOAP "char" type.
|
2013-10-08 22:00:49 +00:00
|
|
|
func UnmarshalChar(s string) (rune, error) {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return 0, errors.New("soap char: got empty string")
|
|
|
|
}
|
|
|
|
r, n := utf8.DecodeRune([]byte(s))
|
|
|
|
if n != len(s) {
|
|
|
|
return 0, fmt.Errorf("soap char: value %q is not a single rune", s)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2013-10-09 20:38:41 +00:00
|
|
|
// MarshalDate marshals time.Time to SOAP "date" type. Note that this converts
|
|
|
|
// to local time, and discards the time-of-day components.
|
2013-10-08 22:00:49 +00:00
|
|
|
func MarshalDate(v time.Time) (string, error) {
|
2013-10-09 21:23:57 +00:00
|
|
|
return v.In(localLoc).Format("2006-01-02"), nil
|
2013-10-08 22:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var dateFmts = []string{"2006-01-02", "20060102"}
|
|
|
|
|
2013-10-09 20:38:41 +00:00
|
|
|
// UnmarshalDate unmarshals time.Time from SOAP "date" type. This outputs the
|
|
|
|
// date as midnight in the local time zone.
|
2013-10-08 22:00:49 +00:00
|
|
|
func UnmarshalDate(s string) (time.Time, error) {
|
|
|
|
for _, f := range dateFmts {
|
2013-10-09 21:23:57 +00:00
|
|
|
if t, err := time.ParseInLocation(f, s, localLoc); err == nil {
|
2013-10-08 22:00:49 +00:00
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Time{}, fmt.Errorf("soap date: value %q is not in a recognized date format", s)
|
|
|
|
}
|
2013-10-09 18:43:33 +00:00
|
|
|
|
|
|
|
// TimeOfDay is used in cases where SOAP "time" or "time.tz" is used.
|
|
|
|
type TimeOfDay struct {
|
|
|
|
// Duration of time since midnight.
|
|
|
|
FromMidnight time.Duration
|
|
|
|
|
2013-10-09 20:35:09 +00:00
|
|
|
// Set to true if Offset is specified. If false, then the timezone is
|
|
|
|
// unspecified (and by ISO8601 - implies some "local" time).
|
|
|
|
HasOffset bool
|
|
|
|
|
|
|
|
// Offset is non-zero only if time.tz is used. It is otherwise ignored. If
|
|
|
|
// non-zero, then it is regarded as a UTC offset in seconds. Note that the
|
|
|
|
// sub-minutes is ignored by the marshal function.
|
|
|
|
Offset int16
|
2013-10-09 18:43:33 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 20:35:09 +00:00
|
|
|
// MarshalTimeOfDay marshals TimeOfDay to the "time" type.
|
2013-10-09 18:43:33 +00:00
|
|
|
func MarshalTimeOfDay(v TimeOfDay) (string, error) {
|
|
|
|
d := int64(v.FromMidnight / time.Second)
|
|
|
|
hour := d / 3600
|
|
|
|
d = d % 3600
|
|
|
|
minute := d / 60
|
|
|
|
second := d % 60
|
|
|
|
|
|
|
|
return fmt.Sprintf("%02d:%02d:%02d", hour, minute, second), nil
|
|
|
|
}
|
|
|
|
|
2013-10-09 20:35:09 +00:00
|
|
|
// UnmarshalTimeOfDay unmarshals TimeOfDay from the "time" type.
|
2013-10-09 18:43:33 +00:00
|
|
|
func UnmarshalTimeOfDay(s string) (TimeOfDay, error) {
|
2013-10-09 20:35:09 +00:00
|
|
|
t, err := UnmarshalTimeOfDayTz(s)
|
|
|
|
if err != nil {
|
|
|
|
return TimeOfDay{}, err
|
|
|
|
} else if t.HasOffset {
|
|
|
|
return TimeOfDay{}, fmt.Errorf("soap time: value %q contains unexpected timezone")
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalTimeOfDayTz marshals TimeOfDay to the "time.tz" type.
|
|
|
|
func MarshalTimeOfDayTz(v TimeOfDay) (string, error) {
|
|
|
|
d := int64(v.FromMidnight / time.Second)
|
|
|
|
hour := d / 3600
|
|
|
|
d = d % 3600
|
|
|
|
minute := d / 60
|
|
|
|
second := d % 60
|
|
|
|
|
|
|
|
tz := ""
|
|
|
|
if v.HasOffset {
|
|
|
|
if v.Offset == 0 {
|
|
|
|
tz = "Z"
|
|
|
|
} else {
|
|
|
|
offsetMins := v.Offset / 60
|
|
|
|
sign := '+'
|
|
|
|
if offsetMins < 1 {
|
|
|
|
offsetMins = -offsetMins
|
|
|
|
sign = '-'
|
|
|
|
}
|
|
|
|
tz = fmt.Sprintf("%c%02d:%02d", sign, offsetMins/60, offsetMins%60)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%02d:%02d:%02d%s", hour, minute, second, tz), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var timeRegexp = regexp.MustCompile(
|
|
|
|
`^(\d\d)(?::?(\d\d)(?::?(\d\d))?)?` + // hh[:mm[:ss]]
|
|
|
|
`(?:(Z)|([+-])(\d\d)(?::?(\d\d))?)?$`) // Z | ±hh[:mm]
|
|
|
|
|
|
|
|
// UnmarshalTimeOfDayTz unmarshals TimeOfDay from the "time.tz" type.
|
|
|
|
func UnmarshalTimeOfDayTz(s string) (TimeOfDay, error) {
|
2013-10-09 18:43:33 +00:00
|
|
|
parts := timeRegexp.FindStringSubmatch(s)
|
2013-10-09 20:35:09 +00:00
|
|
|
if parts == nil {
|
|
|
|
return TimeOfDay{}, fmt.Errorf("soap time.tz: value %q is not in ISO8601 time format", s)
|
2013-10-09 18:43:33 +00:00
|
|
|
}
|
2013-10-09 20:35:09 +00:00
|
|
|
|
|
|
|
// HH:MM:SS parsing.
|
2013-10-09 18:43:33 +00:00
|
|
|
parts = parts[1:]
|
|
|
|
var iParts [3]int64
|
2013-10-09 20:35:09 +00:00
|
|
|
for i, pStr := range parts[:3] {
|
2013-10-09 18:43:33 +00:00
|
|
|
iParts[i], _ = strconv.ParseInt(pStr, 10, 64)
|
|
|
|
}
|
2013-10-09 20:35:09 +00:00
|
|
|
hour, minute, second := iParts[0], iParts[1], iParts[2]
|
|
|
|
|
|
|
|
fromMidnight := time.Duration(hour*3600+minute*60+second) * time.Second
|
|
|
|
|
|
|
|
// ISO8601 special case - values up to 24:00:00 are allowed, so using
|
|
|
|
// strictly greater-than for the maximum value.
|
|
|
|
if fromMidnight > 24*time.Hour || minute >= 60 || second >= 60 {
|
|
|
|
return TimeOfDay{}, fmt.Errorf("soap time.tz: value %q has value(s) out of range", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timezone offset parsing.
|
|
|
|
hasOffset := false
|
|
|
|
var offset int64
|
|
|
|
if parts[3] == "Z" {
|
|
|
|
hasOffset = true
|
|
|
|
offset = 0
|
|
|
|
} else if parts[4] != "" {
|
|
|
|
hasOffset = true
|
|
|
|
hours, _ := strconv.ParseInt(parts[5], 10, 64)
|
|
|
|
var mins int64
|
|
|
|
if parts[6] != "" {
|
|
|
|
mins, _ = strconv.ParseInt(parts[6], 10, 64)
|
|
|
|
}
|
|
|
|
offset = hours*3600 + mins*60
|
|
|
|
if parts[4] == "-" {
|
|
|
|
offset = -offset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TimeOfDay{
|
|
|
|
FromMidnight: time.Duration(hour*3600+minute*60+second) * time.Second,
|
|
|
|
HasOffset: hasOffset,
|
|
|
|
Offset: int16(offset),
|
|
|
|
}, nil
|
2013-10-09 18:43:33 +00:00
|
|
|
}
|
2013-10-09 21:19:01 +00:00
|
|
|
|
|
|
|
// MarshalDatetime marshals time.Time to SOAP "date" type. Note that this
|
|
|
|
// converts to local time.
|
|
|
|
func MarshalDatetime(v time.Time) (string, error) {
|
2013-10-09 21:23:57 +00:00
|
|
|
return v.In(localLoc).Format("2006-01-02T15:04:05"), nil
|
2013-10-09 21:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalDatetime unmarshals time.Time from the SOAP "dateTime" type. This
|
|
|
|
// returns a value in the local timezone.
|
|
|
|
func UnmarshalDatetime(s string) (time.Time, error) {
|
|
|
|
parts := strings.SplitN(s, "T", 2)
|
|
|
|
datePart, err := UnmarshalDate(parts[0])
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
if len(parts) == 2 {
|
|
|
|
timePart, err := UnmarshalTimeOfDay(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
datePart = datePart.Add(timePart.FromMidnight)
|
|
|
|
}
|
|
|
|
return datePart, nil
|
|
|
|
}
|