我有一个 float 的日期和时间。
float thedate = 20161115;
float thetime = 181011.377500;
我想将它们转换为 ISO8601 日期时间格式,换句话说,将它们写入文件“2016-11-15T18:10:11.377500”
我通过执行以下操作设法在 char 数组中获取这些值:
char datetimestr[64];
snprintf(datetimestr, sizeof(datetimestr), "%8.0f%f", thedate, thetime);
printf(datetimestr);
然后打印:“20161115181011.377500”
我读过 strftime
和 strptime
等函数可以解析这些数字并正确输出它们,但为此我想我需要一些特定于架构的 header ?
实现我需要的推荐方法是什么?理想情况下以一种随处运行的方式。
如有任何帮助或进一步引用,我们将不胜感激。
最佳答案
您需要对输入值使用一些数学运算来提取它们的各个组成部分,例如:
int theyear = thedate / 10000;
int themonth = (thedate - (theyear * 10000)) / 100;
...and so on
然后您可以使用 sprintf()
构建您的字符串。
关于将日期从字符数组转换为 ISO8601,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40620806/