// Copyright © 2014 Steve Francia <spf@spf13.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package cast

import (
	
	
	
	
	

	
)

// ToTimeE any value to a [time.Time] type.
func ( any) (time.Time, error) {
	return ToTimeInDefaultLocationE(, time.UTC)
}

// ToTimeInDefaultLocationE casts an empty interface to [time.Time],
// interpreting inputs without a timezone to be in the given location,
// or the local timezone if nil.
func ( any,  *time.Location) ( time.Time,  error) {
	, _ = indirect()

	switch v := .(type) {
	case time.Time:
		return , nil
	case string:
		return StringToDateInDefaultLocation(, )
	case json.Number:
		// Originally this used ToInt64E, but adding string float conversion broke ToTime.
		// the behavior of ToTime would have changed if we continued using it.
		// For now, using json.Number's own Int64 method should be good enough to preserve backwards compatibility.
		 = json.Number(trimZeroDecimal(string()))
		,  := .Int64()
		if  != nil {
			return time.Time{}, fmt.Errorf(errorMsg, , , time.Time{})
		}
		return time.Unix(, 0), nil
	case int:
		return time.Unix(int64(), 0), nil
	case int32:
		return time.Unix(int64(), 0), nil
	case int64:
		return time.Unix(, 0), nil
	case uint:
		return time.Unix(int64(), 0), nil
	case uint32:
		return time.Unix(int64(), 0), nil
	case uint64:
		return time.Unix(int64(), 0), nil
	case nil:
		return time.Time{}, nil
	default:
		return time.Time{}, fmt.Errorf(errorMsg, , , time.Time{})
	}
}

// ToDurationE casts any value to a [time.Duration] type.
func ( any) (time.Duration, error) {
	, _ = indirect()

	switch s := .(type) {
	case time.Duration:
		return , nil
	case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
		,  := ToInt64E()
		if  != nil {
			// TODO: once there is better error handling, this should be easier
			return 0, errors.New(strings.ReplaceAll(.Error(), " int64", "time.Duration"))
		}

		return time.Duration(), nil
	case float32, float64, float64EProvider, float64Provider:
		,  := ToFloat64E()
		if  != nil {
			// TODO: once there is better error handling, this should be easier
			return 0, errors.New(strings.ReplaceAll(.Error(), " float64", "time.Duration"))
		}

		return time.Duration(), nil
	case string:
		if !strings.ContainsAny(, "nsuµmh") {
			return time.ParseDuration( + "ns")
		}

		return time.ParseDuration()
	case nil:
		return time.Duration(0), nil
	default:
		if ,  := resolveAlias();  {
			return ()
		}

		return 0, fmt.Errorf(errorMsg, , , time.Duration(0))
	}
}

// StringToDate attempts to parse a string into a [time.Time] type using a
// predefined list of formats.
//
// If no suitable format is found, an error is returned.
func ( string) (time.Time, error) {
	return internal.ParseDateWith(, time.UTC, internal.TimeFormats)
}

// StringToDateInDefaultLocation casts an empty interface to a [time.Time],
// interpreting inputs without a timezone to be in the given location,
// or the local timezone if nil.
func ( string,  *time.Location) (time.Time, error) {
	return internal.ParseDateWith(, , internal.TimeFormats)
}