曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 35079|回复: 0
打印 上一主题 下一主题

asctime(),ctime(),gmtime(),localtime(),mktime(),asctime_r(),ctime_r(),gmtime_

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2008-11-16 23:07:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
包含头文件:  #include <time.h>

各个函数的原型:
Quote:
[blockquote]
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);

struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);

time_t mktime(struct tm *tm);
[/blockquote]
Thectime(), gmtime() and localtime() functions all take an argument ofdata type time_t which represents calendar time.  When  interpreted  asan  absolute  time  value,  it represents the number of seconds elapsedsince 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).
ctime(),gmtime(),localtime()都使用 time_t这个数据类型声明来表示日历时间。当作为一个绝对时间值来解释时,这个数值表示的是从1970年1月1日0时开始所经历的秒数-这个 time_t类型就是unix/linux存储形式,在头文件 time.h 中定义了 typedef long int time_t)

ctime() 和 mktime()使用下面的 tm 结构体来表示时间:

struct
tm {
     int tm_sec;         /* seconds */
     int tm_min;         /* minutes */
     int tm_hour;        /* hours */
     int tm_mday;        /* day of the month */
     int tm_mon;         /* month */
     int tm_year;        /* year */
     int tm_wday;        /* day of the week */
     int tm_yday;        /* day in the year */
     int tm_isdst;       /* daylight saving time */
};

ctime(t) 相当于 asctime(localtime(t)). 它把日历时间 t 转换为字符串的形式,如:

"Mon Dec 10 18:37:00 2008\n"

localtime() 将参数 timep 所指的 time_t 结构中的信息转换成现实世界所用的时间日期表示方法,然后将结果由结构 tm 返回返回。关于 tm 结构的定义可参考 gmtime() 函数http://www.groad.net/bbs/read.php?tid-604.html 。该函数返回的时间日期已经转换成为当地失去。测试代码如下:
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <time.h>


int 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), (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);


        return 0;
}

运行输出:
./localtime
2011/12/21 Wed 9: 56: 26

mktime() 函数将参数 tm 所指向的 struct tm 结构转换从公元 1970 年 1 月 1 日到至今 UTC 时间所经过的秒数。下面程序先用 time() 获得秒数时间,然后再用 localtime() 将其转换成 struct tm 结构,再使用 mktime() 函数又将 struct tm 结构转换为原来的秒数,即 time() ---> localtime() ---> mktime() :
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <time.h>

int main()
{
        time_t  timep;
        struct tm *p;

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

        p = localtime(&timep);  /*注意,localtime()完成后生成的tm是个静态结构,多次调用会将其覆盖
                                这里也无须再声明一个tm结构用以接收转换后的时间*/
        timep = mktime(p);

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

        return 0;
}

运行输出:
/mktime
time(): 1324435314
time() --> localtime() --> mktime(): 1324435314
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-4 01:29 , Processed in 0.062208 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表