1、snprintf()函数
(1)函数描述
C 库函数 int snprintf(char *str, size_t size, const char *format, ...) 设将可变参数(...)按照 format 格式化成字符串,并将字符串复制到 str 中,size 为要写入的字符的最大数目,超过 size 会被截断。
(2)函数声明
int snprintf ( char * str, size_t size, const char * format, ... );
其中,str---存放最终结果的目标字符串,size---字符串的长度大小(字节为单位),format---字符串格式化要求,...---可变参数字符串。
在<stdio.h>文件中。
(3)返回值
正常情况下返回格式化以后字符串的字节数目,异常情况下返回值小于0.
(4)实例
1 #include <stdio.h>
2
3 int main()
4 {
5 char buffer[50]={0};
6
7 char *str="today is tuesday";
8
9 int ret=snprintf(buffer,10,"Info:%s\n",str);
10 printf("string:\n%s\ncharacter count=%d\n",buffer,ret);
11 return 0;
12 }
输出结果:
string:
Info:toda
character count=22
2、memcpy()函数
(1)函数描述
void *memcpy(void *str1, const void *str2, size_t n) 从存储区 str2 复制 n 个字节到存储区 str1。
(2)头文件
<string.h>
- str1 -- 指向用于存储复制内容的目标数组,类型强制转换为 void* 指针。
- str2 -- 指向要复制的数据源,类型强制转换为 void* 指针。
- n -- 要被复制的字节数。
(3)返回值
该函数返回一个指向目标存储区 str1 的指针。
3、关于系统时间相关的函数:localtime(),localtime_r()和gettimeofday()
(1)函数描述
/****************************gettimeofday()*************************************************/
int gettimeofday(struct timeval*tv, struct timezone *tz)
所属头文件: <sys/time.h>
其参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果:
struct timezone{
int tz_minuteswest;/*格林威治时间往西方的时差*/
int tz_dsttime;/*DST 时间的修正方式*/
};
timezone 参数若不使用则传入NULL即可。
而结构体timeval的定义为:
struct timeval{
long int tv_sec; // 秒数
long int tv_usec; // 微秒数
};
其返回值是从1970-1-1日到现在的的时间,可以精确到微妙级。
/****************************localtime_r()*************************************************/
struct tm *localtime_r(const time_t *timep, struct tm *result);(可重入)
所属头文件 <time.h>
其中result是保存转换时间结果的结构体
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()为负。*/
/****************************localtime()*************************************************/
原型: struct tm *localtime(const time_t * calptr); 头文件 <time.h>
返回值: 成功: 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日 */
int tm_isdst; /* 夏令时标识符,夏令时tm_isdst为正;不实行夏令时tm_isdst为0 */
};
此结构体空间由内核自动分配, 而且不要去释放它. 失败: NULL。
(2)举例
1 #include <stdio.h>
2 #include <time.h>
3 #include <sys/time.h>
4 #include <stdint.h>
5 #include "main.h"
6 #include "spi.h"
7 #include "mdio.h"
8
9 int main(void)
10 {
11 time_t t_now;
12 struct tm *local_time;
13 printf("hello,this is main function.\n");
14 //printf("Date:%s\n",DATE);
15 local_time=localtime(&t_now);
16 printf("[1]Now date:%d-%d-%d %d:%d:%d\n",local_time->tm_year+1900,local_time->tm_mon+1,
17 local_time->tm_mday,local_time->tm_hour,local_time->tm_min,local_time->tm_sec);
18 //
19 struct timeval tv;
20 gettimeofday(&tv,NULL);
21 uint64_t sec=tv.tv_sec;
22 localtime_r((time_t*)&sec,local_time);
23 printf("[2]Now date:%d-%d-%d %d:%d:%d\n",local_time->tm_year+1900,local_time->tm_mon+1,
24 local_time->tm_mday,local_time->tm_hour,local_time->tm_min,local_time->tm_sec);
25 spi_interface_func();
26 printf("\n");
27 mdio_interface_func();
28 return 0;
29 }
结果:
hello,this is main function.
[1]Now date:1970-1-1 0:0:0
[2]Now date:2020-10-13 16:55:40
this is spi interface function.
this is mdio interface function.
4、