cosmossdk.io/client/v2@v2.0.0-beta.1/autocli/flag/timestamp.go (about)

     1  package flag
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"google.golang.org/protobuf/reflect/protoreflect"
     8  	"google.golang.org/protobuf/types/known/timestamppb"
     9  )
    10  
    11  type timestampType struct{}
    12  
    13  func (t timestampType) NewValue(context.Context, *Builder) Value {
    14  	return &timestampValue{}
    15  }
    16  
    17  func (t timestampType) DefaultValue() string {
    18  	return ""
    19  }
    20  
    21  type timestampValue struct {
    22  	value *timestamppb.Timestamp
    23  }
    24  
    25  func (t timestampValue) Get(protoreflect.Value) (protoreflect.Value, error) {
    26  	if t.value == nil {
    27  		return protoreflect.Value{}, nil
    28  	}
    29  	return protoreflect.ValueOfMessage(t.value.ProtoReflect()), nil
    30  }
    31  
    32  func (t timestampValue) String() string {
    33  	if t.value == nil {
    34  		return ""
    35  	}
    36  	return t.value.AsTime().Format(time.RFC3339)
    37  }
    38  
    39  func (t *timestampValue) Set(s string) error {
    40  	time, err := time.Parse(time.RFC3339, s)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	t.value = timestamppb.New(time)
    45  	return nil
    46  }
    47  
    48  func (t timestampValue) Type() string {
    49  	return "timestamp (RFC 3339)"
    50  }