MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / truncateInterval

Function truncateInterval

pkg/sql/sem/tree/datum.go:3157–3178  ·  view source on GitHub ↗

truncateInterval truncates the input interval downward to the nearest interval quantity specified by the DurationField input. If precision is set for seconds, this will instead round at the second layer.

(d *duration.Duration, itm types.IntervalTypeMetadata)

Source from the content-addressed store, hash-verified

3155// interval quantity specified by the DurationField input.
3156// If precision is set for seconds, this will instead round at the second layer.
3157func truncateInterval(d *duration.Duration, itm types.IntervalTypeMetadata) {
3158 switch itm.DurationField.DurationType {
3159 case types.IntervalDurationType_YEAR:
3160 d.Months = d.Months - d.Months%12
3161 d.Days = 0
3162 d.SetNanos(0)
3163 case types.IntervalDurationType_MONTH:
3164 d.Days = 0
3165 d.SetNanos(0)
3166 case types.IntervalDurationType_DAY:
3167 d.SetNanos(0)
3168 case types.IntervalDurationType_HOUR:
3169 d.SetNanos(d.Nanos() - d.Nanos()%time.Hour.Nanoseconds())
3170 case types.IntervalDurationType_MINUTE:
3171 d.SetNanos(d.Nanos() - d.Nanos()%time.Minute.Nanoseconds())
3172 case types.IntervalDurationType_SECOND, types.IntervalDurationType_UNSET:
3173 if itm.PrecisionIsSet || itm.Precision > 0 {
3174 prec := TimeFamilyPrecisionToRoundDuration(itm.Precision)
3175 d.SetNanos(time.Duration(d.Nanos()).Round(prec).Nanoseconds())
3176 }
3177 }
3178}
3179
3180// ParseDIntervalWithTypeMetadata is like ParseDInterval, but it also takes a
3181// types.IntervalTypeMetadata that both specifies the units for unitless, numeric intervals

Callers 2

NewDIntervalFunction · 0.85

Calls 5

SetNanosMethod · 0.80
NanosMethod · 0.80
DurationMethod · 0.80
RoundMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…