linux中time, ctime, gmtime, localtime, gettimeofday和strftime

  1. 与时间相关的结构:

  2. 
    struct tm {
        int tm_sec;      /* Seconds (0-60) */
        int tm_min;      /* Minutes (0-59) */
        int tm_hour;     /* Hours (0-23) */
        int tm_mday;     /* Day of the month (1-31) */
        int tm_mon;      /* Month (0-11) */
        int tm_year;     /* Year - 1900;从1900年算起,至今的年份 */
        int tm_wday;     /* Day of the week (0-6, Sunday = 0) */
        int tm_yday;     /* Day in the year (0-365, 1 Jan = 0) */
        int tm_isdst;    /* Daylight saving time;干啥用的??? */
    };
    
    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; 时间的修正方式 */
    };
    
    

     

  3. 时间与时区:

    1. 整个地球分为24个时区,每个时区都有自己的本地时间。

    2. UTC时间与本地时间:

      1. UTC时间是世界协调时间。

      2. 本地时间 = UTC时间 + 时区差

    3. 时区差:东为正,西为负。东八区的时区差即为+0800。

    4. Unix时间戳:从1970-01-01 00:00:00开始计算至今的秒数。

  4. 与时间相关的函数:

    1. time

      1. 作用: 返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数

      2. 特点:与时区无关。

    2. gmtime

      1. 作用:将time()返回的秒数,转换至struct tm结构中。

      2. 特点:不进行时区转换。

    3. ctime

      1. 作用:将time()返回的秒数,转换成可读的时间字符串。

      2. 特点:已进行时区转换。

    4. asctime

      1. 作用:将gmtime返回的struct tm,转换成可读的时间字符串。

      2. 特点:不进行时区转换。

    5. gettimeofday

      1. 作用:

        1. struct timeval: 获取自1970-01-01 00:00:00至今的描述,其值与time()的返回值相等。

        2. struct timezone: 返回当前的时区信息。tz_minuteswest表示与UTC +0000相差的分钟数,其值为-480时,表示东八区。

      2. 特点:秒数与时区无关。

    6. localtime

      1. 作用:将time()返回的秒数,转换至struct tm结构中。

      2. 特点:已进行时区转换。

    7. mktime

      1. 作用:将struct tm结构,转换至秒数。

      2. 特点:已进行时区转换。

    8. settimeofday

      1. 作用:设置当前的时间和时区。

      2. 特点:

        1. 只有root用户有权限执行。

        2. 只对gettimeofday()有影响,对localtime()无影响。

    9. strftime

      1. 将struct tm结构中的时间,根据格式化字符串,转换为可读的时间字符串。

      2. 格式化字符串:

        1. %a: 英文单词中星期几的缩写版。如:星期三,表示为"Wed"。

        2. %A: 英文单词中星期几的完整版。如:星期三,表示为"Wednesday"。

        3. %b: 英文单词中月份的缩写版。如:11月,表示为"Nov"。

        4. %B: 英文单词中月份的缩写版。如:11月,表示为"November"。

        5. %c: 格式化的时间字符串。如:2018-11-28 10:13:40,表示为"Wed Nov 28 10:13:40 2018"。

        6. %F: 日期格式为yyyy-mm-dd,与%Y:%m:%d作用相同。如:2018-11-28。

        7. %X: 时间格式为hh:mm:ss。如:10:13:40。

        8. %j: 一年中的第几天,范围:001-366.

        9. %W: 一年中的第几周,范围:00-53.

        10. %Y:  日期中的年,如:2018。

        11. %m: 日期中的月,范围:00-12。

        12. %d:  日期中的天,范围:01-31。

        13. %H:  小时,范围:00-24。

        14. %M: 分钟,范围:00-59。

        15. %S:  秒,    范围:00-60。

  5. 与时间相关的文件:

    1. /etc/localtime:

      1. 查看该文件的内容,使用zdump /etc/localtime命令。

      2. 该文件中记录了当前时区的时间。

      3. 通过将/usr/share/zoneinfo目录下某个时区的文件替换/etc/localtime文件,可以将当前时区修改为对应文件的时区。

  6. 与时间有关的命令:

    1. date

    2. hwclock(需要root权限)

      1. 参考:https://blog.csdn.net/liufuchun111/article/details/84582114

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值