C语言中时间的函数

Python018

C语言中时间的函数,第1张

一.概念

在C/C++中,通过学习许多C/C++库,你可以有很多操作、使用时间的方法。但在这之前你需要了解一些“时间”和“日期”的概念,主要有以下几个:

1. 协调世界时,又称为世界标准时间,也就是大家所熟知的格林威治标准时间(Greenwich Mean Time,GMT)。比如,中国内地的时间与UTC的时差为+8,也就是UTC+8。美国是UTC-5。

2. 日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。这个标准时间点对不同的编译器来说会有所不同,但对一个编译系统来说,这个标准时间点是不变的,该编译系统中的时间对应的日历时间都通过该标准时间点来衡量,所以可以说日历时间是“相对时间”,但是无论你在哪一个时区,在同一时刻对同一个标准时间点来说,日历时间都是一样的。

3. 时间点。时间点在标准C/C++中是一个整数,它用此时的时间和标准时间点相差的秒数(即日历时间)来表示。

4. 时钟计时单元(而不把它叫做时钟滴答次数),一个时钟计时单元的时间长短是由CPU控制的。一个clock tick不是CPU的一个时钟周期,而是C/C++的一个基本计时单位。

我们可以使用ANSI标准库中的time.h头文件。这个头文件中定义的时间和日期所使用的方法,无论是在结构定义,还是命名,都具有明显的C语言风格。下面,我将说明在C/C++中怎样使用日期的时间功能。

二. 介绍

1. 计时

C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:

clock_t clock( void )

这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock)。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对它的定义:

#ifndef _CLOCK_T_DEFINED

typedef long clock_t

#define _CLOCK_T_DEFINED

#endif

很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:

#define CLOCKS_PER_SEC ((clock_t)1000)

可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间:

void elapsed_time()

{

printf("Elapsed time:%u secs. ",clock()/CLOCKS_PER_SEC)

}

当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:

/* 测量一个事件持续的时间*/

/* Date : 10/24/2007 */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main( void )

{

long i = 10000000L

clock_t start, finish

double duration

/* 测量一个事件持续的时间*/

printf( "Time to do %ld empty loops is ", i )

start = clock()

while( i-- )

finish = clock()

duration = (double)(finish - start) / CLOCKS_PER_SEC

printf( "%f seconds ", duration )

system("pause")

}

在笔者的机器上,运行结果如下:

Time to do 10000000 empty loops is 0.03000 seconds

上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C/C++中,最小的计时单位是一毫秒。

2.与日期和时间相关的数据结构

在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:

#ifndef _TM_DEFINED

struct tm {

int tm_sec/* 秒 – 取值区间为[0,59] */

int tm_min/* 分 - 取值区间为[0,59] */

int tm_hour/* 时 - 取值区间为[0,23] */

int tm_mday/* 一个月中的日期 - 取值区间为[1,31] */

int tm_mon/* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */

int tm_year/* 年份,其值等于实际年份减去1900 */

int tm_wday/* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */

int tm_yday/* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */

int tm_isdst/* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0不了解情况时,tm_isdst()为负。*/

}

#define _TM_DEFINED

#endif

ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)。

而日历时间(Calendar Time)是通过time_t数据类型来表示的,用time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数。在time.h中,我们也可以看到time_t是一个长整型数:

#ifndef _TIME_T_DEFINED

typedef long time_t/* 时间值 */

#define _TIME_T_DEFINED /* 避免重复定义 time_t */

#endif

大家可能会产生疑问:既然time_t实际上是长整型,到未来的某一天,从一个时间点(一般是1970年1月1日0时0分0秒)到那时的秒数(即日历时间)超出了长整形所能表示的数的范围怎么办?对time_t数据类型的值来说,它所表示的时间不能晚于2038年1月18日19时14分07秒。为了能够表示更久远的时间,一些编译器厂商引入了64位甚至更长的整形数来保存日历时间。比如微软在Visual C++中采用了__time64_t数据类型来保存日历时间,并通过_time64()函数来获得日历时间(而不是通过使用32位字的time()函数),这样就可以通过该数据类型保存3001年1月1日0时0分0秒(不包括该时间点)之前的时间。

