php网站的数据库在哪,库存管理系统软件免费,网站建设规划ppt,网站后台下载目录
1.引言
2.时间函数基本信息
2.1 time函数
2.2 local函数
2.3 asctime函数
3.使用demo 1.引言
很多时候#xff0c;我们需要使用当前时间#xff0c;怎么获取呢#xff1f;其实c语言库函数中提供了一些列时间相关的函数#xff0c;可以从系统中获取时间的方法。…目录
1.引言
2.时间函数基本信息
2.1 time函数
2.2 local函数
2.3 asctime函数
3.使用demo 1.引言
很多时候我们需要使用当前时间怎么获取呢其实c语言库函数中提供了一些列时间相关的函数可以从系统中获取时间的方法。今天我们就熟悉一下相关函数吧并提供获取本地时间的demo.
2.时间函数基本信息
2.1 time函数 头文件 #includetime.h 函数原型 time_t time(time_t *seconds)参数说明 seconds -- 这是指向类型为 time_t 的对象的指针用来存储 seconds 的值返回值 尽管 C 标准没有定义它几乎总是一个保有从纪元开始秒数的整数值不计闰秒对应成功时返回编码成 time_t 对象的当前日历时间。错误时返回 (time_t)(-1) 。若seconds不是空指针则返回值也会存储于 arg 所指向的对象。time_t数据结构 typedef /* unspecified */ time_t; //暂时未指定 2.2 local函数 头文件 #includetime.h 函数原型 struct tm *localtime( const time_t *time );参数说明 time—转换从纪元开始的给定时间 常常是time函数返回的time_t 的值以 struct tm 格式及本地时间表达的日历时间。存储结果于静态存储并返回指向静态存储的指针。返回值 成功时为指向内部静态 struct tm 对象的指针否则为 NULL 。 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 到 11 */ int tm_year; /* 自 1900 年起的年数 */ int tm_wday; /* 一周中的第几天范围从 0 到 6 */ int tm_yday; /* 一年中的第几天范围从 0 到 365 */ int tm_isdst; /* 夏令时 */ }; 2.3 asctime函数 头文件 #includetime.h函数原型 char* asctime( const struct tm* time_ptr );功能将给定的日历时间 *time_ptr 转换成下列固定的 25 字符文本返回字符数组中 Www Mmm dd hh:mm:ss yyyy\n Www - 三字母英文星期缩写 来自 time_ptr-tm_wday 为 Mon 、 Tue 、 Wed 、 Thu 、 Fri 、 Sat 、 Sun 之一。Mmm - 三字母英文月份缩写 来自 time_ptr-tm_mon为 Jan 、 Feb 、 Mar 、 Apr 、 May 、 Jun 、 Jul 、 Aug 、 Sep 、 Oct 、 Nov 、 Dec 之一。dd - 月份日期的 2 位数字 来自 time_ptr-tm_mday 如 sprintf 用 %2d 打印。hh - 小时的 2 位数字 来自 time_ptr-tm_hour 如 sprintf 用 %.2d 打印。mm - 分的 2 位数字 来自 time_ptr-tm_min 如 sprintf 用 %.2d 打印。ss - 秒的 2 位数字 来自 time_ptr-tm_sec 如 sprintf 用 %.2d 打印。yyyy - 年的 4 位数字 来自 time_ptr-tm_year 1900 如 sprintf 用 %4d 打印。 3.使用demo
#includestdio.h
#includetime.hint main(int argn, char* argv[])
{time_t tm1;//为了使用localtime返回的存放静态时间日期的资源指针struct tm* ptm;//获取时间据某个历史时间的秒数tm1 time(NULL);ptm localtime(tm1);//打印其中asctime是为了把tm结构体的资源变成字符串printf(hello tm1:%ld\n,tm1);printf(hello ptm:\n%s\n,asctime(ptm));return 0;
}
编译 g time_demo.cpp -o time_demo 运行 ./time_demo 结果如下
rootxuehaiyang:/mnt/hgfs/99_github/cpp_demo# ./time_demo
hello tm1:1712334446
hello ptm:
Sat Apr 6 00:27:26 2024