我需要了解如何将日期/时间(从服务器获取)转换为UNIX时间戳。我将用C编码。关于如何做到这一点的任何想法,或者有人指导我使用类似的现有代码?
答案 0 :(得分:-1)
请参阅此处的db_unix_timestamp()函数: http://svn.cubrid.org/cubridengine/trunk/src/query/string_opfunc.c
这是一种可能适合您的方式:
{
char *dateTimeString = "2014-05-10 15:50:49"; //"YYYY-MM-DD HH:MM:SS"
struct tm t;
time_t t_of_day;
char *cp = dateTimeString;
t.tm_year = strtol(cp, &cp, 10); // 2011-1900;
t.tm_mon = strtol(++cp, &cp, 10); // Month, 0 - jan
t.tm_mday = strtol(++cp, &cp, 10); // Day of the month
t.tm_hour = strtol(++cp, &cp, 10);
t.tm_min = strtol(++cp, &cp, 10);
t.tm_sec = strtol(++cp, &cp, 10);
t.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown
t_of_day = mktime(&t);
printf("seconds since the Epoch: %ld\n", (long) t_of_day)
}