github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/scanners/terraform/parser/funcs/datetime.go (about)

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