求助:tm怎么转化成time_t

来源:百度知道 编辑:UC知道 时间:2024/06/18 22:54:52
我知道time_t可以转化成tm可通过gmtime_r
那么反向怎么转化?
ansi c

由于tc2.0中没有mktime()这个函数,所以要用高级一点的编译器,最好是满足ANSI/ISO的c/c++标准的编译器,比如dev-cpp.
下面的程序是计算今天(2009年6月21日是星期几)的程序,已编译正确。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
struct tm t;
time_t tlong;
t.tm_year=2009-1900;/*以1900年1月1日为基准,因为1900年1月1日是星期一*/
t.tm_mon=5;/*月份用0-11表示,故这里5表示六月*/
t.tm_mday=21;
t.tm_hour=0;
t.tm_min=0;
t.tm_sec=1;
tlong=mktime(&t);
printf(ctime(&tlong));
system("pause");
return 0;
}

time.h 里面这个函数

time_t mktime ( struct tm * ptm );


Standard:
time_t mktime(struct tm *tm);

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);

struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
------------------------------------
Other:
time_t timelocal(struct t