插值语法中的timestamp()
函数将返回一个 ISO 8601 格式的字符串,如下所示2019-02-06T23:22:28Z
。但是,我想要一个看起来像这样的字符串20190206232240706500000001
。一个只有数字(整数)且没有连字符、空格、冒号、Z 或 T 的字符串。实现此目的的简单而优雅的方法是什么?
如果我在连字符、空格、冒号 Z 和 T 时替换每个单个字符类,它会起作用:
locals {
timestamp = "${timestamp()}"
timestamp_no_hyphens = "${replace("${local.timestamp}", "-", "")}"
timestamp_no_spaces = "${replace("${local.timestamp_no_hyphens}", " ", "")}"
timestamp_no_t = "${replace("${local.timestamp_no_spaces}", "T", "")}"
timestamp_no_z = "${replace("${local.timestamp_no_t}", "Z", "")}"
timestamp_no_colons = "${replace("${local.timestamp_no_z}", ":", "")}"
timestamp_sanitized = "${local.timestamp_no_colons}"
}
output "timestamp" {
value = "${local.timestamp_sanitized}"
}
结果输出采用所需格式,但字符串明显更短:
Outputs:
timestamp = 20190207000744
但是,这个解决方案非常难看。是否有另一种方法可以以更优雅的方式做同样的事情,以及生成与示例字符串长度相同的字符串20190206232240706500000001
?