显示当前时间和日期


#include <time.h>

time_t time(time_t *tloc)

time_t 长整型32位

通过调用time函数可以得到时间值,返回的是从纪元(1970年1月1日0点)开始至今的秒数。如果tloc不是一个空指针,time函数还会把返回值写入tloc指针指向的位置。


double difftime(time_t time1,time_t time2)

difftime函数计算两个时间值之间的差,并将time1-time2的值作为浮点数返回。


struct tm * gmtime(const time_t *timeval);

时间值转换为可读的时间和日期,返回的时间日期未经时区转换,而是UTC时间

struct tm

{

  int tm_sec,         秒  0-61

  int tm_min,         分  0-59

  int tm_hour ,       小时 0-23  

  int tm_mday,       月份中的日期  1-31

  int tm_mon,         月份 0-11    一月份为0

  int tm_year,         从1900年开始计算的年份

  int tm_wday,        星期几  0-6   周日为0

  int tm_yday,         年份中的日期  0-365

  int tm_isdst,         是否夏令时

}


小知识:

夏时制(Daylight Saving Time:DST),又称“日光节约时制”和“夏令时间”,是一种为节约能源而人为规定地方时间的制度,在这一制度实行期间所采用的统一时间称为“夏令时间”。


gmtime打印当前时间和日期

/*
 * gmtime.c
 *
 *  Created on: Jul 22, 2013
 *      Author: root
 */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
    time_t t;
    struct tm * tm_ptr=NULL;
    time(&t);
    tm_ptr=gmtime(&t);
    printf("Raw time is %ld\n",t);
    printf("gmtime:\n");
    printf("date:%2d/%2d/%2d\n",tm_ptr->tm_year,tm_ptr->tm_mon+1,tm_ptr->tm_mday);
    printf("time:%2d:%2d:%2d\n",tm_ptr->tm_hour,tm_ptr->tm_min,tm_ptr->tm_sec);
    return 0;
}

运行

[root@localhost C_test]# gcc -o gmtime gmtime.c
[root@localhost C_test]# ./gmtime
Raw time is 1374515025
gmtime:
date:113/ 7/22
time:17:43:45
[root@localhost C_test]# date
Tue Jul 23 01:45:11 CST 2013

发现两个时间不一致,原因是gmtime返回的是UTC时间


struct tm * localtime(const time_t * timeval);

查看当地时间,localtime函数和gmtime一样,除了返回的结构中包含的值已根据当地时区和是否采用夏令时做了调整。



localtime打印当前时间和日期

/*
 * localtime.c
 *
 *  Created on: Jul 22, 2013
 *      Author: root
 */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
    time_t t;
    struct tm * tm_ptr=NULL;
    time(&t);
    tm_ptr=localtime(&t);
    printf("date:%2d/%2d/%2d\n",tm_ptr->tm_year,tm_ptr->tm_mon+1,tm_ptr->tm_mday);
    printf("time:%d:%d:%d\n",tm_ptr->tm_hour,tm_ptr->tm_min,tm_ptr->tm_sec);
    return 0;
}

运行

[root@localhost C_test]# ./localtime ;date
date:113/ 7/23
time:1:56:51
Tue Jul 23 01:56:51 CST 2013

发现时间一致了!



time_t mktime(struct tm * timeptr)

把分解出来的tm结构转换为原始的time_t时间值,如果不能被为time_t,mktime返回-1


为了更友好得表示时间和日期,可以使用asctime和ctime函数

char * asctime(const struct tm * timeptr)

char * ctime(const time_t * timeval)


asctime函数返回一个字符串,表示由tm结构timeptr给出的时间和日期。这个返回的字符串类似下面的格式

Sun Jun 9 12:34:56 2007\n\0

总是这种长度为26个字符的固定格式。


ctime函数等效于调用下面这个函数

asctime(localtime(timeval))

以原始时间值为参数,并将它转换为一个更易读的本地时间。


/*
 * ctime.c
 *
 *  Created on: Jul 22, 2013
 *      Author: root
 */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
    time_t t;
    time(&t);
    char * p=ctime(&t);
    printf("the local time:%s",p);
    return 0;
}


运行

[root@localhost C_test]# date;./ctime
Tue Jul 23 07:43:22 CST 2013
the local time:Tue Jul 23 07:43:22 2013



size_t  strftime(char *s,size_t maxsize,const char * format,struct tm * timeptr)

strftime函数格式化timeptr指针指向的tm结构表示的时间和日期,并将结果放在字符串s中。

字符串被指定至少maxsize字符长。format字符串用于控制写入字符串s的字符。与printf一样,它包含被传给字符串的普通字符和用于格式化时间和日期元素的转换控制符。



char * strptime(const char * buf,const char * format,struct tm * timeptr)

format字符串的构建方式和strftime的format字符串完全一样。strptime在字符串扫描方面类似sscanf函数,查找可识别字段,并把它们写入对应的变量中。这里根据format字符串来填充tm结构的成员。strptime中的星期几和月份用缩写和全称都行,strftime对小于10的数字总以0开头,而strptime则把它看作是可选的。


/*
 * strftime.c
 *
 *  Created on: Jul 22, 2013
 *      Author: root
 */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
    struct tm * tm_ptr;
    time_t t;
    char buf[256];
    time(&t);
    tm_ptr=localtime(&t);
   strftime(buf,256,"%A %d %B,%I %S %p",tm_ptr);
   printf("strftime:%s",buf);
    return 0;
}


运行

[root@localhost C_test]# ./strftime
strftime:Tuesday 23 July,09 33 AM











你可能感兴趣的:(time)