ParseInterval parses the given interval in the given style.
( style IntervalStyle, s string, itm types.IntervalTypeMetadata, )
| 1050 | |
| 1051 | // ParseInterval parses the given interval in the given style. |
| 1052 | func ParseInterval( |
| 1053 | style IntervalStyle, s string, itm types.IntervalTypeMetadata, |
| 1054 | ) (Duration, error) { |
| 1055 | // At this time the only supported interval formats are: |
| 1056 | // - SQL standard. |
| 1057 | // - Postgres compatible. |
| 1058 | // - iso8601 format (with designators only), see interval.go for |
| 1059 | // sources of documentation. |
| 1060 | // - Golang time.parseDuration compatible. |
| 1061 | |
| 1062 | // If it's a blank string, exit early. |
| 1063 | if len(s) == 0 { |
| 1064 | return Duration{}, makeParseError(s, types.Interval, nil) |
| 1065 | } |
| 1066 | if s[0] == 'P' { |
| 1067 | // If it has a leading P we're most likely working with an iso8601 |
| 1068 | // interval. |
| 1069 | dur, err := iso8601ToDuration(s) |
| 1070 | if err != nil { |
| 1071 | return Duration{}, makeParseError(s, types.Interval, err) |
| 1072 | } |
| 1073 | return dur, nil |
| 1074 | } |
| 1075 | if strings.IndexFunc(s, unicode.IsLetter) == -1 { |
| 1076 | // If it has no letter, then we're most likely working with a SQL standard |
| 1077 | // interval, as both postgres and golang have letter(s) and iso8601 has been tested. |
| 1078 | dur, err := sqlStdToDuration(s, itm) |
| 1079 | if err != nil { |
| 1080 | return Duration{}, makeParseError(s, types.Interval, err) |
| 1081 | } |
| 1082 | return dur, nil |
| 1083 | } |
| 1084 | |
| 1085 | // We're either a postgres string or a Go duration. |
| 1086 | // Our postgres syntax parser also supports golang, so just use that for both. |
| 1087 | dur, err := parseDuration(style, s, itm) |
| 1088 | if err != nil { |
| 1089 | return Duration{}, makeParseError(s, types.Interval, err) |
| 1090 | } |
| 1091 | return dur, nil |
| 1092 | } |
| 1093 | |
| 1094 | func makeParseError(s string, typ *types.T, err error) error { |
| 1095 | if err != nil { |
no test coverage detected
searching dependent graphs…