
#include
#include
void
main()
{
time_t
ltime;
struct
tm
today;
time(
<ime
);
today
=
localtime(
<ime
);
printf("%04d-%02d-%02d
%02d:%02d:%02d\n",1900+today->tm_year,today->tm_mon+1,today->tm_mday,today->tm_hour,today->tm_min,today->tm_sec);
}
先调用time获得当前时间,这是个从1970-1-1午夜0点开始的秒数,然后调用localtime将该时间专为本地时间就可以打印了。其中tm_year需要加上1900,tm_mon需要加上1,看printf你就明白了。
time是C语言获取当前系统时间的函数,以秒作单位,代表当前时间自Unix标准时间戳(1970年1月1日0点0分0秒,GMT)经过了多少秒。
形式为time_t
time(time_t
t);
该函数提供两种返回方式,返回值,和指针参数。
可以根据需要选择。当参数t为空指针(NULL)时,只返回值。
而NULL的定义是(void
)
0,
所以time(0)也就是time(NULL)的另一种写法,表示只通过返回值获取时间值。
扩展资料:
time函数
函数名称:
localtime
函数原型:
struct
tm
localtime(const
time_t
timer)
函数功能:
返回一个以tm结构表达的机器时间信息
函数返回:
以tm结构表达的时间,结构tm定义如下:
#ifndef
_TM_DEFINED
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()为负。/
};
#define
_TM_DEFINED
#endif
参数说明:
timer-使用time()函数获得的机器时间
参考资料来源:百度百科-timeh
程序主要通过当前系统日历的struct tm结构体获得,主要代码如下,
#include <stdioh>
#include <timeh>
//程序功能输出当前时间在24H下的小时数
int main(int argc, char argv[])
{
struct tm ptr;
time_t lt;
time(<);//当前系统时间
ptr=localtime(<);//获取本地日历时间指针
printf("hour=%d(24H )\n",ptr->tm_hour);//输出24H下的小时数
return 0;
}
结构体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()为负。/
long int tm_gmtoff; /指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数/
const char tm_zone; /当前时区的名字(与环境变量TZ有关)/
};
主要分为两种方法:
1这种方法比较高级
#include <timeh>#include <stdioh>
#include <timeh>
int main(int argc, char argv)
{
time_t temp;
struct tm t;
time(&temp);
t = localtime(&temp);
printf("当前时间是:\n%d年%d月%d日\n", t->tm_year+1900, t->tm_mon+1 , t->tm_mday);
printf("%d时%d分%d秒\n", t->tm_hour, t->tm_min, t->tm_sec);
/
t结构体内的成员变量还有以下几个:
tm_wday 星期的第几天 tm_yday 这天是这年的第几天
/
return 0;
}
需要注意的是tm_year返回的是1900年之后的年数,tm_mon返回的比实际月份小1(至于为什么要这样设计,我不是太清楚)
2这种方法较为简单方便,但是同时可能会对接下来的其它 *** 作不利。
#include <timeh>#include <stdioh>
int main(int argc, char argv)
{
time_t temp;
time(&temp);
printf("当前时间为:\n%s", ctime(&temp));
return 0;
}
uptime是linux命令里获取有关系统时间的。他就是到/proc/uptime找出一些信息计算的。
你也可以
/proc/uptime 提供了系统最近一次启动以来运行的时间,
读取这个文件然后自己计算,uptime里的第一个时间是up后的seconds,自己折算为天数等。
1、C语言中读取系统时间的函数为time(),其函数原型为:
#include <timeh>
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 <timeh>
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()为负。/
};
以上就是关于c语言如何获取系统时间并将时间储存在字符串里全部的内容,包括:c语言如何获取系统时间并将时间储存在字符串里、C语言中time(0)的意思是、C语言中如何获取当前系统时间的小时等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)