Hi all,

2nd week programming and face first into the wall again haha. As always I appreciate any advice or critique.

I have a small c++ program that reads through log files from a program and scans for errors. For the most part it works really well but I'm trying to add functionality and options to the program in terms of date/time selection.

essentially the date and time in the logs is presented like this:
12-Oct-2010
11-Jun-2011
14-Sep-2009

and so on. I was able to calculate the current time and my first option which is (current time - 30) days with this:

int endTime = 0;
endTime = time(NULL);
int startTime = (endTime - ((60*60*24)*30));

this gives me a number of 139128491824 blah blah so on which is fine because its constant, what I'm trying to do is convert the date structure listed above into a comparable number so I can say if

if (endTime >= start time && logTime <= endTime

thats basically the last 30 days (of course I can change it to 1 day 10 days whatever the need).

does anyone have any advice on how to do this? I've tried using swscanf() (its all utf-16 so has to be widestring). The code looks something like this

#include so on so on

int day = 0;
int year = 0;
wchar_t month [4] = {0};
wchar_t* logDate = L"12-Oct-2011";

swscanf(logDate, L"%d-%[^-]s-%d", &day, month, &year);

This almost works in that when I

wprintf ("d", day)

and so on I get the date listed in my *logDate but i'm struggling to comprehend how to make it read for any date in the "00-abc-0000" format (not to mention when i printf (%d, year) it only returns 0 >.< ).

As you can see I'm not really sure how to proceed, if I can get swscanf to read all the possible months I can just say if = oct oct=10 kind of thing and that puts it in the 00.00.0000 time format so I can get the same number.

Am I going about this the wrong way? Is there an easier way to do it? As always thanks for any advice.

you could convert it into struct tm that's declared in time.h then call mktime() to convert that structure into type time_t.

Thanks for the reply :)

I had a look at the syntax for it but I can't find much information on how to feed into the conversion. If I set up like this:

time_t time;
struct tm* logTime;
int day, year = 0;
wchar_t month [4]= {0};

How do I need to go about the actual conversion of that format into the struct?

Sorry if the answer is incredibly simple, I just can't get my head around it..
Thanks :)

struct tm* logTime ; // ***

You haven't got a struct tm; all you have is a pointer.


> How do I need to go about the actual conversion of that format into the struct?

Use a look-up table, perhaps?

const wchar_t* const month_names[12] = { L"Jan", L"Feb" /* , etc */ };
int m = 0 ;
for( ; m < 12 ; ++m ) if( memcmp( month, month_names[m], sizeof(month) ) == 0 ) break ;
struct tm log_time ;
log_time.tm_mon = m ;

>>for( ; m < 12 ; ++m ) if( memcmp( month, month_names[m], sizeof(month) ) == 0 ) break ;
struct tm log_time ;

Or you could just use wcscmp() from tchar.h. sizeof(month) may or may not be the same as wcslen(month)
for( ; m < 12 ; ++m ) if( wcscmp( month, month_names[m]) == 0 ) break ;

Why are you trying to do this using wide strings? Based on your first post it was an arbitrary decision.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.