Linux API之获取系统时间

time系统调用返回当前时间以秒为单位的距离1970-01-01 00:00:00 +0000(UTC)过去的秒数。这个time内部就是用jiffies换算得到的秒数。其他函数基本都是围绕着time来工作的。

函数原型是:time_t time(time_t *t);   传进去的是一个数据类型,这个函数会将这个数据进行填充
这个函数有两种使用方法
(1):通过传参来返回  time(&tNow);
(2)不通过传参返回   Now = time(NULL);

gmtime和localtime会把time得到的秒数变成一个struct tm结构体表示的时间。区别是gmtime得到的是国际时间,而localtime得到的是本地(指的是你运行localtime函数的程序所在的计算机所设置的时区对应的本地时间)时间。mktime用来完成相反方向的转换(struct tm到time_t)

 下面函数都是基于time函数工作的

man手册中的函数原型
#include <time.h>

char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);

struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);

time_t mktime(struct tm *tm);


 struct tm 
 {
	int tm_sec;         // seconds 
	int tm_min;         // minutes 
	int tm_hour;        // hours 
	int tm_mday;        // day of the month 
	int tm_mon;         // month 
	int tm_year;        // year 
	int tm_wday;        // day of the week 
	int tm_yday;        // day in the year 
	int tm_isdst;       // daylight saving time 
};

下面进行time函数与常用函数的演示:

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>


int main(void)
{
	time_t tNow = -1;
	struct tm tmNow;
	char buf[100];
	struct timeval tv = {0};
	struct timezone tz = {0};
	int ret = -1;
	
	// time
	//tNow = time(NULL);		// 返回值
	time(&tNow);				// 指针做输出型参数
	if (tNow < 0)
	{
		perror("time");
		return -1;
	}
	printf("time: %ld.\n", tNow);
	
	// ctime
	printf("ctime: %s.\n", ctime(&tNow));
#if 0	
	// gmtime 和localtime
	memset(&tmNow, 0, sizeof(tmNow));
	gmtime_r(&tNow, &tmNow);
	printf("年%d月%d日%d时%d.\n", tmNow.tm_year, tmNow.tm_mon, tmNow.tm_mday, tmNow.tm_hour);
	
	memset(&tmNow, 0, sizeof(tmNow));
	localtime_r(&tNow, &tmNow);
	printf("年%d月%d日%d时%d.\n", tmNow.tm_year, tmNow.tm_mon, tmNow.tm_mday, tmNow.tm_hour);
#endif

#if 0
	// asctime
	memset(&tmNow, 0, sizeof(tmNow));
	localtime_r(&tNow, &tmNow);
	printf("年%d月%d日%d时%d.\n", tmNow.tm_year, tmNow.tm_mon, tmNow.tm_mday, tmNow.tm_hour);
	printf("asctime:%s.\n", asctime(&tmNow));
#endif

#if 0
	// strftime
	memset(&tmNow, 0, sizeof(tmNow));
	localtime_r(&tNow, &tmNow);
	printf("年%d月%d日%d时%d.\n", tmNow.tm_year, tmNow.tm_mon, tmNow.tm_mday, tmNow.tm_hour);
	
	memset(buf, 0, sizeof(buf));
	strftime(buf, sizeof(buf), "%Y * %m * %d, %H-%M-%S.", &tmNow);
	printf("时间为:[%s].\n", buf);
#endif

	// gettimeofday
	ret = gettimeofday(&tv, &tz);
	if (ret < 0)
	{
		perror("gettimeofday");
		return -1;
	}
	printf("seconde: %ld.\n", tv.tv_sec);
	printf("timezone:%d.\n", tz.tz_minuteswest);
	
	
	return 0;
}

(1)从OS中读取时间时用不到mktime的,这个mktime是用来向操作系统设置时间时用的。

asctime得到一个固定格式的字符串格式的当前时间,效果上和ctime一样的。区别是ctime从time_t出发,而asctime从struct tm出发。

asctime和ctime得到的时间字符串都是固定格式的,没法用户自定义格式,如果需要用户自定义时间的格式,则需要用strftime。

使用方法如下:

 

 

 

注明:本博客是根据朱有鹏老师的视频所作的总结

猜你喜欢

转载自blog.csdn.net/David_361/article/details/85698912