package pflag

import (
	
	
	
)

// TimeValue adapts time.Time for use as a flag.
type timeValue struct {
	*time.Time
	formats []string
}

func newTimeValue( time.Time,  *time.Time,  []string) *timeValue {
	* = 
	return &timeValue{
		Time:    ,
		formats: ,
	}
}

// Set time.Time value from string based on accepted formats.
func ( *timeValue) ( string) error {
	 = strings.TrimSpace()
	for ,  := range .formats {
		,  := time.Parse(, )
		if  != nil {
			continue
		}
		*.Time = 
		return nil
	}

	 := ""
	for ,  := range .formats {
		if  > 0 {
			 += ", "
		}
		 += fmt.Sprintf("`%s`", )
	}

	return fmt.Errorf("invalid time format `%s` must be one of: %s", , )
}

// Type name for time.Time flags.
func ( *timeValue) () string {
	return "time"
}

func ( *timeValue) () string {
	if .Time.IsZero() {
		return ""
	}
	return .Time.Format(time.RFC3339Nano)
}

// GetTime return the time value of a flag with the given name
func ( *FlagSet) ( string) (time.Time, error) {
	 := .Lookup()
	if  == nil {
		 := fmt.Errorf("flag accessed but not defined: %s", )
		return time.Time{}, 
	}

	if .Value.Type() != "time" {
		 := fmt.Errorf("trying to get %s value of flag of type %s", "time", .Value.Type())
		return time.Time{}, 
	}

	,  := .Value.(*timeValue)
	if ! {
		return time.Time{}, fmt.Errorf("value %s is not a time", .Value)
	}

	return *.Time, nil
}

// TimeVar defines a time.Time flag with specified name, default value, and usage string.
// The argument p points to a time.Time variable in which to store the value of the flag.
func ( *FlagSet) ( *time.Time,  string,  time.Time,  []string,  string) {
	.TimeVarP(, , "", , , )
}

// TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash.
func ( *FlagSet) ( *time.Time, ,  string,  time.Time,  []string,  string) {
	.VarP(newTimeValue(, , ), , , )
}

// TimeVar defines a time.Time flag with specified name, default value, and usage string.
// The argument p points to a time.Time variable in which to store the value of the flag.
func ( *time.Time,  string,  time.Time,  []string,  string) {
	CommandLine.TimeVarP(, , "", , , )
}

// TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash.
func ( *time.Time, ,  string,  time.Time,  []string,  string) {
	CommandLine.VarP(newTimeValue(, , ), , , )
}

// Time defines a time.Time flag with specified name, default value, and usage string.
// The return value is the address of a time.Time variable that stores the value of the flag.
func ( *FlagSet) ( string,  time.Time,  []string,  string) *time.Time {
	return .TimeP(, "", , , )
}

// TimeP is like Time, but accepts a shorthand letter that can be used after a single dash.
func ( *FlagSet) (,  string,  time.Time,  []string,  string) *time.Time {
	 := new(time.Time)
	.TimeVarP(, , , , , )
	return 
}

// Time defines a time.Time flag with specified name, default value, and usage string.
// The return value is the address of a time.Time variable that stores the value of the flag.
func ( string,  time.Time,  []string,  string) *time.Time {
	return CommandLine.TimeP(, "", , , )
}

// TimeP is like Time, but accepts a shorthand letter that can be used after a single dash.
func (,  string,  time.Time,  []string,  string) *time.Time {
	return CommandLine.TimeP(, , , , )
}