Linux获取系统时间_Linux系统

来源:微信公众号「编程学习基地」

获取系统时间

#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);
struct timeval {
 		time_t      tv_sec;     /* seconds */
 		suseconds_t tv_usec;    /* microseconds */
	};
struct timezone {
 		int tz_minuteswest;     /* minutes west of Greenwich */
 		int tz_dsttime;         /* type of DST correction */
 	};

使用示例:

#include<stdio.h>
#include <time.h>
#include <sys/time.h>
//得到当前系统的时间
void get_cur_time(char *time_str)
{
    struct timeval now;
    gettimeofday(&now, NULL);
    strcpy(time_str, ctime(&now.tv_sec));
}
int main()
{
	char* time_str;
	time_str = (char *)malloc(sizeof(char)*50);
	get_cur_time(time_str);	
	printf("Time is : %s\n", time_str);
	return 0;
}