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

Method parseShortDuration

pkg/util/duration/parse.go:574–647  ·  view source on GitHub ↗
(
	h int64, hasSign bool, itm types.IntervalTypeMetadata,
)

Source from the content-addressed store, hash-verified

572}
573
574func (l *intervalLexer) parseShortDuration(
575 h int64, hasSign bool, itm types.IntervalTypeMetadata,
576) (Duration, error) {
577 sign := int64(1)
578 if hasSign {
579 sign = -1
580 }
581 // postgresToDuration() has rewound the cursor to just after the
582 // first number, so that we can check here there are no unwanted
583 // spaces.
584 if l.str[l.offset] != ':' {
585 return Duration{}, pgerror.Newf(
586 pgcode.InvalidDatetimeFormat, "interval: invalid format %s", l.str[l.offset:])
587 }
588 l.offset++
589 // Parse the second number.
590 m, hasDecimal, mp := l.consumeNum()
591
592 if m < 0 {
593 return Duration{}, pgerror.Newf(
594 pgcode.InvalidDatetimeFormat, "interval: invalid format: %s", l.str)
595 }
596 // We have three possible formats:
597 // - MM:SS.ffffff
598 // - HH:MM (or MM:SS for MINUTE TO SECOND)
599 // - HH:MM:SS[.ffffff]
600 //
601 // The top format has the "h" field parsed above actually
602 // represent minutes. Get this out of the way first.
603 if hasDecimal {
604 l.consumeSpaces()
605 return MakeDuration(
606 h*time.Minute.Nanoseconds()+
607 sign*(m*time.Second.Nanoseconds()+
608 floatToNanos(mp)),
609 0,
610 0,
611 ), nil
612 }
613
614 // Remaining formats.
615 var s int64
616 var sp float64
617 hasSecondsComponent := false
618 if l.offset != len(l.str) && l.str[l.offset] == ':' {
619 hasSecondsComponent = true
620 // The last :NN part.
621 l.offset++
622 s, _, sp = l.consumeNum()
623 if s < 0 {
624 return Duration{}, pgerror.Newf(
625 pgcode.InvalidDatetimeFormat, "interval: invalid format: %s", l.str)
626 }
627 }
628
629 l.consumeSpaces()
630
631 if !hasSecondsComponent && itm.DurationField.IsMinuteToSecond() {

Callers 1

parseDurationFunction · 0.95

Calls 6

consumeNumMethod · 0.95
consumeSpacesMethod · 0.95
NewfFunction · 0.92
MakeDurationFunction · 0.85
floatToNanosFunction · 0.85
IsMinuteToSecondMethod · 0.80

Tested by

no test coverage detected