在time.h头文件中,我们还可以看到一些函数,它们都是以time_t为参数类型或返回值类型的函数:

double difftime(time_t time1, time_t time0)

time_t mktime(struct tm * timeptr)

time_t time(time_t * timer)

char * asctime(const struct tm * timeptr)

char * ctime(const time_t *timer)

此外,time.h还提供了两种不同的函数将日历时间(一个用time_t表示的整数)转换为我们平时看到的把年月日时分秒分开显示的时间格式tm:

struct tm * gmtime(const time_t *timer)

struct tm * localtime(const time_t * timer)

通过查阅MSDN,我们可以知道Microsoft C/C++ 7.0中时间点的值(time_t对象的值)是从1899年12月31日0时0分0秒到该时间点所经过的秒数,而其它各种版本的Microsoft C/C++和所有不同版本的Visual C++都是计算的从1970年1月1日0时0分0秒到该时间点所经过的秒数。

3.与日期和时间相关的函数及应用

在本节,我将向大家展示怎样利用time.h中声明的函数对时间进行操作。这些操作包括取当前时间、计算时间间隔、以不同的形式显示时间等内容。

4. 获得日历时间

我们可以通过time()函数来获得日历时间(Calendar Time),其原型为:

time_t time(time_t * timer)

如果你已经声明了参数timer,你可以从参数timer返回现在的日历时间,同时也可以通过返回值返回现在的日历时间,即从一个时间点(例如:1970年1月1日0时0分0秒)到现在此时的秒数。如果参数为空(NUL),函数将只通过返回值返回现在的日历时间,比如下面这个例子用来显示当前的日历时间:

运行的结果与当时的时间有关,我当时运行的'结果是:

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

time_t lt

lt =time(NULL)

printf("The Calendar Time now is %d ",lt)

return 0

}

The Calendar Time now is 1122707619

其中1122707619就是我运行程序时的日历时间。即从1970-01-01 08:00:00到此时的秒数。

5. 获得日期和时间

这里说的日期和时间就是我们平时所说的年、月、日、时、分、秒等信息。从第2节我们已经知道这些信息都保存在一个名为tm的结构体中,那么如何将一个日历时间保存为一个tm结构的对象呢?

其中可以使用的函数是gmtime()和localtime(),这两个函数的原型为:

struct tm * gmtime(const time_t *timer)

struct tm * localtime(const time_t * timer)

其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将日历时间转化为本地时间。比如现在用gmtime()函数获得的世界标准时间是2005年7月30日7点18分20秒,那么我用localtime()函数在中国地区获得的本地时间会比世界标准时间晚8个小时,即2005年7月30日15点18分20秒。下面是个例子:

//本地时间,世界标准时间

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

struct tm *local

time_t t

t=time(NULL)

local=localtime(&t)

printf("Local hour is: %d:%d:%d ",local->tm_hour,local->tm_min,local->tm_sec)

local=gmtime(&t)

printf("UTC hour is: %d:%d:%d ",local->tm_hour,local->tm_min,local->tm_sec)

return 0

}

运行结果是:

Local hour is: 23:17:47

UTC hour is: 15:17:47

6. 固定的时间格式

我们可以通过asctime()函数和ctime()函数将时间以固定的格式显示出来,两者的返回值都是char*型的字符串。返回的时间格式为:

星期几 月份 日期 时:分:秒 年

例如:Wed Jan 02 02:03:55 1980

其中 是一个换行符,是一个空字符,表示字符串结束。下面是两个函数的原型:

char * asctime(const struct tm * timeptr)

char * ctime(const time_t *timer)

