(ts, tz ref.Val)
| 1042 | } |
| 1043 | |
| 1044 | func inTimeZone(ts, tz ref.Val) (time.Time, error) { |
| 1045 | t := ts.(types.Timestamp) |
| 1046 | val := string(tz.(types.String)) |
| 1047 | ind := strings.Index(val, ":") |
| 1048 | if ind == -1 { |
| 1049 | loc, err := time.LoadLocation(val) |
| 1050 | if err != nil { |
| 1051 | return time.Time{}, err |
| 1052 | } |
| 1053 | return t.In(loc), nil |
| 1054 | } |
| 1055 | |
| 1056 | // If the input is not the name of a timezone (for example, 'US/Central'), it should be a numerical offset from UTC |
| 1057 | // in the format ^(+|-)([01]\d|2[0-3]):[0-5][0-9]$. The numerical input is parsed in terms of hours and minutes. |
| 1058 | hr, err := strconv.Atoi(string(val[0:ind])) |
| 1059 | if err != nil { |
| 1060 | return time.Time{}, err |
| 1061 | } |
| 1062 | min, err := strconv.Atoi(string(val[ind+1:])) |
| 1063 | if err != nil { |
| 1064 | return time.Time{}, err |
| 1065 | } |
| 1066 | if hr < -23 || hr > 23 { |
| 1067 | return time.Time{}, fmt.Errorf("timezone offset hours out of range [-23, 23]: %s", val) |
| 1068 | } |
| 1069 | if min < 0 || min > 59 { |
| 1070 | return time.Time{}, fmt.Errorf("timezone offset minutes out of range [0, 59]: %s", val) |
| 1071 | } |
| 1072 | var offset int |
| 1073 | if string(val[0]) == "-" { |
| 1074 | offset = hr*60 - min |
| 1075 | } else { |
| 1076 | offset = hr*60 + min |
| 1077 | } |
| 1078 | secondsEastOfUTC := int((time.Duration(offset) * time.Minute).Seconds()) |
| 1079 | timezone := time.FixedZone("", secondsEastOfUTC) |
| 1080 | return t.In(timezone), nil |
| 1081 | } |
no test coverage detected