Linux时间管理

9 篇文章 0 订阅
6 篇文章 0 订阅

Linux时间管理

在Linux系统下,对时间管理首先要弄清楚UTC时间和Local Time时间的区别。

  • UTC(Universal Time Coordinated)即GMT(Greenwich Mean Time)。

  • Local time 为本地时间
    系统默认的时区配置文件位置为/etc/sysconfig/clock:
    如果要修改设置时区,可以使用tzselect命令。如果要显示当前系统时间,可以使用命令:
    date //时钟格式显示当前时间
    date +%s //以秒为单位显示当前时间,自1970-1-1:0:0:0到现在秒数
    在编程应用中,经常需要读取系统时间、进程运行的时间等信息,这涉及到大量在time.h头文件中声明的函数。
    clock()函数用来查看进程运行 的时间、声明如下:
    extern clock_t clock(void)
    此函数返回当前时刻程序运行的时间(user time+system time),其结果为时钟计数器值,将其转换为秒的公式为:
    result/CLOCK_PER_SECOND
    函数time()用来获取当前系统时间,函数声明如下:
    extern time_t time(time_t *_timer)
    其时间是自1970-1-1 0:0:0以来经历的秒数。如果其参数设置为空,将返回时间秒数,如果参数不为空,将存储于该参数中。
    显然,直接使用秒数是不符合人们的习惯的,需要把秒数转换为人们熟悉的时间格式,函数ctime将返回当前时间字符串,该函数声明如下:
    extern char *ctime (_const time_t *_timer)
    它将时间转换为如下格式:
    Day Mon dd hh:mm:ss yyyy
    函数gmtime将返回当前时间,其时间基准为UTC,该函数声明如下:
    extern struct tm *gmtime(_const time_t *_timer)
    函数localtime将返回本地时间,其时间基准为当前设置的时区,该函数声明如下:
    extern struct tm *localtime(_const time_t *timer)
    以上两个函数返回struct tm结构体存储时间,该结构体声明如下:
    struct tm {
    int tm_sec; //seconds after the minute [0,60]
    int tm_min; //minutes after the hour [0,59]
    int tm_hour; //hours after the midnight [0,23]
    int tm_mday; //day of the month [1,31]
    int tm_mon; //month of the year [0,11]
    int tm_year; //year since 1900
    int tm_wday; //days since sunday [0,6]
    int tm_yday; //days since January 1: [0,365]
    int tm_isdst; //daylight saving time flag: <0, 0, >0
    };
    如果需要将此时间类型转换为人们习惯的时间字符串,可以调用asctime函数,该函数声明如下:
    extern char * asctime(_const struct tm *_tp)
    如果需要从struct tm中提取某一项,例如日期,可以调用函数strftime()函数,该函数声明如下:
    extern size_t strftime(char *_restrict _s,size_t _maxsize,_const char * _restrict _format,_const struct tm * _restrict _tp)
    此函数第一个参数为存储某项的空间,第二个参数为该空间的大小,第三个参数为欲提取的项,第四个参数为从哪个struct tm中提取,其中 ,第三个参数可提取项可为以下任意项:
    %a 缩写的周如名
    %A 全周日名
    %b 缩写的月名
    %B 月全名
    %c 日期和时间
    %d 月日
    %H 小时(每天24小时)
    %I 小时(上、下午各12小时)
    %j 年日
    %m 月
    %M 分
    %P AM/PM
    %S 秒
    %U 星期日周数
    %w 周日
    %W 星期一周数
    %x 日期
    %X 时间
    %y 不带公元的年
    %Y 带公元的年
    %Z 时区名
    //time_exp.c
    #include<stdio.h>
    #include<time.h>
    #include<string.h>
    int main()
    {
    time_t timep;
    time(&timep); //读取时间,秒数
    printf(“ctime return:%s\n”,ctime(&timep)); //转换为字符串输出

    time_t timep1,timep2;
    time(&timep1); //提取时间,秒数
    time(&timep2); //提取时间,秒数
    printf("%s\n",asctime(gmtime(&timep1))); //转换为UCT时间,并以字符串输出
    printf("%s\n",asctime(localtime(&timep2))); //转换为本地时间,并以字符串输出

    char buff[128];
    memset(buff,’\0’,128);
    printf(“globe time:”);
    strftime(buff,128,"%Z",gmtime(&timep1)); //提示时区
    printf(“TZ=%s\n”,buff);

    printf(“local time:”);
    strftime(buff,128,"%Z",localtime(&timep2)); //提示时区
    printf(“TZ=%s\n”,buff);
    return 0;
    }
    ctime return:Sat May 9 15:14:33 2020

Sat May 9 07:14:33 2020

Sat May 9 15:14:33 2020

globe time:TZ=GMT
local time:TZ=CST

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值