Data and Time Function
Terraform has a set of functions that work with date and time. It allows you to perform quite a lot of operations and formatting with the timestamps.
Types of Data and Time Functions
Terraform provides the following types of Data and Time functions:
1. plantimestamp Function
The plantimestamp function is a Terraform function that returns a UTC (Coordinated Universal Time) timestamp string in the RFC 3339 format. The syntax is
plantimestamp()
The return value of the plantimestamp function will be different every time terraform plan is run. It is intended to be used from within Custom Conditions to enable validation of resources which have a time component, such as validation of whether or not a TLS certificate is still valid.
NOTE: The plantimestamp function is not available for usage inside the Terraform console. It can only be utilized within Terraform configurations.
For example
terraform console
> plantimestamp()
"2018-05-13T07:44:12Z"
2. timestamp Function
The timestamp function returns the UTC timestamp string in RFC 3339 format. This format is generally used internally within the Terraform language to provide time-related information. Syntax:
timestamp()
However, this function's result changes every second and will most likely cause a detected difference every time Terraform runs. One should therefore not use this function directly with resource attributes since it will always provide a difference on every execution of Terraform.
For example
terraform console
> timestamp()
"2024-08-17T06:48:24Z"
3. timeadd Function
The timeadd function in Terraform is used to add a duration to a timestamp, returning a new timestamp. The syntax for the timeadd function is
timeadd(timestamp, duration)
Here is a breakdown of how the function works:
timestamp
: The input timestamp should be a string, and its format should be conformed to the syntax of RFC 3339 "Date and Time format". The RFC 3339 format is the standard way of representing timestamps within the Terraform language.
duration
: A string representation of a time difference, consisting of sequences of number and unit pairs, such as "1.5h" or "1h30m". The supported units are ns, us (or µs), ms, s, m, h.
For example, if you have a timestamp of "2018-05-13T07:44:12Z" and you want to add 2 hours and 30 minutes to it, you would use the following expression:
terraform console
> timeadd("2018-05-13T07:44:12Z", "2h30m")
"2018-05-13T10:14:12Z"
4. timecmp Function
The timecmp function is used in Terraform to compare two timestamps. It returns an integer number representing the ordering of instants those timestamps represent. The syntax of the timecmp function is
timecmp(timestamp_a, timestamp_b)
timestamp_a and timestamp_b are two timestamps to be compared. Both must be in RFC 3339 "Date and Time format" syntax, a conventional way of representing timestamps in Terraform. The function returns
- If timestamp_a is before timestamp_b, the function returns -1
- If timestamp_a represents the same instant as timestamp_b, the function returns 0.
- This function returns 1 if timestamp_a is after timestamp_b.
For example
terraform console
> timecmp("2022-01-01T12:00:00Z","2022-01-01T13:00:00Z")
-1
5. formatdate Function
The formatdate function in Terraform converts a timestamp from one format to another. The syntax for using the file function is
formatdate(spec, timestamp)
Here is a breakdown of how the function works:
spec
: A string representing the format of the timestamp output that should be returned.
timestamp
: The input timestamp should be a string, and its format should be conformed to the syntax of RFC 3339 "Date and Time format". The RFC 3339 format is the standard way of representing timestamps within the Terraform language.
The formatdate function takes an input timestamp and converts it into the format specified by the spec argument. The argument spec specifies the output format using a format string in a way very similar to many programming languages' strftime function.
For example, if you have a timestamp in the format "2023-04-24T12:34:56Z", you want it to transform into "April 24, 2023"; then you could use a function formatdate as follows:
terraform console
> formatdate("DD MMM YYYY", "2023-04-24T12:34:56Z")
"24 Apr 2023"
Related Pages
Feedback
Was this page helpful?