你有几个选择。
假设您将它放在一个名为的变量字符数组中string
.
现在,如果您知道时间始终位于字符串的末尾,那么您可以很容易地执行以下操作:
#define TIMEWIDTH 8
#include <stdio.h>
#include <string.h>
int main() {
const char string[] = {"0001-01-01T17:45:33\0"};
unsigned int strlength = strlen(string);
char temp[TIMEWIDTH + 1]; // add one for null character
printf("%s\n", string);
strncpy(temp, string + strlength - TIMEWIDTH, TIMEWIDTH + 1); // another + 1 for the null char
printf("%s\n", temp);
}
如果它更复杂,您必须进行更多分析才能找到它。手动或使用不同的可用工具,例如sscanf()
或者是其他东西。确保特定的宽度sscanfs()
.
http://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm http://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm
if the T
指示您可以使用 strchr 查找它的时间的开始:
#include <stdio.h>
#include <string.h>
int main() {
const char string[] = {"0001-01-01T17:45:33\0"};
char *temp;
temp = strchr(string, 'T') + 1;
printf("%s\n", temp);
}
这实际上取决于输入的变量......如果它只是一个单一的例子,你可以使用其中之一。虽然最后一种效率更高。
您指出它是 ISO 8601 时间戳。那我就用第二种方法。