C语言中的时间函数及使用实例

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

#include <stdio.h> /* NULL */

#include <time.h> /* ctime, asctime */

main()

{

time_t now; /* define 'now'. time_t is probably

* a typedef */

/* Calender time is the number of

* seconds since 1/1/1970 */

now = time((time_t *)NULL); /* Get the system time and put it

* into 'now' as 'calender time' */

printf("%s", ctime(&now)); /* Format data in 'now'

* NOTE that 'ctime' inserts a

* '\n' */

/*********************************************************************/

/* Here is another way to extract the time/date information */

time(&now);

printf("%s", ctime(&now)); /* Format data in 'now' */

/*********************************************************************/

{

struct tm *l_time;

l_time = localtime(&now); /* Convert 'calender time' to

* 'local time' - return a pointer

* to the 'tm' structure. localtime

* reserves the storage for us. */

printf("%s", asctime(l_time));

}

/*********************************************************************/

time(&now);

printf("%s", asctime(localtime( &now )));

/*********************************************************************/

{

struct tm *l_time;

char string[20];

time(&now);

l_time = localtime(&now);

strftime(string, sizeof string, "%d-%b-%y\n", l_time);

printf("%s", string);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航