C语言中系统时间函数是怎么用的??

如题所述

1、C语言中读取系统时间的函数为time(),其函数原型为:
#include <time.h>
time_t time( time_t * ) ;
time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。
2、C语言还提供了将秒数转换成相应的时间格式的函数:
char * ctime(const time_t *timer); //将日历时间转换成本地时间,返回转换后的字符串指针 可定义字符串或是字符指针来接收返回值
struct tm * gmtime(const time_t *timer); //将日历时间转化为世界标准时间(即格林尼治时间),返回结构体指针 可定义struct tm *变量来接收结果
struct tm * localtime(const time_t * timer); //将日历时间转化为本地时间,返回结构体指针 可定义struct tm *变量来接收结果
3、例程:

#include <time.h>
void main()
{
time_t t;
struct tm *pt ;
char *pc ;
time(&t);
pc=ctime(&t) ; printf("ctime:%s", pc );
pt=localtime(&t) ; printf("year=%d", pt->tm_year+1900 );
}
//时间结构体struct tm 说明:
struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-02-13
#include《time.h》
可以配合随机化算法使用
如数值计算随机化等
第2个回答  推荐于2017-10-13
在C语言中,用SYSTEMTIME数据结构,通过GetSystemTime()函数来得到系统时间。
#include "WTYPES.H"
SYSTEMTIME time;
GetSystemTime(&time);
cout << time.wYear<<"-" <<time.wMonth<<"-"time.wDay;

如果是C++中,更简单,用CTime类,很容易操作:
CTime mTime;
CString str;
mTime=CTime::GetCurrentTime();
str = mTime.Format("%Y-%m-%d");本回答被提问者采纳
第3个回答  2011-02-13
装个msdn,搜搜就知道了。
相似回答