Covert DateTime To a standard format

Hello my problem is to convert "Friday, 16 March 2012 12:45:11 +05:30"
to "2012-03-16 12:45:11 z+05:30" i have tried to solve it by following code and i'm getting a bug only YEAR and MONTH are displayed and the rest all are zero's
i tried and unable to resolve the bug can anyone findout the bug i have made correct it thanks in advance.I used visual studio 2010 to build the code.





#include "stdafx.h"
#include<time.h>
#include<iostream>
#include<string>

using namespace std;
void print(string);
int d,m,y,h,mm,se;
int main()
{
int x,i,j=0;
char x1[256];
string w[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
string mon[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
cout<<"Enter date"<<endl;
cin.getline(x1,256);
string s(x1);

int flag=0,start=0,mflag=0,dflag=0,fflag=0;
string s1;
x=s.length();
for(i=0;i<x;i++)
{

if((!isdigit(s[i])&&flag==0)||(isdigit(s[i])&&flag==0))
flag=1;
else
{
string p=s.substr(start,i);
if(!mflag||!dflag)
{
for(j=0;j<12;j++)
{
if((p.find(mon[j])!=string::npos))

{
m=j+1;
start=i+1;

mflag=1;
}

}
for(j=0;j<7;j++)
{
if(p.find(w[j])!=string::npos)

{
start=i+1;

dflag=1;
}

}
}
if(s[i]==' '&&!fflag)
{
j=atoi(s.substr(start,i).c_str());
if(j<32)
{
d=j;
start=i+1;
}
else
{
y=j;
start=i+1;
}
if(d!=0&&y!=0)
fflag=1;
}
else if(s[i]==' '&&fflag)
{

h=atoi(s.substr(start,i).c_str());
mm=atoi(s.substr(i+1,i+3).c_str());
se=atoi(s.substr(i+4,i+6).c_str());
break;
}


}


}
string out="z";
int ttt=s.find('+');
if(ttt==string::npos)
ttt=s.find('-');
out.append(s.substr(ttt).c_str());
print(out);



}


void print(string s)
{
cout<<y<<"-";
if(m<10)
cout<<"0"<<m<<"-";
else
cout<<m<<" ";
if(d<10)
cout<<"0"<<d<<" ";
else
cout<<d<<" ";
if(h<10)
cout<<"0"<<h<<":";
else
cout<<h<<":";
if(mm<10)
cout<<"0"<<mm<<":";
else
cout<<mm<<":";
if(se<10)
cout<<"0"<<se;
else
cout<<se;
cout<<s.c_str();
}






If you're going to iterate the string, one character at a time, you'll need to keep track of the state. That is, you'll need to keep track of what you're processing; the day, date, time, ...

An alternative way to parse things is to extract tokens. No state, you just pick up what you're looking for and can optionally validate it yourself.

In this case, the seperators are ", " and the input format is:
<day of week text><month day><month text><year><time><time zone>
and your output is
<year>-<month>-<month day> <time> z<time zone>

There is a C++ tokenizer in boost and there's a C tokeniser in the C standard library (strtok)
http://www.boost.org/doc/libs/1_34_0/libs/tokenizer/tokenizer.htm
http://pubs.opengroup.org/onlinepubs/007908799/xsh/strtok.html

It may be easier to lookup strtok examples and use that as getting setup for Boost is a task in itself.
Last edited on
Thank you very much kbw(4313) i solved my problem.
Topic archived. No new replies allowed.