github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/lang/funcs/datetime.go (about)

     1  package funcs
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/zclconf/go-cty/cty"
     7  	"github.com/zclconf/go-cty/cty/function"
     8  )
     9  
    10  // TimestampFunc constructs a function that returns a string representation of the current date and time.
    11  var TimestampFunc = function.New(&function.Spec{
    12  	Params: []function.Parameter{},
    13  	Type:   function.StaticReturnType(cty.String),
    14  	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
    15  		return cty.StringVal(time.Now().UTC().Format(time.RFC3339)), nil
    16  	},
    17  })
    18  
    19  // TimeAddFunc constructs a function that adds a duration to a timestamp, returning a new timestamp.
    20  var TimeAddFunc = function.New(&function.Spec{
    21  	Params: []function.Parameter{
    22  		{
    23  			Name: "timestamp",
    24  			Type: cty.String,
    25  		},
    26  		{
    27  			Name: "duration",
    28  			Type: cty.String,
    29  		},
    30  	},
    31  	Type: function.StaticReturnType(cty.String),
    32  	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
    33  		ts, err := time.Parse(time.RFC3339, args[0].AsString())
    34  		if err != nil {
    35  			return cty.UnknownVal(cty.String), err
    36  		}
    37  		duration, err := time.ParseDuration(args[1].AsString())
    38  		if err != nil {
    39  			return cty.UnknownVal(cty.String), err
    40  		}
    41  
    42  		return cty.StringVal(ts.Add(duration).Format(time.RFC3339)), nil
    43  	},
    44  })
    45  
    46  // Timestamp returns a string representation of the current date and time.
    47  //
    48  // In the Terraform language, timestamps are conventionally represented as
    49  // strings using RFC 3339 "Date and Time format" syntax, and so timestamp
    50  // returns a string in this format.
    51  func Timestamp() (cty.Value, error) {
    52  	return TimestampFunc.Call([]cty.Value{})
    53  }
    54  
    55  // TimeAdd adds a duration to a timestamp, returning a new timestamp.
    56  //
    57  // In the Terraform language, timestamps are conventionally represented as
    58  // strings using RFC 3339 "Date and Time format" syntax. Timeadd requires
    59  // the timestamp argument to be a string conforming to this syntax.
    60  //
    61  // `duration` is a string representation of a time difference, consisting of
    62  // sequences of number and unit pairs, like `"1.5h"` or `1h30m`. The accepted
    63  // units are `ns`, `us` (or `µs`), `"ms"`, `"s"`, `"m"`, and `"h"`. The first
    64  // number may be negative to indicate a negative duration, like `"-2h5m"`.
    65  //
    66  // The result is a string, also in RFC 3339 format, representing the result
    67  // of adding the given direction to the given timestamp.
    68  func TimeAdd(timestamp cty.Value, duration cty.Value) (cty.Value, error) {
    69  	return TimeAddFunc.Call([]cty.Value{timestamp, duration})
    70  }