Hi,

I have a csv datafile with stock price data that I've read into a vector of structures that looks like this:

09/11/2009 9.30 open, high, low, close, volume
09/11/2009 9.31 o, h, l, c, volume

and so on to
09/11/2009 16.14 o, h, l, c, volume
//next day starts here:
09/12/2009 9.30, o, h, l, c, volume
09/12/2009 9.31, o, h, l, c, volume

As you can see, each date has ~390 minutes of price data associated with it.

I would like to be able to compare these dates, but the date field is of type string. How do I convert the string, such as 09/11/2009 to a type that allows me to do analysis on the data for a given date range, say 09/11/2009 to 09/20/2009?

Well if you just want to compare, then writing the dates out in yyyymmddhhmmss format allows you to use normal string comparison functions.

Or you could parse out the data, and use mktime() to give you some integer representation of time, which again can be compared to see which is before/after.

You can use a date as reference time, and calculate the number of seconds since that that time. You can do this for each data, and do comparison and analysis between dates that way.

Well if you just want to compare, then writing the dates out in yyyymmddhhmmss format allows you to use normal string comparison functions.

Or you could parse out the data, and use mktime() to give you some integer representation of time, which again can be compared to see which is before/after.

If I want to restrict analysis of o, h, l, c or volume for a given date or a range of dates, then can I use normal string comparison functions for that?

Let's say I want to find the lowest price for the range of prices from 12/10/2009 to end of 12/10/2009? or 12/10/2009 to 12/11/2009.

Can I just work with the string somehow to iterate over that range?

It will, but you will have to rearrange the dates in yyyymmdd format first, where 12/10/2009 becomes 20091210 and a date like 12/1/2009 would become 20091202 (force 0 leading month and day to make then 2 digits). Put all date strings in std::string objects then you can use > and < operators to test whether a particular date falls between the two date ranges.

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.