-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Function to return the unix timestamp form of a given timestamp #31751
Comments
Hi @rwblokzijl! Thanks for sharing this feature request. The implementation of Someone recently requested the same thing with the intention of using it in Packer, over in zclconf/go-cty#130. As you can see in that issue, my decision was that Unix timestamps are outside of In that issue I proposed that downstream applications which need Unix timestamps should offer separate functions tailored to that purpose, separately from Now putting my Terraform maintainer hat 🎩 back on: I think the Terraform team here must decide first if it agrees with my upstream decision; the Terraform team could potentially fork As some background context to help inform that decision, we originally selected an RFC3339-based syntax as Terraform's canonical convention for representing timestamps because it seemed to strike a good balance between being compact and being human-readable for inclusion in plan output. Unix timestamps are more compact but most humans cannot easily understand what wallclock time a particular timestamp represents without the help of tooling. Our position so far has therefore been that providers designed for use with Terraform should be built to accept and produce RFC3339, even if the upstream system they represent uses a different format. This then allows Terraform providers and built-in functions to compose together well, without explicit format conversions. However, any convention like this runs into the same challenge: not everything a Terraform configuration is integrating with has a first-class Terraform provider, and in such cases the module itself must implement the translation between Terraform's conventions and the external system's conventions. This seems like a variation of that situation: there is a Terraform provider here that could in principle adapt the timestamp format, but the provider here is just passing through a JSON payload verbatim without actually interpreting it, and the recipient of that JSON payload isn't part of Terraform and has its own conventions. It does therefore seem necessary to generate a Unix timestamp here despite our usual compromise/convention of using RFC3339 for readability. When considering this we should also take into account whether this function seems like a good candidate for being a provider-contributed function, once there is a mechanism for providers to bring their own functions (#31225). That could either mean a function specifically for generating Unix timestamps as described above, or it could mean something more specialized for (The Packer team has a similar feature request at hashicorp/packer#11945. We should communicate with them to try to make the same decision in both products unless we have a strong reason for them to differ.) |
Thank you @apparentlymart for taking the time to provide such a detailed answer. I appreciate the context and different perspectives. Wearing the only hat I have, my user cap 🧢, I would like to share my perspective: While I do think the Terraform is the main tool we have for dealing with these issues. It is the glue that connects a wide range of APIs and providers of varying design philosophies and quality. When terraform does not provide proper tools to deal with an issue, we're stuck with hacky solutions, waiting for github issues at providers, or just not being able to solve the problem at all. If I allow myself to dream, I imagine terraform with the possibility to write and import user defined functions. I think a functional extention to terraform would work very well with its declarative nature. If neccessary seperating pure and impure functions (though i think that should be the users responsibility). Overall I love terraform. I think its declarative nature is a beautiful abstraction over REST apis. I can't wait to see how it develops into the future. |
I also need a unix stamp. I need a timestamp that I can do some math on in a |
formatdate
Having 'rfc3339_to_unix()' and 'unix_to_rfc3339()' functions would enable use of arithmetic operators and comparison operations on dates and times for scheduling puposes. E.g. calculate desired start and end windows, based on open ended repeating intervals from a base date time reference. Especially when crossing year boundaries. One of those math operators is of course the % operator (modulo arithmetic). Having come up against difficulties and limitations with "time_rotating" resource, timeadd() function, regex, formatdate() - it shouldn't be this hard. Rfc3339 exists purely for readability and we shouldn't have to resort to arcane code to do basic maths. |
Hi @mikewoodd3432, While it is true that you can potentially treat a Unix timestamp as a number and do arithmetic to it, it has been my experience that systems which do that inevitably have weird edge cases due to their unawareness of calendar and of details like leap seconds. I'm not meaning to suggest that this is true in all cases or true in your case, but if more complex date and time manipulation were the use-case I expect we'd aim to meet it by providing time and calendar aware functionally specific to timestamps, rather than encouraging the naive use of integer arithmetic on timestamps. I expect that once we can support functions contributed by providers the If we were to add a function for generating Unix timestamps then I expect it would be only to solve the original stated problem of integrating with a system that can only accept that as its input format, with the assumption that the resulting timestamp will just be passed verbatim and not used as part of other calculations. At the moment though it seems like this is niche enough that we'll probably delegate it also to a provider (perhaps (There are many other requests for specialized functions like this and for our planning and prioritization purposes we are considering them all together to be indirect justification for #31225, which would make provider-contributed functions possible. In the meantime providers can achieve a similar effect with a considerably more awkward syntax using data sources that perform only local calculations.) |
Thanks @apparentlymart ok I admit you're making some sense, but in reply I would say, terraform appears to have gotten providers to standardise (nicely) on RFC3339 as the standard date-time data type, and could do with providing a bit more filler for the current gaps? Someone might debate that In summary I do feel strongly the "Data and Time functions" category is rather wanting. Lack of calendar aware "dateadd" or equivalent enhancement to "timeadd" is glaring (understandable but glaring). Calendar aware functions would be my first wish. Unix time would be nice. |
In case it's interesting to anyone, support for provider-contributed functions is planned for the forthcoming Terraform v1.8, and so once the plugin framework is updated to help providers expose such functions it would become technically possible to offer a variety of time and date related functions in the I don't belong to the team that is ultimately responsible for that provider and so I cannot decide on their behalf that such things are in scope there, but it might be worth starting a discussion in an issue over there about it, if anyone who was following this issue is still interested. |
The team maintaining the If you have any other RFC3339 or standards-based time functionality requests, please feel free to create a feature request in that provider's issue tracker: https://github.com/hashicorp/terraform-provider-time/issues |
Thank you for your continued interest in this issue. Terraform version 1.8 launches with support of provider-defined functions. It is now possible to implement your own functions! We would love to see this implemented as a provider-defined function. Please see the provider-defined functions documentation to learn how to implement functions in your providers. If you are new to provider development, learn how to create a new provider with the Terraform Plugin Framework. If you have any questions, please visit the Terraform Plugin Development category in our official forum. We hope this feature unblocks future function development and provides more flexibility for the Terraform community. Thank you for your continued support of Terraform! |
Hey everyone 👋🏻 , Following up on @bflad's #31751 (comment), the You can test this out now with Terraform # Configuration using provider functions must include required_providers configuration.
terraform {
required_providers {
time = {
source = "hashicorp/time"
version = "0.11.1"
}
}
# Provider functions require Terraform 1.8 and later.
required_version = ">= 1.8.0"
}
locals {
plan_time = provider::time::rfc3339_parse(plantimestamp())
}
output "unix" {
value = local.plan_time.unix
} $ terraform plan
Changes to Outputs:
+ unix = 1710196634 If you run into any problems with the new function, please create an issue over in the |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Terraform Version
Use Cases
Certain data sources require a timestamp as input. For example: google_service_account_jwt optionally takes an
exp
claim in its json payload.Attempted Solutions
Currently this is the only way i manage to do it:
The problem is that is only works on linux and depends on
jq
being installed.Proposal
Add the option for the formatdate function to convert to unix time.
References
No response
The text was updated successfully, but these errors were encountered: