将时间戳()转换为仅数字字符串 [英] Terraform timestamp() to numbers only string

查看:144
本文介绍了将时间戳()转换为仅数字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

timestamp() 函数在插值语法中将返回一个 ISO 8601 格式的字符串,类似于 2019-02-06T23:22:28Z.但是,我想要一个类似于 20190206232240706500000001 的字符串.一个只有数字(整数)且没有连字符、空格、冒号、Z 或 T 的字符串.有什么简单而优雅的方法来实现这一点?

The timestamp() function in the interpolation syntax will return an ISO 8601 formatted string, which looks like this 2019-02-06T23:22:28Z. However, I want to have a string which looks like this 20190206232240706500000001. A string with only numbers (integers) and no hyphens, white spaces, colon, Z or T. What is a simple and elegant way to achieve this?

如果我在连字符、空格、冒号 Z 和 T 时替换每个单个字符类,它会起作用:

It works if I replace every a single character class at the time hyphens, white spaces, colon Z and 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}"
}

生成的输出是所需的格式,除了字符串明显更短:

The resulting output is in the desired format, except the string is significantly shorter:

Outputs:

timestamp = 20190207000744

但是,这个解决方案非常难看.是否有另一种方法可以以更优雅的方式执行相同的操作,并生成与示例字符串 20190206232240706500000001 长度相同的字符串?

However, this solution is very ugly. Is there another way of doing the same thing in a more elegant way as well as producing a string with the same length as the example string 20190206232240706500000001?

推荐答案

当前interpolate 函数 timestamp() 已在源代码中使用输出格式 RFC3339 进行硬编码:

https://github.com/hashicorp/terraform/blob/master/config/interpolate_funcs.go#L1521

返回时间.Now().UTC().Format(time.RFC3339),无

return time.Now().UTC().Format(time.RFC3339), nil

所以你的方式没有问题,但是我们可以稍微改进一下.

So there is nothing wrong with your way, however we can improve it a little bit.

locals {
  timestamp = "${timestamp()}"
  timestamp_sanitized = "${replace("${local.timestamp}", "/[- TZ:]/", "")}"

}

参考:

https://github.com/google/re2/wiki/Syntax

replace(string, search, replace) - 对给定的字符串进行搜索和替换.所有的 search 实例都被替换为 replace 的值.如果搜索包含在正斜杠中,则将其视为正则表达式.如果使用正则表达式,replace 可以使用 $n 引用正则表达式中的子捕获,其中 n 是子捕获的索引或名称.如果使用正则表达式,语法符合 re2 正则表达式语法.

replace(string, search, replace) - Does a search and replace on the given string. All instances of search are replaced with the value of replace. If search is wrapped in forward slashes, it is treated as a regular expression. If using a regular expression, replace can reference subcaptures in the regular expression by using $n where n is the index or name of the subcapture. If using a regular expression, the syntax conforms to the re2 regular expression syntax.

这篇关于将时间戳()转换为仅数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