github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf.
     6  
     7  package gconv
     8  
     9  import (
    10  	"time"
    11  
    12  	"github.com/wangyougui/gf/v2/internal/utils"
    13  	"github.com/wangyougui/gf/v2/os/gtime"
    14  )
    15  
    16  // Time converts `any` to time.Time.
    17  func Time(any interface{}, format ...string) time.Time {
    18  	// It's already this type.
    19  	if len(format) == 0 {
    20  		if v, ok := any.(time.Time); ok {
    21  			return v
    22  		}
    23  	}
    24  	if t := GTime(any, format...); t != nil {
    25  		return t.Time
    26  	}
    27  	return time.Time{}
    28  }
    29  
    30  // Duration converts `any` to time.Duration.
    31  // If `any` is string, then it uses time.ParseDuration to convert it.
    32  // If `any` is numeric, then it converts `any` as nanoseconds.
    33  func Duration(any interface{}) time.Duration {
    34  	// It's already this type.
    35  	if v, ok := any.(time.Duration); ok {
    36  		return v
    37  	}
    38  	s := String(any)
    39  	if !utils.IsNumeric(s) {
    40  		d, _ := gtime.ParseDuration(s)
    41  		return d
    42  	}
    43  	return time.Duration(Int64(any))
    44  }
    45  
    46  // GTime converts `any` to *gtime.Time.
    47  // The parameter `format` can be used to specify the format of `any`.
    48  // It returns the converted value that matched the first format of the formats slice.
    49  // If no `format` given, it converts `any` using gtime.NewFromTimeStamp if `any` is numeric,
    50  // or using gtime.StrToTime if `any` is string.
    51  func GTime(any interface{}, format ...string) *gtime.Time {
    52  	if any == nil {
    53  		return nil
    54  	}
    55  	if v, ok := any.(iGTime); ok {
    56  		return v.GTime(format...)
    57  	}
    58  	// It's already this type.
    59  	if len(format) == 0 {
    60  		if v, ok := any.(*gtime.Time); ok {
    61  			return v
    62  		}
    63  		if t, ok := any.(time.Time); ok {
    64  			return gtime.New(t)
    65  		}
    66  		if t, ok := any.(*time.Time); ok {
    67  			return gtime.New(t)
    68  		}
    69  	}
    70  	s := String(any)
    71  	if len(s) == 0 {
    72  		return gtime.New()
    73  	}
    74  	// Priority conversion using given format.
    75  	if len(format) > 0 {
    76  		for _, item := range format {
    77  			t, err := gtime.StrToTimeFormat(s, item)
    78  			if t != nil && err == nil {
    79  				return t
    80  			}
    81  		}
    82  		return nil
    83  	}
    84  	if utils.IsNumeric(s) {
    85  		return gtime.NewFromTimeStamp(Int64(s))
    86  	} else {
    87  		t, _ := gtime.StrToTime(s)
    88  		return t
    89  	}
    90  }