C标准时间和日期函数

标准c时间与日期函数

1.    数据结构

time_t ----长整型__int64

clock_t ----long

struct tm {

        inttm_sec;     /*seconds after the minute - [0,59] */

        inttm_min;     /*minutes after the hour - [0,59] */

        inttm_hour;    /*hours since midnight - [0,23] */

        inttm_mday;    /*day of the month - [1,31] */

        inttm_mon;     /*months since January - [0,11] */

        inttm_year;    /*years since 1900 */

        inttm_wday;    /*days since Sunday - [0,6] */

        inttm_yday;    /*days since January 1 - [0,365] */

        inttm_isdst;   /*daylight savings time flag */

    };

 

2.    函数

time

语法:

 
  #include <time.h>
  time_t time( time_t *time );

功能: 函数返回当前时间,如果发生错误返回零。如果给定参数time ,那么当前时间存储到参数time中。

time函数返回以格林尼治时间GMT)为标准,从19701100:00:00到现在的此时此刻所经过的秒数。

difftime

语法:

 
  #include <time.h>
  double difftime( time_t time2, time_t time1 );

功能:函数返回时间参数time2time1之差的秒数表示。

clock

语法:

 
  #include <time.h>
  clock_t clock( void );

功能:函数返回自程序开始运行的处理器时间,如果无可用信息,返回-1。转换返回值以秒记, 返回值除以CLOCKS_PER_SECOND. (注: 如果编译器是POSIX兼容的,CLOCKS_PER_SECOND定义为1000000.)

ctime

语法:

 
  #include <time.h>
  char *ctime( const time_t *time );

功能:函数转换参数time为本地时间格式:

    day month date hours:minutes:seconds year\n\0

ctime() 等同

asctime( localtime( tp ) );

localtime

语法:

 
  #include <time.h>
  struct tm *localtime( const time_t *time );

功能:函数返回本地日历时间。

Watch out.

This function returns a variable thatis statically located, and therefore overwritten each time this function iscalled. If you want to save the return value of this function, you shouldmanually save it elsewhere.
Of course, when you save it elsewhere, you should make sure to actually copythe value(s) of this variable to another location. If the return value is astruct, you should make a new struct, then copy over the members of the struct. 

 

asctime

语法:

 
  #include <time.h>
  char *asctime( const struct tm *ptr );

功能: 函数将ptr所指向的时间结构转换成下列字符串:

    day month date hours:minutes:seconds year\n\0

例如:

    Mon Jun 26 12:03:53 2000

gmtime

语法:

 
  #include <time.h>
  struct tm *gmtime( const time_t *time );

mktime

语法:

 
  #include <time.h>
  time_t mktime( struct tm *time );

功能:函数转换参数time 类型的本地时间至日历时间,并返回结果。如果发生错误,返回-1。

 

strftime

语法:

 
  #include <time.h>
  size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time );

功能:函数按照参数fmt所设定格式将time类型的参数格式化为日期时间信息,然后存储在字符串str中(至多maxsize 个字符)。用于设定时间不同类型的代码为:

代码

含义

%a

星期的缩略形式

%A

星期的完整形式

%b

月份的缩略形式

%B

月份的完整形式

%c

月份的缩略形式

%d

月中的第几天(1-31)

%H

小时, 24小时格式 (0-23)

%I

小时, 12小时格式 (1-12)

%j

年中的第几天(1-366)

%m

月份 (1-12). Note: 某些版本的Microsoft Visual C++ 可能使用取值范围0-11.

%M

分钟(0-59)

%p

本地时间的上午或下午(AM or PM)

%S

秒钟(0-59)

%U

年中的第几周,星期天是一周的第一天

%w

星期几的数字表示(0-6, 星期天=0)

%W

一年中的第几周,星期天是一周的第一天

%x

标准日期字符串

%X

标准时间字符串

%y

年(0-99)

%Y

用CCYY表示的年(如:2004)

%Z

时区名

%%

百分号

函数strftime()返回值为处理结果字符串str中字符的个数,如果发生错误返回零。

3.    一个简单示例

4.       // ctime_test.cpp : ¡§°???¬¡§®|®?¨¬¨°Ì?¨?¨²Ì?¡ê

5.       //

6.        

7.       #include "stdafx.h"

8.        

9.       #include <time.h>

10.     #include <stdlib.h>

11.      

12.     int _tmain(int argc, _TCHAR*argv[])

13.     {

14.              time_tstart,end;

15.              start= time(NULL);

16.              system("pause");

17.              end= time(NULL);

18.              printf("The pause used %f seconds.\n",difftime(end,start));

19.      

20.              clock_t  _clk;

21.              _clk= clock();

22.              printf("clock:%d\n", _clk);

23.      

24.              system("pause");

25.              //ctime

26.              printf("%s\n", ctime(&start));

27.              system("pause");

28.      

29.              //localtime

30.              printf("%s\n",asctime(localtime(&start)));

31.              //gmtime

32.              printf("%s\n",asctime(gmtime(&start)));

33.              //mktime

34.              printf("%I64d\n",mktime(localtime(&start)));

35.      

36.              //strftime

37.              char buff[1024];

38.              strftime(buff,1023, "%Y-%m-%d  %H:%M:%S\n",localtime(&start));

39.              printf("%s\n",buff);

40.               

41.              return 0;

42.     }

43.    

44.      

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值