#嵌入式芯片:
#include <stdio.h>
#include <sys/time.h>
int set_sys_tm_all(int year, int mon, int day, int hour, int min, int sec)
{
time_t rawtime;
struct tm *timeinfo;
struct timeval tv;
time(&rawtime);
timeinfo = localtime(&rawtime);
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = mon - 1;
timeinfo->tm_mday = day;
timeinfo->tm_hour = hour;
timeinfo->tm_min = min;
timeinfo->tm_sec = sec;
tv.tv_usec = 0;
tv.tv_sec = mktime(timeinfo);
return settimeofday(&tv, NULL);
}
char *get_sys_tm_str(int flag_format_YMD)
{
static char ret_str[35];
time_t tm_now = time(NULL);
struct tm *sys_tm = localtime(&tm_now);
if (flag_format_YMD)
sprintf(ret_str, "%d-%d-%d %02d:%02d:%02d", sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday, sys_tm->tm_hour, sys_tm->tm_min, sys_tm->tm_sec);
else
sprintf(ret_str, "%02d:%02d:%02d", sys_tm->tm_hour, sys_tm->tm_min, sys_tm->tm_sec);
return ret_str;
}