github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_time.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gconv
     8  
     9  import (
    10  	"time"
    11  
    12  	"github.com/gogf/gf/v2/internal/utils"
    13  	"github.com/gogf/gf/v2/os/gtime"
    14  	"github.com/gogf/gf/v2/util/gconv/internal/localinterface"
    15  )
    16  
    17  // Time converts `any` to time.Time.
    18  func Time(any interface{}, format ...string) time.Time {
    19  	// It's already this type.
    20  	if len(format) == 0 {
    21  		if v, ok := any.(time.Time); ok {
    22  			return v
    23  		}
    24  	}
    25  	if t := GTime(any, format...); t != nil {
    26  		return t.Time
    27  	}
    28  	return time.Time{}
    29  }
    30  
    31  // Duration converts `any` to time.Duration.
    32  // If `any` is string, then it uses time.ParseDuration to convert it.
    33  // If `any` is numeric, then it converts `any` as nanoseconds.
    34  func Duration(any interface{}) time.Duration {
    35  	// It's already this type.
    36  	if v, ok := any.(time.Duration); ok {
    37  		return v
    38  	}
    39  	s := String(any)
    40  	if !utils.IsNumeric(s) {
    41  		d, _ := gtime.ParseDuration(s)
    42  		return d
    43  	}
    44  	return time.Duration(Int64(any))
    45  }
    46  
    47  // GTime converts `any` to *gtime.Time.
    48  // The parameter `format` can be used to specify the format of `any`.
    49  // It returns the converted value that matched the first format of the formats slice.
    50  // If no `format` given, it converts `any` using gtime.NewFromTimeStamp if `any` is numeric,
    51  // or using gtime.StrToTime if `any` is string.
    52  func GTime(any interface{}, format ...string) *gtime.Time {
    53  	if any == nil {
    54  		return nil
    55  	}
    56  	if v, ok := any.(localinterface.IGTime); ok {
    57  		return v.GTime(format...)
    58  	}
    59  	// It's already this type.
    60  	if len(format) == 0 {
    61  		if v, ok := any.(*gtime.Time); ok {
    62  			return v
    63  		}
    64  		if t, ok := any.(time.Time); ok {
    65  			return gtime.New(t)
    66  		}
    67  		if t, ok := any.(*time.Time); ok {
    68  			return gtime.New(t)
    69  		}
    70  	}
    71  	s := String(any)
    72  	if len(s) == 0 {
    73  		return gtime.New()
    74  	}
    75  	// Priority conversion using given format.
    76  	if len(format) > 0 {
    77  		for _, item := range format {
    78  			t, err := gtime.StrToTimeFormat(s, item)
    79  			if t != nil && err == nil {
    80  				return t
    81  			}
    82  		}
    83  		return nil
    84  	}
    85  	if utils.IsNumeric(s) {
    86  		return gtime.NewFromTimeStamp(Int64(s))
    87  	} else {
    88  		t, _ := gtime.StrToTime(s)
    89  		return t
    90  	}
    91  }