0

I am programming in c++,i have a requirement as to get the date only ,rest of the components are not needed,

i have contents in my file in the following way:-

2012-03-26T15:05:24.844Z - DEBUG:   Logging_Test:test3 : Testing file logger

2012-03-26T15:05:24.844Z->this part is returned from a function GetDateTime(),which has a stringstream sDateStream and returing to the function GetDateTime() a string

My objective is just to get the date from this part and compare it with the system's date and for the system date i m using:-

void FileLogger::date()
{

SYSTEMTIME time;//variable time to get the system time
GetLocalTime(&time);//reference to the timek
int hour = time.wHour;
if (hour > 12) hour -= 12;
int year = time.wYear;
int month = time.wMonth;
int day = time.wDay;
//not returning anything just storing values of day ,month
//& year

}

now i need to make a comparision b/w two dates and if the date in the .txt is smaller than today's date a new file gets created

please help guys i know it is very simple for the masters but for a starter like me it is creating troubles , please provide help in form of some code so that i can understand it

Yahia
  • 69,653
  • 9
  • 115
  • 144
gandhigcpp
  • 587
  • 3
  • 8
  • 16
  • I would keep the dates in the ISO format `2012-03-26`, as they are comparable as strings. – Bo Persson Mar 28 '12 at 06:28
  • Never mind, opposite of question was asked previously. – David D Mar 28 '12 at 06:32
  • David D :yes david i m stuck with this for more than 3 days may be i am not a master and any of the suggestions are welcomed i hope people guide me in the right direction – gandhigcpp Mar 28 '12 at 06:37
  • Bo Person:hmmm i have used a stringstream to make it look like an iso ,now my question would be how do i get only that part i was searching for the substring function still unsuccesful in implementing that – gandhigcpp Mar 28 '12 at 06:38

1 Answers1

0

If you've got the line as a string, and you're sure of the format, the simplest solution would be:

std::string dateString(
    line.begin(), std::find( line.begin(), line.end(), 'T' ) );

(I'd still do some extra checking; e.g. that the dateString is exactly 10 characters long, that it only contains digits or '-', etc.)

This is the ISO date format; it has been especially designed to support comparison directly as a string, so you might want to consider getting the system date as a string in ISO format as well. (This is very easy if you use the standard functions time, localtime and strftime.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • To James:-Thanks james for giving an answer actually i have a string _LastMessage – gandhigcpp Mar 28 '12 at 08:00
  • To James:-Thanks james for giving an answer actually i have a string _LastMessage and the getdatetimefunction is used some as :-_lastMessage=getdatetime()+"debug"...;something like this i actually have the system date as it is shown above as string date;now i m confused with the comparision that would be made do i need to compare something like if(datestream<=date) { file.open....//dont know how the file (.txt file) would be created and new log entries be written into it please help in this regard as well } also it shows a red line on 'line' in the editor window thanks ... – gandhigcpp Mar 28 '12 at 08:10
  • 1
    I'm not too clear about what you're trying to do. If you have a `time_t` with the system time, using `localtime` and `strftime` will allow you to format it however you want. If you have two strings with ISO dates, then simple string comparison ('<', '==', etc. on the strings) will give correct results, provided the format options (whether or not the `'-'` are present) are the same; you don't have to convert the strings into anything else. – James Kanze Mar 28 '12 at 08:14
  • To James:- My Objective is to compare the last logged date with the current date and if the last log day is smaller than current day i would close the existing log file and would create a new log file....so i am comparing the two dates for the last logged date(since i did not get anything on how to get the Date Modified :which is a property of file once it gets created ) so i thought it is better to open the file,below is the code i have used string dateString; ifstream file; file.open("C:/repos/trunk/logging/text.txt"); after this the statement cited above – gandhigcpp Mar 28 '12 at 09:03
  • cont..so basically i am opening the file and i have the string as the date ,time .... and trying to compare that date with the systems' one i know that thi is not the right way as 1 could modify the file's content but then i could not see some other way out to this... – gandhigcpp Mar 28 '12 at 09:04
  • @gandhigcpp So you have the newly generated line and the previous line, both formatted in the same manner, and both starting with the ISO date. In this case, all you need to do is `if ( previousLine.compare( 0, 10, newLine, 0, 10 ) < 0 )`. (This call to `compare` will return an integral value `<`, `==` or `> 0` according to whether the first 10 characters of `previousLine` compare `<`, `==` or `>` the first 10 characters of `newLine` in the native collating sequence. Which isn't generally that useful, but when you know that you are dealing with ISO dates, it's exactly what is needed. – James Kanze Mar 28 '12 at 10:41