时间

shell操作时间

  1 date        #查看或者设置系统时钟                                                                                                                              
  2 cal        
  3 ncal
  4 calendar
  5 time cal
<span style="font-size:14px;color:#333333;background-color:#FFFFFF;line-height:25px;white-space:pre-wrap;">system-config-date       #图形化修改</span>

hwclock           #硬件时间 查看设置与同步


</pre></p><p></p><p>1,获取和转换日期时间(Calendar Time)</p><p>系统调用:gettimeofday(),time(),ctime(),gmtime(),localtime(),mktime(),asctime().</p><p><pre name="code" class="objc"> 19 #include <locale.h>
 20 #include <time.h>
 21 #include <sys/time.h>
 22 #include <stdio.h>
 23 #include <stdlib.h>
 24 #define SECONDS_IN_TROPICAL_YEAR (365.24219*24*60*60)
 25 #define S_Y SECONDS_IN_TROPICAL_YEAR
 26 
 27 int 
 28 main(int argc,char *argv[])
 29 {
 30     time_t t;
 31     struct tm *gmp, *locp;
 32     struct tm gm,loc;
 33     struct timeval tv;
 34 
 35     t = time(NULL);
 36     printf("Seconds since the Epoch(1 Jan 1970):%ld\t", (long)t);
 37     printf("(about %6.3f years)\n", t/S_Y);
 38 
 39     if (gettimeofday(&tv, NULL) == -1)
 40       exit(EXIT_FAILURE);
 41     printf("    gettimeofday() return %ld secs,%ld microsecs\n", 
 42                 (long) tv.tv_sec, (long) tv.tv_usec);
 43 
 44     gmp = gmtime(&t);                                                                                                                    
 45     if (gmp == NULL)
 46       exit(-1);
 47     gm = *gmp;    /*save local copy, since *gmp may be modified by asctime() or gmtime() */
 48     printf("Broken down by gmtime():\n");
 49     printf("    year:mon:mday:hour:min:sec = %d %d %d %d %d %d %d\t", 
 50                 gm.tm_year, gm.tm_mon, gm.tm_mday, gm.tm_hour, gm.tm_min, gm.tm_sec);
 51     printf("wday:yday:isdst = %d %d %d\n",
 52                 gm.tm_wday, gm.tm_yday, gm.tm_isdst);
 53 
 54     locp = localtime(&t);
 55     if (locp == NULL)
 56       exit(-1);
 57     loc = *locp;    /*save local copy*/
 58     printf("Broken down by localtime():\n");
 59     printf("    year:mon:mday:hour:min:sec = %d %d %d %d %d %d %d\t", 
 60                 loc.tm_year, loc.tm_mon, loc.tm_mday, loc.tm_hour, loc.tm_min, loc.tm_sec);
 61     printf("wday:yday:isdst = %d %d %d\n",
 62                 loc.tm_wday, loc.tm_yday, loc.tm_isdst);
 63 
 64     printf("asctime() formats the gmtime() value as:%s", asctime(&gm));
 65     printf("ctime() formats the time() value as:    %s", ctime(&t));
 66 
 67     printf("mktime() of gmtime() value:    %ld secs\n", (long)mktime(&gm));
 68     printf("mktime() of localtime() value: %ld secs\n", (long)mktime(&loc));
 69     return 0;
 70 }
 71 

#output
 Seconds since the Epoch(1 Jan 1970):1458092949    (about 46.205 years)
    gettimeofday() return 1458092949 secs,76250 microsecs
Broken down by gmtime():
    year:mon:mday:hour:min:sec = 116 2 16 1 49 9 -1437482552    wday:yday:isdst = 3 75 0
Broken down by localtime():
    year:mon:mday:hour:min:sec = 116 2 16 9 49 9 -1437482552    wday:yday:isdst = 3 75 0
asctime() formats the gmtime() value as:Wed Mar 16 01:49:09 2016
ctime() formats the time() value as:    Wed Mar 16 09:49:09 2016
mktime() of gmtime() value:    1458064149 secs
mktime() of localtime() value: 1458092949 secs

2、返回当前时间的字符串

系统调用:strftime(),currTime()

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE 1000

char *
currTime(const char *format)
{
    static char buf[BUF_SIZE];    /* Nonreentrant*/
    time_t t;
    size_t s;
    struct tm *tm;

    t = time(NULL);
    tm = localtime(&t);
    if (tm == NULL)
      return NULL;

    s = strftime(buf, BUF_SIZE, (format != NULL) ? format : "%c", tm);

    return (s == 0) ? NULL : buf;
}

3、获取和转换日历时间

系统调用:strptime()

 19 #define _XOPEN_SOURCE
 20 #include <time.h>
 21 #include <locale.h>
 22 #include <stdlib.h>
 23 #include <stdio.h>
 24 
 25 #define SBUF_SIZE 1000
 26 
 27 int
 28 main(int argc, char *argv[])
 29 {
 30     struct tm tm;
 31     char sbuf[SBUF_SIZE];
 32     char *ofmt;
 33 
 34     if (argc < 3 || strcmp(argv[1], "--help") == 0){
 35         printf("%s input-date-time in-format [out-foramt]\n", argv[0]);
 36         exit(-1);
 37     }
 38 
 39     if(setlocale(LC_ALL,"") ==NULL)
 40       exit(-1);
 41 
 42     memset(&tm, 0, sizeof(struct tm));
 43     if (strptime(argv[1],argv[2], &tm) == NULL)
 44       exit(-1);
 45 
 46     tm.tm_isdst = -1;
 47     printf("calendar time (seconds since Epoch): %ld\n", (long) mktime(&tm));
 48 
 49     ofmt = (argc > 3) ? argv[3] :"%H:%M:%S %A, %d %B %Y %Z";
 50     if (strftime(sbuf, SBUF_SIZE, ofmt, &tm) == 0)
 51       exit(-1);
 52     printf("strftime() yields: %s\n", sbuf);
 53 
 54     return 0;
 55 }

#output
$ ./a.out  "9:39:46pm 1 Feb 2016" "%I:%M:%S%p %d %b %Y"
calendar time (seconds since Epoch): 1454333986
strftime() yields: 21:39:46 星期一, 01 二月 2016 CST

$ ./a.out  "9:39:46pm 1 Feb 2016" "%I:%M:%S%p %d %b %Y" "%F %T"
calendar time (seconds since Epoch): 1454333986
strftime() yields: 2016-02-01 21:39:46

4,时区

系统调用:setlocale(),settimeofday(),adjtime(),

5、软件时钟

可配置的内核选项,常见的300赫兹可以被视频帧速度25帧每秒(PAL)和30(NTSC)整除,250赫兹(默认)

6、进程时间

系统调用:times(),clock()。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值