MCPcopy Create free account
hub / github.com/auxten/postgresql-parser / addFrac

Function addFrac

pkg/sql/sem/tree/interval.go:630–656  ·  view source on GitHub ↗

addFrac increases the duration given as first argument by the unit given as second argument multiplied by the factor in the third argument. For computing fractions there are 30 days to a month and 24 hours to a day.

(d duration.Duration, unit duration.Duration, f float64)

Source from the content-addressed store, hash-verified

628// argument. For computing fractions there are 30 days to a month and
629// 24 hours to a day.
630func addFrac(d duration.Duration, unit duration.Duration, f float64) (duration.Duration, error) {
631 if unit.Months > 0 {
632 f = f * float64(unit.Months)
633 d.Months += int64(f)
634 switch unit.Months {
635 case 1:
636 f = math.Mod(f, 1) * 30
637 d.Days += int64(f)
638 f = math.Mod(f, 1) * 24
639 d.SetNanos(d.Nanos() + int64(float64(time.Hour.Nanoseconds())*f))
640 case 12:
641 // Nothing to do: Postgres limits the precision of fractional years to
642 // months. Do not continue to add precision to the interval.
643 // See issue #55226 for more details on this.
644 default:
645 return duration.Duration{}, errors.AssertionFailedf("unhandled unit type %v", unit)
646 }
647 } else if unit.Days > 0 {
648 f = f * float64(unit.Days)
649 d.Days += int64(f)
650 f = math.Mod(f, 1) * 24
651 d.SetNanos(d.Nanos() + int64(float64(time.Hour.Nanoseconds())*f))
652 } else {
653 d.SetNanos(d.Nanos() + int64(float64(unit.Nanos())*f))
654 }
655 return d, nil
656}
657
658// floatToNanos converts a fractional number representing nanoseconds to the
659// number of integer nanoseconds. For example: ".354874219" to "354874219"

Callers 1

parseDurationFunction · 0.85

Calls 2

SetNanosMethod · 0.80
NanosMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…