Hi all, first post. So I'm working on a project where I have a class for parsing times from a file into tm structs which are then converted into time_t's (to be used with difftime). This works out pretty gewd, but every once in a while I get a few times and dates that are grossly wrong. Allow me to code:
time_t ConvertTime(string time_str, string date_str){
int sec, min, hour, day, mon, year;
char *time_char = //string to char* conversion
char *date_char = //string to char* conversion
//parse the string into vals
sscanf(time_char,"%i:%i:%i",&hour,&min,&sec);
sscanf(date_char,"%i/%i/%i",&mon,&day,&year);
year = year - 1900;
mon = mon - 1;
//fill in tm struct
tm_time.tm_sec = sec;
//...etc...
//create the time_t
time_t time_struct = mktime(&tm_time);
//******************************************************************
//for some reason this is necessary to get the correct date and time
stringstream sx;
sx << ctime(&time_struct) << endl;
stringstream ss;
ss << mon -1 << " "<<day<< " " <<year-1900<<" "<<hour<<":"<<min<<":"<<sec<<endl;
//
//*******************************************************************
return time_struct;
}
So, let's say this is running through a file with a thousand similar times and dates: 6/8/2011 13:00:00. If one were to remove the section with the string streams one may see a malformed date pop up; say 3/5/2077 15:05:22. I came up with the string stream solution while attempting to debug this with output statements, every time I would output the date and time after the struct is filled in, I would get correct dates and times.
This doesn't appear to be random, and it doesn't make sense why I need to send my data to a stream to get it to be correct. This is driving me nuts, thanks for any help in advance. Thoughts on why?
Hmmm, I wouldn't necessarily need to initialize my variables since they will get set by sscanf. Upon further analysis I found that sscanf doesn't like non-normal numbers like 08; this results in some butt-hurt:
1 2 3
char x[3];
sscanf(time_char,"%i:%i:%s",&hour,&min,x);
sec = atoi(x); //<-- the correct number of seconds
However, this can't describe the problem in full as the format of years, months, days, and hours are normal numbers: 5/15/2011 8:05:08. I'm also sure that the input is all properly formatted, leaving no holes. If any of these variables were NULL I would get the usual December 31 1990 garbage. Thanks anyways.
###Update###
Initial problem solved: problem no longer present after converting sub char strings using atoi. It's rather interesting that this solved the problem since it originally didn't have anything to do with the problem, just time accuracy. In short, one should be careful when using sscanf.
I wouldn't necessarily need to initialize my variables ...
You should always initialise your variables.
It's possible you have some other corruption in your program and the change in stack or heap from creating a couple of streams changes things sufficiently to move the corruption somewhere you haven't noticed yet.
As of the update I have removed the streams with out issue. I've also initialized the variables, but, as previously stated, I am not concerned with such problems as corruption. The goal of this project is to simply parse a log file and run stats; it's an all or nothing operation running through a gargantuan log that better be formatted correctly or fail.