Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1069665
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: C/C++

2011-07-27 11:36:04

#define _XOPEN_SOURCE
#include
#include
#include
#include
#include

#define err(msg) perror(msg)

static void strftime_sample(time_t seconds)
{
        struct tm tm;
        char buf[1024];

        memset(&tm, '\0', sizeof(struct tm));
        localtime_r(&seconds, &tm);

        memset(buf, '\0', sizeof(buf));
        strftime(buf, sizeof(buf) - 1, "%a, %d %b %Y %H:%M:%S GMT", &tm);
        printf("%s\n", buf);
}

static void strptime_sample(void)
{
        char buf[] = "Thu, 04 Aug 2011 19:21:29 GMT";
        struct tm tm;
        time_t seconds;

        printf("%s\n", buf);

        memset(&tm, '\0', sizeof(struct tm));
        if (strptime(buf, "%a, %d %b %Y %H:%M:%S GMT", &tm) == NULL)
        {
                err("strptime");
                goto out;
        }

        seconds = mktime(&tm);

        strftime_sample(seconds);
out:
        return;
}

int main(void)
{
        strptime_sample();
        return 0;
}

阅读(1069) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~