时间(2019-07-21 22:03:57)转换为时间戳 (1563717837)

int gettimestamp(const char *spTime)
{
        struct tm s_time;
        time_t s_timep = 0;

        sscanf(spTime, "%d-%d-%d %d:%d:%d", &s_time.tm_year, &s_time.tm_mon, &s_time.tm_mday,
                        &s_time.tm_hour, &s_time.tm_min, &s_time.tm_sec);

        s_time.tm_year -= 1900;
        s_time.tm_mon -= 1;

        s_timep = mktime(&s_time);

        return s_timep;
}

时间戳 (1563717837)转换为时间(2019-07-21 22:03:57)

void getTime(struct timeval tv, char *szTime)
{
	struct tm *p;
	
	p = localtime(&tv.tv_sec);
	memset(szTime, '\0', 32);
	sprintf(szTime, "%04d-%02d-%02d %02d:%02d:%02d", p->tm_year+1900, p->tm_mon+1,
					p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
}