**AT91SAM9G45系统编译过程中,有时需要系统打印时间来作为测试代码,整理下面测试方法,供大家学习参考。
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
/*
- 获取系统时间,这种方式获取得到的格式是: Wed Mar 30 20:41:21 2022
*/
int printf_time(void)
{
time_t timep;
time(&timep);
char *s = ctime(&timep);
printf(“date:%s”,s);
return 0;
}
/*
-
获取系统时间,这种方式获取得到的格式是: 2022-03-30 20:41:21.104
*/
int get_time_ms(char buff, int len)
{
struct timeval tv;
struct tm ptm;
char time_string[40];
long milliseconds;if(buff == NULL)
{
printf(“%s buff is NULL.\n”, func);
return -1;
}gettimeofday(&tv, NULL);
ptm = localtime (&(tv.tv_sec));
strftime (time_string, sizeof