MCPcopy Create free account
hub / github.com/cel-expr/cel-go / inTimeZone

Function inTimeZone

common/stdlib/standard.go:1044–1081  ·  view source on GitHub ↗
(ts, tz ref.Val)

Source from the content-addressed store, hash-verified

1042}
1043
1044func 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}

Callers 10

timestampGetFullYearFunction · 0.85
timestampGetMonthFunction · 0.85
timestampGetDayOfYearFunction · 0.85
timestampGetDayOfWeekFunction · 0.85
timestampGetHoursFunction · 0.85
timestampGetMinutesFunction · 0.85
timestampGetSecondsFunction · 0.85
timestampGetMillisecondsFunction · 0.85

Calls 1

IndexMethod · 0.80

Tested by

no test coverage detected