现在的位置: 首页 > 综合 > 正文

深入详解标准c时间与日期函数

2014年01月04日 ⁄ 综合 ⁄ 共 4706字 ⁄ 字号 评论关闭

                                   深入详解 标准c时间与日期函数

时间与日期结构体(time.h)
struct tm {
            int tm_sec;     /* seconds after the minute - [0,59] */
            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_isdst;   /* daylight savings time flag */
                        /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时                        的进候,tm_isdst为0;不确定时,tm_isdst()为负,由系统自动选择。*/
        };
通过预定义的两种结构来存储时间:
1.time_t(长整型long)型变量,用time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数。
2.tm结构体。

函数列表:
time()返回系统的当前日历时间
clock()返回自程序开始运行所经过的时间
asctime()时间文本格式
gmtime()返回指向当前格林威治时间的指针
localtime()返回指向当前时间的指针
mktime()返回指定时间的日历格式
ctime()返回特定格式时间
difftime()两时刻的间隔
strftime()返回日期和时间的单个元素
_strtime()获取时间
_strdate()获取日期
 
/////////////////////////////////////////////////////////////////////////////////////

#include <time.h>
clock_t clock( void );
功能:C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t。在MSDN中对clock函数定义如下:
        clock_t clock( void );
函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,clock_t是一个长整形数,保存时间的数据类型。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:
                    #define CLOCKS_PER_SEC ((clock_t)1000)
每过千分之一秒(1毫秒),调用clock()函数返回的值就加1,时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而使计时精度更高呢?这样是不行的。在标准C/C++中,最小的计时单位是一毫秒。

double difftime( time_t time2, time_t time1 );
功能:函数返回时间参数time2和time1之差的秒数表示。
示例:
#include "time.h"
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
    time_t t_start, t_end;  
 time_t c_start,c_end;
    c_start = clock();
    t_start = time(NULL) ;
    system("pause");
    c_end = clock();
    t_end = time(NULL) ;
    printf("The pause used %f ms by time()./n",difftime(t_end,t_start)) ;
       printf("The pause used %f s by clock()./n",difftime(c_end,c_start)) ;
    system("pause");
    return 0;
}
输出结果:
The pause used 9.000000 ms by time().
The pause used 9406.000000 s by clock().

time_t time( time_t *time );
功能:函数返回当前时间,如果发生错误返回零。如果给定参数time ,那么当前时间存储到参数time中。
char *asctime( const struct tm *timeptr );
wchar_t *_wasctime( const struct tm *timeptr );
功能: 函数将timeptr所指向的时间结构转换成下列字符串:
wday month date hours:minutes:seconds year/n/0
例如:
   Mon Jun 26 12:03:53 2000
struct tm *localtime( const time_t *time );
功能:函数返回本地日历时间。

struct tm *gmtime( const time_t *time );
功能:函数返回给定的统一世界时间(通常是格林威治时间),如果系统不支持统一世界时间系统返回NULL。
time_t mktime( struct tm *time );
功能:函数转换参数time 类型的本地时间至日历时间,并返回结果。如果发生错误,返回-1。
示例:
/* 用time()取得时间(秒数),利用localtime()
转换成struct tm ,用asctime()把tm转换成字符串,再利用mktime()将struct tm转换成原来的秒数*/
#include <time.h>
#include <stdio.h>
struct tm *newtime;
time_t aclock;
void main( void )
{
   time( &aclock );                 /* Get time in seconds */
   newtime = localtime( &aclock );  /* Convert time to struct */
   printf( "The current date and time are: %d/n",aclock);                                 /* tm form */
   /* Print local time as a string */
   printf( "The current date and time are: %s", asctime( newtime ) );
   time_t timep = mktime(newtime);
   printf("time()->localtime()->asctime->mktime():%d/n",timep);
}
输出结果:
The current date and time are: 1243395187
The current date and time are: Wed May 27 11:33:07 2009
time()->localtime()->asctime->mktime():1243395187

char *ctime( const time_t *time );
功能:函数转换参数time为本地时间格式:
day month date hours:minutes:seconds year/n/0
ctime() 等同 asctime( localtime( tp ) );

size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time );
功能:函数按照参数fmt所设定格式将time类型的参数格式化为日期时间信息,然后存储在字符串str中(至多maxsize 个字符)。函数strftime()返回值为处理结果字符串str中字符的个数,如果发生错误返回零。用于设定时间不同类型的代码为:
------------------------------------------------------------------------------------
%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 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号
------------------------------------------------------------------------------------
char *_strtime( char *timestr );
功能:将获取的系统时间存放到timestr中。
char *_strdate( char *datestr );
功能:将获取的系统时期存放到datestr中。
示例:
#include <time.h>
#include <stdio.h>
void main( void )
{
   char dbuffer [9];
   char tbuffer [9];
   char tmpbuf[128];
   time_t totime;
   time(&totime);
   struct tm *today=localtime(&totime);
   _strdate( dbuffer );
   printf( "The current date is %s /n", dbuffer );
   _strtime( tbuffer );
   printf( "The current time is %s /n", tbuffer );
   strftime( tmpbuf, 128,
         "Today is %A, day %d of %B in the year %Y./n", today );
    printf( tmpbuf );
}
输出结果:
The current date is 05/27/09
The current time is 12:48:33
Today is Wednesday, day 27 of May in the year 2009.

 

抱歉!评论已关闭.