其中asctime()函数是通过tm结构来生成具有固定格式的保存时间信息的字符串,而ctime()是通过日历时间来生成时间字符串。这样的话,asctime()函数只是把tm结构对象中的各个域填到时间字符串的相应位置就行了,而ctime()函数需要先参照本地的时间设置,把日历时间转化为本地时间,然后再生成格式化后的字符串。在下面,如果t是一个非空的time_t变量的话,那么:

printf(ctime(&t))

等价于:

struct tm *ptr

ptr=localtime(&t)

printf(asctime(ptr))

那么,下面这个程序的两条printf语句输出的结果就是不同的了(除非你将本地时区设为世界标准时间所在的时区):

//本地时间,世界标准时间

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

struct tm *ptr

time_t lt

lt =time(NULL)

ptr=gmtime(<)

printf(asctime(ptr))

printf(ctime(<))

return 0

}

运行结果:

Sat Jul 30 08:43:03 2005

Sat Jul 30 16:43:03 2005

7. 自定义时间格式

我们可以使用strftime()函数将时间格式化为我们想要的格式。它的原型如下:

size_t strftime(

char *strDest,

size_t maxsize,

const char *format,

const struct tm *timeptr

)

我们可以根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。该函数返回向strDest指向的字符串中放置的字符数。

函数strftime()的操作有些类似于sprintf():识别以百分号(%)开始的格式命令集合,格式化输出结果放在一个字符串中。格式化命令说明串strDest中各种日期和时间信息的确切表示方法。格式串中的其他字符原样放进串中。格式命令列在下面,它们是区分大小写的。

%a 星期几的简写

%A 星期几的全称

%b 月分的简写

%B 月份的全称

%c 标准的日期的时间串

%C 年份的后两位数字

%d 十进制表示的每月的第几天

%D 月/天/年

%e 在两字符域中,十进制表示的每月的第几天

%F 年-月-日

%g 年份的后两位数字,使用基于周的年

%G 年分,使用基于周的年

%h 简写的月份名

%H 24小时制的小时

%I 12小时制的小时

%j 十进制表示的每年的第几天

%m 十进制表示的月份

%M 十时制表示的分钟数

%n 新行符

%p 本地的AM或PM的等价显示

%r 12小时的时间

%R 显示小时和分钟:hh:mm

%S 十进制的秒数

%t 水平制表符

%T 显示时分秒:hh:mm:ss

%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)

%U 第年的第几周,把星期日做为第一天(值从0到53)

%V 每年的第几周,使用基于周的年

%w 十进制表示的星期几(值从0到6,星期天为0)

%W 每年的第几周,把星期一做为第一天(值从0到53)

%x 标准的日期串

%X 标准的时间串

%y 不带世纪的十进制年份(值从0到99)

%Y 带世纪部分的十进制年份

%z,%Z 时区名称,如果不能得到时区名称则返回空字符。

%% 百分号

如果想显示现在是几点了,并以12小时制显示,就象下面这段程序:

//显示现在是几点了,并以12小时制显示

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

struct tm *ptr

time_t localTime

char str[80]

localTime=time(NULL)

ptr=localtime(&localTime)

strftime(str,100,"It is now %I %p ",ptr)

printf(str)

return 0

}

其运行结果为:

It is now 4PM

而下面的程序则显示当前的完整日期:

//显示当前的完整日期

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

void main( void )

{

struct tm *newtime

char tmpbuf[128]

time_t localTime1

time( &localTime1 )

newtime=localtime(&localTime1)

strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y. ", newtime)

printf(tmpbuf)

}

运行结果:

Today is Saturday, day 30 of July in the year 2005.

8. 计算持续时间的长度

有时候在实际应用中要计算一个事件持续的时间长度,比如计算打字速度。在第1节计时部分中,我已经用clock函数举了一个例子。Clock()函数可以精确到毫秒级。同时,我们也可以使用difftime()函数,但它只能精确到秒。该函数的定义如下:

double difftime(time_t time1, time_t time0)

虽然该函数返回的以秒计算的时间间隔是double类型的,但这并不说明该时间具有同double一样的精确度,这是由它的参数觉得的(time_t是以秒为单位计算的)。比如下面一段程序:

//计算持续时间的长度

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

time_t start,end

start = time(NULL)

system("pause")

end = time(NULL)

printf("The pause used %f seconds. ",difftime(end,start))//<-

system("pause")

return 0

}

运行结果为:

请按任意键继续. . .

The pause used 2.000000 seconds.

请按任意键继续. . .

可以想像,暂停的时间并不那么巧是整整2秒钟。其实,你将上面程序的带有“//<-”注释的一行用下面的一行代码替换:

printf("The pause used %f seconds. ",end-start)

其运行结果是一样的。

9. 分解时间转化为日历时间

这里说的分解时间就是以年、月、日、时、分、秒等分量保存的时间结构,在C/C++中是tm结构。我们可以使用mktime()函数将用tm结构表示的时间转化为日历时间。其函数原型如下:

time_t mktime(struct tm * timeptr)

其返回值就是转化后的日历时间。这样我们就可以先制定一个分解时间,然后对这个时间进行操作了,下面的例子可以计算出1997年7月1日是星期几:

//计算出1997年7月1日是星期几

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

struct tm time

time_t t_of_day

time.tm_year=1997-1900

time.tm_mon=6

time.tm_mday=1

time.tm_hour=0

time.tm_min=0

time.tm_sec=1

time.tm_isdst=0

t_of_day=mktime(&time)

printf(ctime(&t_of_day))

return 0

}

运行结果:

Tue Jul 01 00:00:01 1997

有了mktime()函数,是不是我们可以操作现在之前的任何时间呢?你可以通过这种办法算出1945年8月15号是星期几吗?答案是否定的。因为这个时间在1970年1月1日之前,所以在大多数编译器中,这样的程序虽然可以编译通过,但运行时会异常终止。

注:linux系统时间如果转换为 time_t 类型,都是从1970-01-01 08:00:00 开始计算

C语言的标准库函数包括一系列日期和时间处理函数,它们都在头文件中说明。

在头文件中定义了三种类型:time_t,struct tm和clock_t。

下面列出了这些函数。

time_t time(time_t *timer)

double difftime(time_t time1,time_t time2)

struct tm *gmtime(const time_t *timer)

struct tm *localtime(const time_t *timer)

char *asctime(const struct tm *timeptr)

char *ctime(const time_t *timer)

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

time_t mktime(struct tm *timeptr)

clock_t clock(void)

【具体应用举例】

asctime(将时间和日期以字符串格式表示)

相关函数

time,ctime,gmtime,localtime

表头文件

#i nclude

定义函数

char * asctime(const struct tm * timeptr)

函数说明

asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。

此函数已经由时区转换成当地时间,字符串格式为:"Wed Jun 30 21:49:08 1993\n"

返回值

若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。

附加说明

返回一字符串表示目前当地的时间日期。

范例

#i nclude

main()

{

time_t timep

time (&timep)

printf("%s",asctime(gmtime(&timep)))

}

执行

Sat Oct 28 02:10:06 2000

 

ctime(将时间和日期以字符串格式表示)

相关函数

time,asctime,gmtime,localtime

表头文件

#i nclude

定义函数

char *ctime(const time_t *timep)

函数说明

ctime ()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。

此函数已经由时区转换成当地时间,字符串格式为"Wed Jun 30 21 :49 :08 1993\n"。若再调用相关的时间日期函数,此字符串可能会被破坏。

返回值

返回一字符串表示目前当地的时间日期。

范例

#i nclude

main()

{

time_t timep

time (&timep)

printf("%s",ctime(&timep))

}

执行

Sat Oct 28 10 : 12 : 05 2000

 

gettimeofday(取得目前的时间)

相关函数

time,ctime,ftime,settimeofday

表头文件

#i nclude

#i nclude

定义函数

int gettimeofday ( struct timeval * tv , struct timezone * tz )

