在C++ 中想要获取当前系统的时候可以使用函数
time_t time (time_t* timer);
使用这个函数如果传入的 参数不是NULL 那么,它就会把当前系统的时间设置到这个指针当中
这个函数返回的 数字是 从 00:00 hours, Jan 1, 1970 UTC 的 秒
struct tm * localtime (const time_t * timer);把time_t类型转换为 本地时间char* asctime (const struct tm * timeptr);把本地时间格式化成字符串的形式。示例:/* localtime example */ #include <stdio.h> /* puts, printf */ #include <time.h> /* time_t, struct tm, time, localtime */ int main () { time_t rawtime; struct tm * timeinfo; time (&rawtime); timeinfo = localtime (&rawtime); printf ("Current local time and date: %s", asctime(timeinfo)); return 0; }
Output:
Current local time and date: Wed Feb 13 17:17:11 2013当然我们可以对 tm 这个结构体中的数据进行加工处理,获取我们想要的格式的输出时间。
C++ 系统时间获取
最新推荐文章于 2024-08-24 18:25:54 发布