1. 概述:
该demo主要实现linux下时间的基本使用, 更新本地时间, 以及计算程序运行时间差
2. 测试:
/*
demo_time_update.c
时间编程demo : 更新本地时间
1. 硬件时间 :
指主机板上的时钟设备, 也就是通常可在BIOS画面设定的时钟
相关命令 : hwclock 或者 timedatectl
2. 系统时间 :
指kernel中的时钟, 当Linux启动时, 系统时钟会去读取硬件时钟的设定, 之后系统时钟即独立运作
所有Linux相关指令与函数都是读取系统时钟的设定
相关命令 : date 或者 timedatectl
3. GMT : 格林威治时间
4. UTC : 世界标准的时间
5. 本地时间 : GMT + 时区 或者 UTC + 时区
6. 日历时间 : 一个标准时间点(如1970年1月1日0点)到此时经过的秒数
(所以可以说日历时间是相对时间, 无论你在哪一个时区, 在同一时刻对同一个标准时间点来说, 日历时间都是一样的)
7. 联网更新时间 :
ntpdate cn.pool.ntp.org
8. 获取本地时区 :
相关命令 : date -R 或者 timedatectl
9. 设置本地时区 :
void tzset(void)
相关命令 : tzselect
10. 获取日历时间 :
time_t time(time_t *tloc)
11. 设置本地时间 :
int stime(time_t *t)
int settimeofday(const struct timeval *tv , const struct timezone *tz);
12. 时间格式转换 :
struct tm *gmtime_r(const time_t *timep, struct tm *tm)
struct tm *localtime_r(const time_t *timep, struct tm *tm)
char *ctime_r(const time_t *timep, char *buf)
char *asctime_r(const struct tm *tm, char *buf)
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *tm)
char *strptime(const char *s, const char *format, struct tm *tm)
time_t mktime(struct tm *tm)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define MAX_BUF 64
int main(int argc, char **argv){
char buf[MAX_BUF];
time_t seconds = 0;
struct tm gmt;
/*
获取日历时间
*/
seconds = time((time_t *)NULL);
printf("time\t\t seconds = %ld\n\n",seconds);
/*
日历时间->tm结构体的UTC时间
*/
gmtime_r(&seconds, &gmt);
printf("gmtime_r\t %d-%02d-%02d ",(1900+gmt.tm_year),(1+gmt.tm_mon),gmt.tm_mday);
printf("%d %02d:%02d:%02d\n\n",gmt.tm_wday,gmt.tm_hour,gmt.tm_min,gmt.tm_sec);
/*
日历时间->tm结构体的本地时间
*/
localtime_r(&seconds, &gmt);
printf("localtime_r\t %d-%02d-%02d ",(1900+gmt.tm_year),(1+gmt.tm_mon),gmt.tm_mday);
printf("%d %02d:%02d:%02d\n\n",gmt.tm_wday,gmt.tm_hour,gmt.tm_min,gmt.tm_sec);
/*
日历时间->字符串形式的本地时间
*/
memset(buf, 0x0, sizeof(buf));
ctime_r(&seconds, buf);
printf("ctime_r\t\t %s\n", buf);
/*
tm结构体的时间->字符串形式的时间
*/
memset(buf, 0x0, sizeof(buf));
asctime_r(&gmt, buf);
printf("asctime_r\t %s\n", buf);
/*
根据定义的格式化规则, tm结构体的时间->格式化字符串的时间
*/
memset(buf, 0x0, sizeof(buf));
strftime(buf, sizeof(buf), "%Y-%m-%d %w %H:%M:%S", &gmt);
printf("strftime\t %s\n\n", buf);
/*
tm结构体的时间->日历时间
*/
seconds = mktime(&gmt);
printf("mktime\t\t seconds = %ld\n", seconds);
/*
设置本地时间
*/
stime(&seconds);
return 0;
}
/*
demo_time_calculate.c
时间编程demo : 计算程序运行时间差
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
int main(int argc, char **argv){
float timeuse;
time_t timestart,timeend;
struct timeval tstart,tend;;
/*
获取日历时间
*/
gettimeofday(&tstart, NULL);
timestart = time(NULL);
/*
秒
*/
sleep(1);
/*
微妙
*/
usleep(1000);
timeend = time(NULL);
printf("difftime\t User time: %f seconds\n", difftime(timeend, timestart));
gettimeofday(&tend, NULL);
timeuse = (tend.tv_sec - tstart.tv_sec) + (float)(tend.tv_usec - tstart.tv_usec)/(1000*1000);
printf("gettimeofday\t User time: %f seconds\n",timeuse);
return 0;
}
#Makefile
CC := gcc
INCLUDE = -I /home/demo/include/
all:
$(CC) demo_time_update.c $(INCLUDE) -o demo_time_update -Wall -Werror
$(CC) demo_time_calculate.c $(INCLUDE) -o demo_time_calculate -Wall -Werror
clean:
rm demo_time_update demo_time_calculate