【发布时间】:2015-06-27 20:14:34
【问题描述】:
这是我在 Linux 中如何做到这一点的示例,但在 Windows 中我没有 strptime 函数,任何人都可以帮我解决这个问题吗?
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
time_t to_seconds(const char *date)
{
struct tm storage = {0, 0, 0, 0, 0, 0, 0, 0, 0};
char *p = NULL;
time_t retval = 0;
p = (char *) strptime(date, "%d-%b-%Y", &storage);
if (p == NULL)
{
retval = 0;
}
else
{
retval = mktime(&storage);
}
return retval;
}
int main(int argc, char** argv)
{
time_t d1 = to_seconds("16-Jun-2015");
time_t d2 = to_seconds("13-Jun-2015");
if(d1 > d2)
{
printf("date 1 > date 2");
}
}
【问题讨论】:
-
为什么选择演员
(char *) strp ...?另外,strptime()不是标准的。 -
我的意思是当它可用时你不需要将
strptime()转换为char *,它不在 Windows 上,因为它不是标准功能。为什么是这种格式,你从哪里得到日期?是文件吗? -
你是对的......我从网络上 c/p 那个函数它在 Linux 上运行良好,但在 Windows 上运行良好。我可以用什么代替 strptime() ?
-
这取决于你为什么要使用它,如果你解释用例我可能会帮助你。如果你想写信给我 iharob@gmail.com
-
我编写了一些开源项目...我需要比较日期然后对数组中的行进行排序,项目网址:github.com/vforv/reminder