ParseTimeTZ parses and returns the TimeTZ represented by the provided string, or an error if parsing is unsuccessful.
(now time.Time, s string, precision time.Duration)
| 83 | // ParseTimeTZ parses and returns the TimeTZ represented by the |
| 84 | // provided string, or an error if parsing is unsuccessful. |
| 85 | func ParseTimeTZ(now time.Time, s string, precision time.Duration) (TimeTZ, error) { |
| 86 | // Special case as we have to use `ParseTimestamp` to get the date. |
| 87 | // We cannot use `ParseTime` as it does not have timezone awareness. |
| 88 | if !timeTZHasTimeComponent.MatchString(s) { |
| 89 | return TimeTZ{}, pgerror.Newf( |
| 90 | pgcode.InvalidTextRepresentation, |
| 91 | "could not parse %q as TimeTZ", |
| 92 | s, |
| 93 | ) |
| 94 | } |
| 95 | |
| 96 | // ParseTimestamp requires a date field -- append date at the beginning |
| 97 | // if a date has not been included. |
| 98 | if !timeTZIncludesDateRegex.MatchString(s) { |
| 99 | s = "1970-01-01 " + s |
| 100 | } else { |
| 101 | s = timeutil.ReplaceLibPQTimePrefix(s) |
| 102 | } |
| 103 | |
| 104 | t, err := pgdate.ParseTimestamp(now, pgdate.ParseModeYMD, s) |
| 105 | if err != nil { |
| 106 | // Build our own error message to avoid exposing the dummy date. |
| 107 | return TimeTZ{}, pgerror.Newf( |
| 108 | pgcode.InvalidTextRepresentation, |
| 109 | "could not parse %q as TimeTZ", |
| 110 | s, |
| 111 | ) |
| 112 | } |
| 113 | retTime := timeofday.FromTime(t.Round(precision)) |
| 114 | // Special case on 24:00 and 24:00:00 as the parser |
| 115 | // does not handle these correctly. |
| 116 | if timeTZMaxTimeRegex.MatchString(s) { |
| 117 | retTime = timeofday.Time2400 |
| 118 | } |
| 119 | |
| 120 | _, offsetSecsUnconverted := t.Zone() |
| 121 | offsetSecs := int32(-offsetSecsUnconverted) |
| 122 | if offsetSecs > MaxTimeTZOffsetSecs || offsetSecs < MinTimeTZOffsetSecs { |
| 123 | return TimeTZ{}, pgerror.Newf( |
| 124 | pgcode.NumericValueOutOfRange, |
| 125 | "time zone displacement out of range: %q", |
| 126 | s, |
| 127 | ) |
| 128 | } |
| 129 | return MakeTimeTZ(retTime, offsetSecs), nil |
| 130 | } |
| 131 | |
| 132 | // String implements the Stringer interface. |
| 133 | func (t *TimeTZ) String() string { |
no test coverage detected
searching dependent graphs…