函数说明

gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。

timeval结构定义为:

struct timeval{

long tv_sec/*秒*/

long tv_usec/*微秒*/

}

timezone 结构定义为:

struct timezone{

int tz_minuteswest/*和Greenwich 时间差了多少分钟*/

int tz_dsttime/*日光节约时间的状态*/

}

上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下

DST_NONE /*不使用*/

DST_USA /*美国*/

DST_AUST /*澳洲*/

DST_WET /*西欧*/

DST_MET /*中欧*/

DST_EET /*东欧*/

DST_CAN /*加拿大*/

DST_GB /*大不列颠*/

DST_RUM /*罗马尼亚*/

DST_TUR /*土耳其*/

DST_AUSTALT /*澳洲(1986年以后)*/

返回值

成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。

范例

#i nclude

#i nclude

main(){

struct timeval tv

struct timezone tz

gettimeofday (&tv , &tz)

printf("tv_sec%d\n", tv,.tv_sec)

printf("tv_usec%d\n",tv.tv_usec)

printf("tz_minuteswest%d\n", tz.tz_minuteswest)

printf("tz_dsttime, %d\n",tz.tz_dsttime)

}

执行

tv_sec: 974857339

tv_usec:136996

tz_minuteswest:-540

tz_dsttime:0

 

gmtime(取得目前时间和日期)

相关函数

time,asctime,ctime,localtime

表头文件

#i nclude

定义函数

struct tm*gmtime(const time_t*timep)

函数说明

gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。

结构tm的定义为

struct tm

{

int tm_sec

int tm_min

int tm_hour

int tm_mday

int tm_mon

int tm_year

int tm_wday

int tm_yday

int tm_isdst

}

int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒

int tm_min 代表目前分数,范围0-59

int tm_hour 从午夜算起的时数,范围为0-23

int tm_mday 目前月份的日数,范围01-31

int tm_mon 代表目前月份,从一月算起,范围从0-11

int tm_year 从1900 年算起至今的年数

int tm_wday 一星期的日数,从星期一算起,范围为0-6

int tm_yday 从今年1月1日算起至今的天数,范围为0-365

int tm_isdst 日光节约时间的旗标

此函数返回的时间日期未经时区转换,而是UTC时间。

返回值

返回结构tm代表目前UTC 时间

范例

#i nclude

main(){

char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}

time_t timep

struct tm *p

time(&timep)

p=gmtime(&timep)

printf("%d%d%d",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday)

printf("%s%d%d%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec)

}

执行

2000/10/28 Sat 8:15:38

 

localtime(取得当地目前时间和日期)

相关函数

time, asctime, ctime, gmtime

表头文件

#i nclude

定义函数

struct tm *localtime(const time_t * timep)

函数说明

localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。

结构tm的定义请参考gmtime()。此函

数返回的时间日期已经转换成当地时区。

返回值

返回结构tm代表目前的当地时间。

范例

#i nclude

main(){

char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}

time_t timep

struct tm *p

time(&timep)

p=localtime(&timep)/*取得当地时间*/

printf ("%d%d%d ", (1900+p->tm_year),( l+p->tm_mon), p->tm_mday)

printf("%s%d:%d:%d\n", wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec)

}

执行

2000/10/28 Sat 11:12:22

 

mktime(将时间结构数据转换成经过的秒数)

相关函数

time,asctime,gmtime,localtime

表头文件

#i nclude

定义函数

time_t mktime(strcut tm * timeptr)

函数说明

mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。

返回值

返回经过的秒数。

范例

/* 用time()取得时间(秒数),利用localtime()

转换成struct tm 再利用mktine()将struct tm转换成原来的秒数*/

#i nclude

main()

{

time_t timep

strcut tm *p

time(&timep)

printf("time() : %d \n",timep)

p=localtime(&timep)

timep = mktime(p)

printf("time()->localtime()->mktime():%d\n",timep)

}

执行

