设为首页 加入收藏

TOP

C 语言日期时间处理 (一)
2014-11-23 21:58:14 来源: 作者: 【 】 浏览:27
Tags:语言 日期 时间 处理

前言
在标准C中, 日期和时间的处理包含在 time.h 的头文件中。

需要使用日期和时间相关的类型的函数的话, 需要导入time.h.

本篇介绍的部分有:

1. 日期时间相关的类型

2. 日期时间相关的函数

3. 一些例子


日期时间相关的数据类型
1. time_t


time_t是一个长整型数。表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数。

这个类型的值类似:


9.73E+08

2. tm


结构体类型。 可以在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 */ 
        }; 

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 */
        };

3. timeva l


日期时间相关的函数
1. time(取得目前的时间)

 time_t time(time_t *t);

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


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

相关函数:time,ctime,gmtime,localtime

表头文件:#include

定义函数:char * asctime(const struct tm * timeptr);

函数说明:asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,

     然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,

     字符串格式为:“Wed Jun 30 21:49:08 1993\n”

返 回 值:若再调用相关的时间日期函数,此字符串可能会被破坏。

     此函数与ctime不同处在于传入的参数是不同的结构。

附加说明:返回一字符串表示目前当地的时间日期。

范  例:


1#include
2main() {
3 time_t timep;
4 time (&timep);
5 printf(“%s”,asctime(gmtime(&timep)));
6}

执行结果:Sat Oct 28 02:10:06 2000

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

相关函数:time,asctime,gmtime,localtime

表头文件:#include

定义函数:char *ctime(const time_t *timep);

函数说明:ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,

     然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,

     字符串格式为“Wed Jun 30 21 :49 :08 1993\n”。

     若再调用相关的时间日期函数,此字符串可能会被破坏。

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

范  例:


1#include
2main(){
3 time_t timep;
4 time (&timep);
5 printf(“%s”,ctime(&timep));
6}

执行结果:Sat Oct 28 10 : 12 : 05 2000

4. gettimeofday(取得目前的时间)

相关函数:time,ctime,ftime,settimeofday

表头文件:#include

#include

定义函数:int gettimeofday ( struct timeva l * tv , struct timezone * tz )

函数说明:gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
timeva l结构定义为:


1struct timeva l{
2 long tv_sec; /*秒*/
3 long tv_usec; /*微秒*/
4};

timezone 结构定义为:

1struct timezone{
2 int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
3 int tz_dsttime; /*日光节约时间的状态*/
4};

上述两个结构都定义在/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所指的内存空间超出存取权限。

范  例:


1#include
2#include
3main(){
4 struct timeva l tv;
5 struct timezone tz;
6 gettimeofday (&tv , &tz);
7 printf(“tv_sec; %d\n”, tv,.tv_sec) ;
8 printf(“tv_usec; %d\n”,tv.tv_usec);
9 printf(“tz_minuteswest; %d\n”, tz.tz_minuteswest);
10 printf(“tz_dsttime, %d\n”,tz.tz_dsttime);
11}

执行结果:

tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0

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

相关函数:time,asctime,

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇 C中的几组指针 下一篇C中嵌入SQL

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: