Hi,

I've got stock market data in a structure as follows.

struct PriceInfo
{
	double       Open;
	double       High;
	double       Low;
	double       Close;
	unsigned int Volume;
	unsigned int Time;
	std::string  Date;
};

My program imports the data from a csv file and stores it in memory etc. The date and time look like this when printed to screen.

Date Time Open High Low Close Volume
12/10/2009 930 2100 2200 2075 2150 10000
12/10/2009 931 ....etc
12/10/2009 932 ....erc

I now need to be able to compare data using the date (string) and time (unsigned int) as 'sort by' fields. Essentially, I'd like to be able to do something like:

For 12/10/2009 930 to 12/10/2009 1600 do analysis

or For 12/10/2009 to 12/14/2009 do analysis

Should the date string and time int be combined to form one datetime object? If so, can someone show me the steps? I've googled but am too inexperienced to really understand the examples and need someone to walk me step by step.

Thanks,
TR

Recommended Answers

All 6 Replies

I now need to be able to compare data using the date (string) and time (unsigned int) as 'sort by' fields. Essentially, I'd like to be able to do something like:

I'd say that's your problem right there. If you are storing the date as a string and want to "sort" or "compare" by date, strings compare as "less" and "more" by what comes first in alphabetical order, which is useless for your purposes (i.e. "July" is before "May" in the alphabet).

"struct tm" is provided for you.

http://www.cplusplus.com/reference/clibrary/ctime/tm/

Dates and times are numbers. You can convert between time_t (seconds since 1970) and "struct tm". You can also display a date / time as a human readable string. All of these conversion functions are provided in the ctime library.


http://www.cplusplus.com/reference/clibrary/ctime/

This is what I have, following Vernon's guidelines:

int main(int argc, char* argv[])
{
    
		if (argc != 2)
	{
		std::cerr << "Usage: " << argv[0] << " filename " << std::endl;
		return EXIT_FAILURE;
	}

	
	PriceList prices;
    ReadPricesFromFile(argv[1], prices);
    // PrintPrices(std::cout, prices);
    std::vector<PriceInfo>::iterator it;
    it = prices.begin();
    std::string dt;
	std::string t;
	dt = it->Date;
	t = it->Time;
	std::istringstream istr (dt);
	std::istringstream istrt (t);
	int m, d, y, h, min;
	istr >> m;
	istr.ignore();
	istr >> d;
	istr.ignore();
	istr >> y;
	istrt >> h;
	istrt >> min;
	std::cout << m << std::endl;
	std::cout << d << std::endl;
    std::cout << y << std::endl;
	std::cout << h << std::endl;
	std::cout << min << std::endl;

	struct tm timestr;
	timestr.tm_year = y;
	timestr.tm_mon = m;
	timestr.tm_wday = d;
	timestr.tm_hour = h;
	timestr.tm_min = min;
	printf ("%d", "%d", "%d", "%d", "%d",
		timestr.tm_wday,
		timestr.tm_mon,
		timestr.tm_year,
		timestr.tm_hour,
        timestr.tm_min
		);

	
    return EXIT_SUCCESS;
}

When i compile and run, the program outputs the first broken up date as:
12
10
2009

which is good. Then, it prints

-858993460
-858993460
4323396

Now, I know that the 4323396 represents the date (12/10/2009) in terms of a time_t variable, but why is it printing the 858993460 (meant to be the time, 9.30am) twice?

Also, how do I combine the two fields to form a DateTime field and how can mktime be used next to do DateTime comparisons?

P.S: This is NOT a homework assigment. Could really use some help here...

Read up on the syntax for printf() . You are using it wrong.

And if this is a C++ program, why are you using printf() anyway?

You may be doing too much of the work yourself. C++ provides a lot of these printing and conversion functions. Do your comparisons as two time_t objects. You may be able to do a straight conversion between two struct tm objects, I don't know. But comparing two time_t objects is simple because they are integral types. ust use the <, >, == operators as you would with any two integers. As WaltP mentioned, the printf is wrong. It's one long string with the formatters: "%d %d %d %d %d", not one string PER formatter separated by commas. You have the arguments correctly separated by commas.


Convert times to human-readable strings
http://www.cplusplus.com/reference/clibrary/ctime/ctime/
http://www.cplusplus.com/reference/clibrary/ctime/strftime/
http://www.cplusplus.com/reference/clibrary/ctime/asctime/


Convert between struct tm and time_t
http://www.cplusplus.com/reference/clibrary/ctime/mktime/
http://www.cplusplus.com/reference/clibrary/ctime/gmtime/
http://www.cplusplus.com/reference/clibrary/ctime/localtime/

You may be doing too much of the work yourself. C++ provides a lot of these printing and conversion functions.

But how will that help when he goes into the next language and these conversions are not available? :icon_wink:
It's good to know how to do it...

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.