time():974943297

time()->localtime()->mktime():974943297

 

settimeofday(设置目前时间)

相关函数

time,ctime,ftime,gettimeofday

表头文件

#i nclude

#i nclude

定义函数

int settimeofday ( const struct timeval *tv,const struct timezone *tz)

函数说明

settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。

注意,只有root权限才能使用此函数修改时间。

返回值

成功则返回0,失败返回-1,错误代码存于errno。

错误代码

EPERM 并非由root权限调用settimeofday(),权限不够。

EINVAL 时区或某个数据是不正确的,无法正确设置时间。

 

time(取得目前的时间)

相关函数

ctime,ftime,gettimeofday

表头文件

#i nclude

定义函数

time_t time(time_t *t)

函数说明

此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,

此函数也会将返回值存到t指针所指的内存。

返回值

成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。

范例

#i nclude

mian()

{

int seconds= time((time_t*)NULL)

printf("%d\n",seconds)

}

Date and Time Functions: <time.h>

The header <time.h>declares types and functions for manipulating date and time. Some functions process local time,

which may differ from calendar time, for example because of time zone. clock_t and time_t are arithmetic types

representing times, and struct tm holds the components

of a calendar time:

int tm_sec seconds after the minute (0,61)

int tm_min minutes after the hour (0,59)

int tm_hour hours since midnight (0,23)

int tm_mday day of the month (1,31)

int tm_mon months since January (0,11)

int tm_year years since 1900

int tm_wday days since Sunday (0,6)

int tm_yday days since January 1 (0,365)

int tm_isdstDaylight Saving Time flag

tm_isdst is positive if Daylight Saving Time is in effect, zero if not, and negative if the information is not available.

clock_t clock(void)

clock returns the processor time used by the program since the beginning of execution, or -1 if unavailable.

clock()/CLK_PER_SEC is a time in

seconds.

time_t time(time_t *tp)

time returns the current calendar time or -1 if the time is not available. If tp is not NULL,

the return value is also assigned to *tp.

double difftime(time_t time2, time_t time1)

difftime returns time2-time1 expressed in seconds.

time_t mktime(struct tm *tp)

mktime converts the local time in the structure *tp into calendar time in the same representation used by time.

The components will have values

in the ranges shown. mktime returns the calendar time or -1 if it cannot be represented.

The next four functions return pointers to static objects that may be overwritten by other calls.

char *asctime(const struct tm *tp)

asctime*tp into a string of the form

Sun Jan 3 15:14:13 1988\n\0

char *ctime(const time_t *tp)

ctime converts the calendar time *tp to local timeit is equivalent to

asctime(localtime(tp))

struct tm *gmtime(const time_t *tp)

gmtime converts the calendar time *tp into Coordinated Universal Time (UTC). It returns NULL if UTC is not available.

The name gmtime has historical significance.

struct tm *localtime(const time_t *tp)

localtime converts the calendar time *tp into local time.

size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp)

strftime formats date and time information from *tp into s according to fmt, which is analogous to a printf format.

Ordinary characters (including the terminating '\0') are copied into s. Each %c is replaced as described below,

using values appropriate for the local environment.

No more than smax characters are placed into s. strftime returns the number of characters, excluding the '\0',

or zero if more than smax characters were produced.

%a abbreviated weekday name.

%A full weekday name.

%b abbreviated month name.

%B full month name.

%c local date and time representation.

%d day of the month (01-31).

%H hour (24-hour clock) (00-23).

%I hour (12-hour clock) (01-12).

%j day of the year (001-366).

%m month (01-12).

%M minute (00-59).

%p local equivalent of AM or PM.

%S second (00-61).

%U week number of the year (Sunday as 1st day of week) (00-53).

%w weekday (0-6, Sunday is 0).

%W week number of the year (Monday as 1st day of week) (00-53).

%x local date representation.

%X local time representation.

%y year without century (00-99).

%Y year with century.

%Z time zone name, if any.

%% %