Thu, 21 Aug 2014 19:15:12 +0800 (CST) 时间格式化

Jaeson_Chen 2014-08-21 07:34:28
如题所示获取到的时间字符串”Thu, 21 Aug 2014 19:15:12 +0800 (CST)“,需要用C语言进行格式化为这样子的字符串:
2014-08-21 19:15:12 ,请问是否有相关函数?或者该如何进行转换?
...全文
4908 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jaeson_Chen 2014-11-06
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
仅供参考:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <time.h>
#include <sys/timeb.h>

struct tm st;
time_t tt;
char mon[4];
char mn[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int i;
char timstr[27];
char tmpbuf[128];
struct _timeb tb;
int tz;
char c;

void main() {
    strcpy(timstr,"08/Dec/2011:15:25:03 +0800");
    sscanf(timstr,"%2d/%3s/%4d:%2d:%2d:%2d",&st.tm_mday,mon,&st.tm_year,&st.tm_hour,&st.tm_min,&st.tm_sec);
    for (i=0;i<12;i++) if (0==stricmp(mn[i],mon)) {st.tm_mon=i; break;}
    st.tm_year-=1900;
    tt=mktime(&st);
    if (-1!=tt) {
        strftime(tmpbuf,128,"%Y-%m-%d %H:%M:%S\n",localtime(&tt));
        printf(tmpbuf);//2011-12-08 15:25:03
    } else {
        printf("[%s] is Invalid time string!\n",timstr);
    }

    _ftime(&tb);
    strftime(tmpbuf,128,"%m/%b/%Y:%H:%M:%S",localtime(&tb.time));
    tz=-tb.timezone;
    c=(tz>0)?'+':'-';
    tz=(tz>0)?tz:-tz;
    sprintf(tmpbuf,"%s %c%02d%02d\n",tmpbuf,c,tz/60,tz%60);
    printf(tmpbuf);//12/Dec/2011:17:36:41 +0800
}
忘记结贴了,非常感谢你!!
Jaeson_Chen 2014-08-22
  • 打赏
  • 举报
回复
多谢,总监的对C不了解,一定要我掉函数,我欲哭无泪,自己已写好了
707wk 2014-08-22
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
仅供参考:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <time.h>
#include <sys/timeb.h>

struct tm st;
time_t tt;
char mon[4];
char mn[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int i;
char timstr[27];
char tmpbuf[128];
struct _timeb tb;
int tz;
char c;

void main() {
    strcpy(timstr,"08/Dec/2011:15:25:03 +0800");
    sscanf(timstr,"%2d/%3s/%4d:%2d:%2d:%2d",&st.tm_mday,mon,&st.tm_year,&st.tm_hour,&st.tm_min,&st.tm_sec);
    for (i=0;i<12;i++) if (0==stricmp(mn[i],mon)) {st.tm_mon=i; break;}
    st.tm_year-=1900;
    tt=mktime(&st);
    if (-1!=tt) {
        strftime(tmpbuf,128,"%Y-%m-%d %H:%M:%S\n",localtime(&tt));
        printf(tmpbuf);//2011-12-08 15:25:03
    } else {
        printf("[%s] is Invalid time string!\n",timstr);
    }

    _ftime(&tb);
    strftime(tmpbuf,128,"%m/%b/%Y:%H:%M:%S",localtime(&tb.time));
    tz=-tb.timezone;
    c=(tz>0)?'+':'-';
    tz=(tz>0)?tz:-tz;
    sprintf(tmpbuf,"%s %c%02d%02d\n",tmpbuf,c,tz/60,tz%60);
    printf(tmpbuf);//12/Dec/2011:17:36:41 +0800
}
+1
zhuyf87 2014-08-22
  • 打赏
  • 举报
回复
”Thu, 21 Aug 2014 19:15:12 +0800 (CST)“,如果格式固定的话,自己转很简单,最基本的c语言编程。 总监还管你调用哪个函数?
边走边瞧 2014-08-22
  • 打赏
  • 举报
回复
引用 4 楼 chenxiong815 的回复:
多谢,总监的对C不了解,一定要我掉函数,我欲哭无泪,自己已写好了
你们总监很有才啊,这么个细节也要关注一下。
赵4老师 2014-08-22
  • 打赏
  • 举报
回复
仅供参考:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <time.h>
#include <sys/timeb.h>

struct tm st;
time_t tt;
char mon[4];
char mn[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int i;
char timstr[27];
char tmpbuf[128];
struct _timeb tb;
int tz;
char c;

void main() {
    strcpy(timstr,"08/Dec/2011:15:25:03 +0800");
    sscanf(timstr,"%2d/%3s/%4d:%2d:%2d:%2d",&st.tm_mday,mon,&st.tm_year,&st.tm_hour,&st.tm_min,&st.tm_sec);
    for (i=0;i<12;i++) if (0==stricmp(mn[i],mon)) {st.tm_mon=i; break;}
    st.tm_year-=1900;
    tt=mktime(&st);
    if (-1!=tt) {
        strftime(tmpbuf,128,"%Y-%m-%d %H:%M:%S\n",localtime(&tt));
        printf(tmpbuf);//2011-12-08 15:25:03
    } else {
        printf("[%s] is Invalid time string!\n",timstr);
    }

    _ftime(&tb);
    strftime(tmpbuf,128,"%m/%b/%Y:%H:%M:%S",localtime(&tb.time));
    tz=-tb.timezone;
    c=(tz>0)?'+':'-';
    tz=(tz>0)?tz:-tz;
    sprintf(tmpbuf,"%s %c%02d%02d\n",tmpbuf,c,tz/60,tz%60);
    printf(tmpbuf);//12/Dec/2011:17:36:41 +0800
}
brookmill 2014-08-21
  • 打赏
  • 举报
回复
linux里面可以用strptime和strftime,windows里面好像没有。 自己写很简单,就用sscanf和sprintf,转换一下月份就行了
Jaeson_Chen 2014-08-21
  • 打赏
  • 举报
回复
已写,老大一直认为是有对应的库函数的,我一直觉得是没有的,还是自己写一写吧
jwj070524 2014-08-21
  • 打赏
  • 举报
回复
Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec 自己写个函数转换吧,不是很困难

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