// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. package gconv import ( "time" "github.com/gogf/gf/internal/utils" "github.com/gogf/gf/os/gtime" ) // Time converts to time.Time. func Time(i interface{}, format ...string) time.Time { // It's already this type. if len(format) == 0 { if v, ok := i.(time.Time); ok { return v } } if t := GTime(i, format...); t != nil { return t.Time } return time.Time{} } // Duration converts to time.Duration. // If is string, then it uses time.ParseDuration to convert it. // If is numeric, then it converts as nanoseconds. func Duration(i interface{}) time.Duration { // It's already this type. if v, ok := i.(time.Duration); ok { return v } s := String(i) if !utils.IsNumeric(s) { d, _ := gtime.ParseDuration(s) return d } return time.Duration(Int64(i)) } // GTime converts to *gtime.Time. // The parameter can be used to specify the format of . // If no given, it converts using gtime.NewFromTimeStamp if is numeric, // or using gtime.StrToTime if is string. func GTime(i interface{}, format ...string) *gtime.Time { if i == nil { return nil } // It's already this type. if len(format) == 0 { if v, ok := i.(*gtime.Time); ok { return v } } s := String(i) if len(s) == 0 { return gtime.New() } // Priority conversion using given format. if len(format) > 0 { t, _ := gtime.StrToTimeFormat(s, format[0]) return t } if utils.IsNumeric(s) { return gtime.NewFromTimeStamp(Int64(s)) } else { t, _ := gtime.StrToTime(s) return t } }