Pri*_*Raj 1 validation timezone go
前端在注册期间发送时区以及其他用户详细信息。我需要在时区上放置一个验证器进行 api 测试。时区中的数据格式为:
(GMT-10:00) Hawaii
(GMT-08:00) Pacific Time (US & Canada)
Run Code Online (Sandbox Code Playgroud)
我所做的是在数组中定义所有时区,然后搜索接收到的时区。如果它存在那么确定否则返回错误。我的功能是:
func timeZoneValidator(field validator.FieldLevel) bool {
if field.Field().Kind() != reflect.String {
return false
}
timeZoneField := field.Field().String()
for i:= range timeZones {
if timeZones[i] == timeZoneField {
// Found!
return true
}
}
return false
}
Run Code Online (Sandbox Code Playgroud)
但我想使用图书馆或为我完成这项任务的东西。有的话请指教。
您也许可以使用time.LoadLocation.
func LoadLocation(name string) (*Location, error)
Run Code Online (Sandbox Code Playgroud)
您可以传入一个字符串,例如“America/New_York”,错误返回应该告诉您它是否有效。
请注意,格式将与您当前拥有的不同。查看https://www.iana.org/time-zones
| 归档时间: |
|
| 查看次数: |
819 次 |
| 最近记录: |