探秘C time.h

来源:互联网 发布:mac 迅雷 bt 速度0 编辑:程序博客网 时间:2024/05/07 01:59

前言

这是现阶段探秘系列的最后一个库了,前前后后我们也经历了这么多时间,花了很多的精力,不敢说精研C 标准库,只能说确实有所了解,有所收获。期间看过英文原版的书,下过glibc 的源代码。中间还敢上了金工实习,再网吧还写了博客。今天暂时结束了,但是还是那句话“C 无所不能,只是我们的能力还不行。

__start

在开发UNIX的时候,时间和日期已经都达到了一个新的水平,有几个开发者是天文爱好者。他们希望可以表示几十年的时间而不是,仅仅几年的时间,所以他们自发使用了格林尼治时间。

 

time.h 的内容

头文件定义了两个宏,声明了四种类型和几个操作时间的函数。很多函数都处理表示当前时间和时间的日历时间。有些函数处理本地时间和夏令时。本地时区和夏令时都是实现定义的。

   定义的宏有:

NULL          ((void *) -1)

CLOCKS_PER_SEC          它是函数clock 的返回值的每秒的时间单位数

size_t           unsigned long int

clock_t        time_t         他们是可以表示时间的算术类型

struct   tm     这个结构体保存了一个日历时间的各个组成部分,日历时间被称为细分时间。这个结构体至少应该按照某种顺序包含以下成员。

[c]

/* Used by other time functions. */
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) */
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] */
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] */
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1]*/

# ifdef __USE_BSD
long int tm_gmtoff; /* Seconds east of UTC. */
const char *tm_zone; /* Timezone abbreviation. */
# else
long int __tm_gmtoff; /* Seconds east of UTC. */
const char *__tm_zone; /* Timezone abbreviation. */
# endif
};

[/c]

很简单,一眼就能看明白这个结构体里面装的是什么。

  时间操纵函数

#include<time.h>

clock_t  clock ( void );

函数clock 确定处理器使用的时间。这个计算的是从进程开始到使用CPU 的时间,这个是时钟时间,如果需要知道秒数就必须使用返回之除以CLOCK_PER_SEC 即可。如果不能获得使用的处理器时间或者这个值不能表示,则函数返回(clock_t ) -1^138.

double difftime( time_t timel,time_t time0);

这个函数计算两个日历时间之差。

函数difftime 返回以秒为单位的double 类型的差值。

#include<time.h>

time_t   mktime(struct tm *timeptr);

用来将时间转换为从1970 年 1月 1日到现在的UTC 时间。这个函数还有一个其他的用法,就是把timeptr 指向的结构中的表示为本地时间的细分时间转换成一个日历时间。结构成员tm_wday 和 tm_yday 的初始值先不管。其他成员不受限制,如果成功则tm_wday 和 tm_yday 被正确设置,同时其他的成员也被设置用来表示指定的日历时间,但是他们的值必须再上面说明的范围内,直到确定了tm-mon 和 tm_year 后才设置tm_mday 的最终值。

比如计算2015 年 11月 26日是星期几?

[c]

&nbsp;

#include<stdio.h>
#include<time.h>

static const char *const wday[] = {
"sunday","monday","tuesday","wednesday",
"thursday","friday","saturday","-unknow-"
};
int main(){
struct tm time_str;
time_str.tm_year = 2015 -1900;
time_str.tm_mon = 11 -1;
time_str.tm_mday = 26;
time_str.tm_hour = 0 ;
time_str.tm_min = 0 ;
time_str.tm_sec = 1 ;
time_str.tm_isdst = -1 ;
if(mktime(&time_str) == -1)
time_str.tm_wday = 7;
printf("%s\n",wday[time_str.tm_wday]);
}

[/c]

#include<time.h>

time_t  time(time_t *timer);

函数time 确定当前日历时间,其实就是获得当前系统时间,可以先声明一个time_t 的东西,然后在使用这个函数,将当前时间静态的保存在这个结构体中。

时间转换函数

#include  <time.h>

char *asctime(const struct tm *timeptr);

函数把timeptr 指向的结构表示的细分时间转换成以下形式的串。

函数简单实现如下:

[c]

&nbsp;

char *asctime(const struct tm *timeptr){
static const char wday_name[7][4]={
"sun","Mon","Tue","Wed","Thu","Fir","Sat"
};

static const char mon_name[12][4] = {
"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"
};

static char result[26];
sprintf(result,"%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
wday_name[timeptr->tm_wday],
mon_name[timeptr->tm_mon],
timeptr->tm_min,timeptr->tm_hour,
timeptr->tm_min,timeptr->tm_sec,
1900 + timeptr->tm_year);
return result;
}

[/c]

#include < time.h >

char * ctime(const time_t *timer);

这个函数把timer 转换成串的时间,等价于:

asctime(localtime  (timer) );

返回值ctime 返回 asctime 把那个细分时间作为参数时返回指针。

 

#include < time.h>

struct tm * gmtime(const time_t  *timer);

把timer 指向的日历时间转换成协调世界时间,就是真实的时间。

 

#include < time.h>

struct tm *loacltime(const time_t  *timer);

函数把timer 指向的日历时间转换成表示本地时间的细分时间,获得的式系统时间未经过时区转换

<time.h>的使用

类型clock_t  表示程序使用的处理器时间,作为函数clock 的返回类型

类型time_t   表示日历时间,作为函数time 或者 函数mktime 的返回类型

类型double   用秒表示日历时间,作为difftime 的返回类型

类型struct tm ,表示分成几个独立的组成部分的日历时间,作为函数gmtime 和 localtime 的返回类型。

类型char * ,表示日历时间,作为函数asctime,ctime 和 strftime 的返回类型。

 

查看原文:http://zmrlinux.com/2015/12/03/%e6%8e%a2%e7%a7%98c-time-h/

0 0