linux 时间 基本使用

1. 概述:
该demo主要实现linux下时间的基本使用, 更新本地时间, 以及计算程序运行时间差

2. 测试:
在这里插入图片描述

/*
time_update_tip.h

*/
#include <time.h>

/*
硬件时间 : 
    指主机板上的时钟设备, 也就是通常可在BIOS画面设定的时钟
    相关命令 : hwclock 或者 timedatectl

系统时间 : 
    指kernel中的时钟, 当Linux启动时, 系统时钟会去读取硬件时钟的设定, 之后系统时钟即独立运作
    所有Linux相关指令与函数都是读取系统时钟的设定
    相关命令 : date 或者 timedatectl

GMT : 格林威治时间

UTC : 世界标准的时间

本地时间 : GMT + 时区 或者 UTC + 时区

日历时间 : 一个标准时间点(如1970年1月1日0点)到此时经过的秒数
              (所以可以说日历时间是相对时间, 无论你在哪一个时区, 在同一时刻对同一个标准时间点来说, 日历时间都是一样的)
*/

/******************************联网更新时间*************************************/
//相关命令 : ntpdate cn.pool.ntp.org

/******************************获取本地时区*************************************/
//相关命令 : date -R 或者 timedatectl

/******************************设置本地时区*************************************/
//相关命令 : tzselect
void tzset(void);

/******************************获取日历时间*************************************/
time_t time(time_t *tloc);

/******************************设置本地时间*************************************/
int stime(time_t *t);
int settimeofday(const struct timeval *tv , const struct timezone *tz);

/******************************时间格式转换*************************************/
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);
/*
demo_time_update.c
时间编程demo : 更新本地时间

*/
#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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值