in lib/time/time.go [234:300]
func (d Duration) Binary(op syntax.Token, y starlark.Value, side starlark.Side) (starlark.Value, error) {
x := time.Duration(d)
switch op {
case syntax.PLUS:
switch y := y.(type) {
case Duration:
return Duration(x + time.Duration(y)), nil
case Time:
return Time(time.Time(y).Add(x)), nil
}
case syntax.MINUS:
switch y := y.(type) {
case Duration:
return Duration(x - time.Duration(y)), nil
}
case syntax.SLASH:
switch y := y.(type) {
case Duration:
if y == 0 {
return nil, fmt.Errorf("%s division by zero", d.Type())
}
return starlark.Float(x.Nanoseconds()) / starlark.Float(time.Duration(y).Nanoseconds()), nil
case starlark.Int:
if side == starlark.Right {
return nil, fmt.Errorf("unsupported operation")
}
i, ok := y.Int64()
if !ok {
return nil, fmt.Errorf("int value out of range (want signed 64-bit value)")
}
if i == 0 {
return nil, fmt.Errorf("%s division by zero", d.Type())
}
return d / Duration(i), nil
case starlark.Float:
f := float64(y)
if f == 0 {
return nil, fmt.Errorf("%s division by zero", d.Type())
}
return Duration(float64(x.Nanoseconds()) / f), nil
}
case syntax.SLASHSLASH:
switch y := y.(type) {
case Duration:
if y == 0 {
return nil, fmt.Errorf("%s division by zero", d.Type())
}
return starlark.MakeInt64(x.Nanoseconds() / time.Duration(y).Nanoseconds()), nil
}
case syntax.STAR:
switch y := y.(type) {
case starlark.Int:
i, ok := y.Int64()
if !ok {
return nil, fmt.Errorf("int value out of range (want signed 64-bit value)")
}
return d * Duration(i), nil
}
}
return nil, nil
}