package pflag
import (
"fmt"
"strings"
"time"
)
type timeValue struct {
*time .Time
formats []string
}
func newTimeValue(val time .Time , p *time .Time , formats []string ) *timeValue {
*p = val
return &timeValue {
Time : p ,
formats : formats ,
}
}
func (d *timeValue ) Set (s string ) error {
s = strings .TrimSpace (s )
for _ , f := range d .formats {
v , err := time .Parse (f , s )
if err != nil {
continue
}
*d .Time = v
return nil
}
formatsString := ""
for i , f := range d .formats {
if i > 0 {
formatsString += ", "
}
formatsString += fmt .Sprintf ("`%s`" , f )
}
return fmt .Errorf ("invalid time format `%s` must be one of: %s" , s , formatsString )
}
func (d *timeValue ) Type () string {
return "time"
}
func (d *timeValue ) String () string {
if d .Time .IsZero () {
return ""
}
return d .Time .Format (time .RFC3339Nano )
}
func (f *FlagSet ) GetTime (name string ) (time .Time , error ) {
flag := f .Lookup (name )
if flag == nil {
err := fmt .Errorf ("flag accessed but not defined: %s" , name )
return time .Time {}, err
}
if flag .Value .Type () != "time" {
err := fmt .Errorf ("trying to get %s value of flag of type %s" , "time" , flag .Value .Type ())
return time .Time {}, err
}
val , ok := flag .Value .(*timeValue )
if !ok {
return time .Time {}, fmt .Errorf ("value %s is not a time" , flag .Value )
}
return *val .Time , nil
}
func (f *FlagSet ) TimeVar (p *time .Time , name string , value time .Time , formats []string , usage string ) {
f .TimeVarP (p , name , "" , value , formats , usage )
}
func (f *FlagSet ) TimeVarP (p *time .Time , name , shorthand string , value time .Time , formats []string , usage string ) {
f .VarP (newTimeValue (value , p , formats ), name , shorthand , usage )
}
func TimeVar (p *time .Time , name string , value time .Time , formats []string , usage string ) {
CommandLine .TimeVarP (p , name , "" , value , formats , usage )
}
func TimeVarP (p *time .Time , name , shorthand string , value time .Time , formats []string , usage string ) {
CommandLine .VarP (newTimeValue (value , p , formats ), name , shorthand , usage )
}
func (f *FlagSet ) Time (name string , value time .Time , formats []string , usage string ) *time .Time {
return f .TimeP (name , "" , value , formats , usage )
}
func (f *FlagSet ) TimeP (name , shorthand string , value time .Time , formats []string , usage string ) *time .Time {
p := new (time .Time )
f .TimeVarP (p , name , shorthand , value , formats , usage )
return p
}
func Time (name string , value time .Time , formats []string , usage string ) *time .Time {
return CommandLine .TimeP (name , "" , value , formats , usage )
}
func TimeP (name , shorthand string , value time .Time , formats []string , usage string ) *time .Time {
return CommandLine .TimeP (name , shorthand , value , formats , usage )
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .