time的使用

时间函数使用如time,ctime,gmtime_r,localtime_r,gettimeofday,还有一个sysinfo,可以获取开机之后的秒数,更多的可以查看sysinfo结构体

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

int main(int argc, char const *argv[])
{
    time_t time_t1 = -1;
    struct tm tm1;
    struct timeval timeval1;

    time_t1 = time(NULL);                      //获取1970年至今的秒数
    printf("time_t1 = %ld\n", time_t1);

    printf("ctime: %s\n", ctime(&time_t1));   //把秒数转换为日历时间calender time
    memset(&tm1, 0, sizeof(tm1));

    gmtime_r(&time_t1, &tm1);                 //把秒数转换为UTC时间 
    printf("utc hour = %d\n", tm1.tm_hour);
    memset(&tm1, 0, sizeof(tm1));

   localtime_r(&time_t1, &tm1);               //把秒数转换为本地时间
    printf("local hour = %d\n", tm1.tm_hour);
    memset(&timeval1, 0, sizeof(timeval1));

    gettimeofday(&timeval1, NULL);           //获取1970年至今的秒数和微秒数
    printf("sec = %ld, usec = %ld\n", timeval1.tv_sec, timeval1.tv_usec);

    sysinfo(&sysinfo1);
    printf("boot time = %ld\n", sysinfo1.uptime);  //开机到目前为止经过的秒数

    return 0;
}

out:
time_t1 = 1506002179
ctime: Thu Sep 21 21:56:19 2017

utc hour = 13
local hour = 21
sec = 1506002179, usec = 157858
boot time = 1137

tm结构体和sysinfo结构体:

           
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 */
               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 sysinfo {
               long uptime;             /* Seconds since boot */
               unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
               unsigned long totalram;  /* Total usable main memory size */
               unsigned long freeram;   /* Available memory size */
               unsigned long sharedram; /* Amount of shared memory */
               unsigned long bufferram; /* Memory used by buffers */
               unsigned long totalswap; /* Total swap space size */
               unsigned long freeswap;  /* swap space still available */
               unsigned short procs;    /* Number of current processes */
               char _f[22];             /* Pads structure to 64 bytes */
           };

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值