Compare dates in C ++

I have a file, 'date.txt', that has a date in it. Similar to Mon Oct 13 09:37:08 2009.

Now I want to compare this date with the system date. How do I compare dates in C ++.?

I used this code to get content from file 'date.txt'

    string date;
    while ( inDateFile >>date) //inDateFile is an ifstream object 
           cout<<date<<endl;

      

And this code to get the system date,

  time_t timer;
  struct tm *tblock;
  timer = time(NULL);
  tblock = localtime(&timer);
  string str = asctime(tblock);

      

Now how can I compare these two dates.?

+2
c ++ datetime


source to share


5 answers


Divide the values, use std::mktime

to get time_t

and now use std::difftime

to get the difference.



+4


source to share


We are using boost date_time to read dates from strings and then compare them. It works great in our experience.



+2


source to share


converts a date string to a numeric time value (32 or 64 bit), aka time, then compares to the system time at which time (NULL) is returned.

+1


source to share


if you just want to parse a line in your file, you need to split the line in resp values, you will also probably need to check which country the region is accounted for this as different regions have different names in order / month etc .. You can make this configuration in which order the different tokens will appear in your text.

One way to solve this, for example, is to take the string "date" and then use the Tokenizer pattern from boost (or if you want to do that, call the strtok () function in a C call) to split the string into substrings based on the delimiters. It is then easy to convert the input.

The other is maybe easier when you are reading from a file rather than reading the entire line read before the delimiter, eg. ',' then read again - you can use inDateFile.getline () to do this.

0


source to share


http://datetime.perl.org/index.cgi?FAQBasicUsage

You will have to convert your date from txt file to tm structre before you can compare.

-2


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics