初步认识和使用C语言时间日期函数

在C语言的头文件time.h中,定义了日期和时间操作的数据类型和操作。

在此处,我们参考MinGW(Minimalist GNU for Windows)中的time.h。

首先我们看看time.h所声明的数据类型:

 

 1 typedef    long    clock_t;
 2 
 3 typedef __int32 __time32_t;
 4 typedef    __time32_t time_t;
 5 
 6 /*
 7  * A structure for storing all kinds of useful information about the
 8  * current (or another) time.
 9  */
10 struct tm
11 {
12     int    tm_sec;        /* Seconds: 0-59 (K&R says 0-61?) */
13     int    tm_min;        /* Minutes: 0-59 */
14     int    tm_hour;    /* Hours since midnight: 0-23 */
15     int    tm_mday;    /* Day of the month: 1-31 */
16     int    tm_mon;        /* Months *since* january: 0-11 */
17     int    tm_year;    /* Years since 1900 */
18     int    tm_wday;    /* Days since Sunday (0-6) */
19     int    tm_yday;    /* Days since Jan. 1: 0-365 */
20     int    tm_isdst;    /* +1 Daylight Savings Time, 0 No DST,
21                  * -1 don't know */
22 };

clock_t 是毫秒数。

time_t 是秒数。

struct tm是存放当前时间和日期信息的结构体。

 

然后我们来看看各个函数的声明和作用。(参考《C程序设计语言》(第二版))

  1. clock_t clock( void ) :返回程序开始执行以后相应的进程占用处理器的时间(毫秒数),我们可以使用clock()/CLOCKS_PER_SEC来得到秒数。其中宏CLOCKS_PER_SEC就是1000。
  2. time_t time(time_t *tp) :返回当前的时间距离1970年1月1日0时的秒数,同时把这个秒数赋值给tp指向的地址。如果不能获得时间,则返回-1。
  3. double difftime(time_t time2, time_t time1) :返回time2和time1相差的秒数。
  4. time_t mktime(struct tm *tp) :读入tp所指向的struct tm类型,返回当前的时间距离1970年1月1日0时的秒数。如果不能表示,则返回-1。
  5. char *asctime(const struct tm *tp) :将tp指向的结构转换为字符串形式,如Fri Nov 07  11:41:56 2014。此函数返回一个指针,其指向一块静态的缓存,可能被其他调用所覆盖。下面6,7,8说明的函数的返回值也有同样的问题。
  6. char *ctime(const time_t *tp) :将tp指向的时间转换为字符串形式。它等价于asctime(localtime(tp))。
  7. struct tm *localtime(const time_t *tp):将tp指向的时间转换当地时间,数据为struct tm结构,返回一个指向静态缓存的指针(可能被其他调用所覆盖,特别是gmtime(),请注意!)
  8. struct tm *gmtime(const time_t *tp) :将tp指向的时间转换为当地时间,数据为struct tm结构,返回一个指向静态缓存的指针(可能被其他调用所覆盖,特别是localtime(),请注意!)
  9. size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp) :函数根据fmt中的格式把tp所指向的时间结构转化为字符串,复制在s指向的内存中,最多smax个字符写入到s中,包括'\0'。如果fmt格式对应的内容加上'\0'超过smax,则函数返回0。否则函数返回复制的字符个数,不包括'\0'。如fmt为"%a"时,应该向s写入“Fri”,如果smax大于等于4,则函数返回3,否则复制失败,函数返回0。fmt的格式说明见《C程序设计语言》(第二版)的第235页。

简单的代码来调用上述函数:

 1 #include <time.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 #define ROUND_TO_INT(x) ((int)((x) + (((x)>0)?0.5:-0.5)))
 6 
 7 int
 8 main(int argc, char** argv)
 9 {
10     // time() 
11     time_t t1;
12     time_t* tp;
13     tp = (time_t*)malloc(sizeof(time_t));
14     t1 = time(tp);
15     printf("当前日历时间(距离1970年1月1日0时的秒数):%u\n", t1); 
16     // time(), difftime()
17     time_t t2;
18     t2 = time(tp);
19     double difft = difftime(t2, t1);
20     printf("当前日历时间(距离1970年1月1日0时的秒数):%u,两次日历时间的差值:%f(秒)\n", t2, difft); 
21     // localtime(),struct tm , asctime(), ctime() 
22     // mktime(),将struct tm转换为time_t类型
23     struct tm *localtp;
24     localtp = localtime(tp);
25     printf("是否夏令时:%d,距离1月1日:%d(天),距离星期天:%d(天),距离1900年:%d年,\n"
26            "距离1月:%d(月),今天是:%d日%d时%d分%d秒\n", localtp->tm_isdst, localtp->tm_yday, 
27            localtp->tm_wday, localtp->tm_year, localtp->tm_mon, localtp->tm_mday, localtp->tm_hour,
28            localtp->tm_min, localtp->tm_sec); 
29     printf("以asctime呈现本地时间:%s", asctime(localtp));
30     printf("以ctime呈现本地时间:%s", ctime(tp)); // ctime(tp) == asctime(localtime(tp)) 
31     time_t t3 = mktime(localtp);
32     printf("当前日历时间(距离1970年1月1日0时的秒数):%u\n", t3);
33     // gmtime(),当我们使用 gmtime()之后,其实改变的是localtp所指向的地址。
34     // 因为asctime(), ctime(), gmtime(), localtime()返回的是指向可被其他调用覆盖的静态对象的指针 
35     struct tm *gmtp;
36     gmtp = gmtime(tp);
37     printf("UTC时间:%s", asctime(gmtp)); 
38     time_t t4 = mktime(gmtp);
39     printf("当前日历时间(距离1970年1月1日0时的秒数):%u\n", t4);
40     printf("本地时间和UTC时间相差:%d(小时)\n", (int)(difftime(t3, t4)/3600)); 
41     // strftime() 
42     char* buf = (char*)malloc(1024);
43     if (buf == NULL) {
44         printf("Out of space!\n");
45         return -1;
46     }
47     int count; 
48     if ((count = strftime(buf, 1024, "%Z %Y年%m月%d日 %H时%M分%S秒", localtime(tp))) != 0) {
49         printf("格式化字数(中文算2个):%d\n格式化的时间:%s\n", count, buf); 
50     }
51     // clock() 
52     //sleep(1000);
53     int nclock = clock(); 
54     printf("程序开始执行后占用的处理器时间:%d(毫秒),约为%d(秒)\n", 
55                     nclock, ROUND_TO_INT((double)nclock/CLOCKS_PER_SEC));
56     system("pause");
57     return 0;
58 }

 

转载于:https://www.cnblogs.com/nipan/p/4080969.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值