Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在CMake中将UNIX时间戳转换为ISO_Cmake_Unix Timestamp - Fatal编程技术网

如何在CMake中将UNIX时间戳转换为ISO

如何在CMake中将UNIX时间戳转换为ISO,cmake,unix-timestamp,Cmake,Unix Timestamp,我有一个UNIX风格的时间戳,看起来像1587405820-0600,我想把它转换成ISO风格的格式,比如YYYY-MM-DDTHH:MM:SSZ CMake在处有一个字符串(TIMESTAMP…命令,但这只获取格式化字符串中的当前时间,该字符串不适用于我的应用程序。我需要能够转换成ISO格式的现有时间 有办法做到这一点吗 更新 根据@squareskittles的回答,以下是我在做正确事情的测试中得出的结论: # Check that we get the current timestamp

我有一个UNIX风格的时间戳,看起来像
1587405820-0600
,我想把它转换成ISO风格的格式,比如
YYYY-MM-DDTHH:MM:SSZ

CMake在处有一个
字符串(TIMESTAMP…
命令,但这只获取格式化字符串中的当前时间,该字符串不适用于我的应用程序。我需要能够转换成ISO格式的现有时间

有办法做到这一点吗


更新 根据@squareskittles的回答,以下是我在做正确事情的测试中得出的结论:

# Check that we get the current timestamp
string(TIMESTAMP TIME_T UTC)
message(STATUS ">>> T1: ${TIME_T}")

# Get the ISO string from our specific timestamp
set(ENV{SOURCE_DATE_EPOCH} 1587405820)
string(TIMESTAMP TIME_T UTC)
unset(ENV{SOURCE_DATE_EPOCH})
message(STATUS ">>> T2: ${TIME_T}")

# Check that we get the current timestamp again correctly
string(TIMESTAMP TIME_T UTC)
message(STATUS ">>> T3: ${TIME_T}")
这给了我这个输出:

-- >>> T1: 2020-04-22T15:08:13Z
-- >>> T2: 2020-04-20T18:03:40Z
-- >>> T3: 2020-04-22T15:08:13Z

如果希望此函数使用当前时间以外的特定时间,可以将环境变量
SOURCE\u DATE\u EPOCH
添加到UNIX样式的时间戳(整数):

打印(适用于UTC-0600):

如果需要将此时间调整为UTC时间,可以添加
UTC
参数:

set(ENV{SOURCE_DATE_EPOCH} 1587405820)
string(TIMESTAMP MY_TIME UTC)
message(STATUS ${MY_TIME})
印刷品:

2020-04-20T18:03:40Z

注意:如果此
SOURCE\u DATE\u EPOCH
变量在CMake代码中的其他位置使用,最好在修改之前保存
SOURCE\u DATE\u EPOCH
值,以便在完成后将其设置回其以前的值

我相信我会建议在使用后将
源日期日期日期
重置为以前的值,这样就不会干扰代码的其余部分。@KamilCuk这是一个很好的观点,所以我添加了一个注释。像这样在CMake中设置环境变量的范围只会影响当前的CMake调用,因此效果仅限于此。
set(ENV{SOURCE_DATE_EPOCH} 1587405820)
string(TIMESTAMP MY_TIME UTC)
message(STATUS ${MY_TIME})
2020-04-20T18:03:40Z