Solved

How to unpack year, month, and day from date in ISO 8601 format ?

Posted on 2014-02-07
9
362 Views
Last Modified: 2014-02-07
The date in ISO 8601 form is "2014-02-07" or more generically "YYYY-MM-DD".

Please provide code to extract year, month, and day and assign to variables.
0
Comment
Question by:naseeam
  • 5
  • 4
9 Comments
 
LVL 1

Author Comment

by:naseeam
ID: 39841815
Earlier one of experts provided sprintf ( ) solution to to pack year, month, and day into string.  Is there a similar function to go do the opposite ?
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 39841879
Hi naseeam,

the counterpart to sprintf is sscanf. Here's a small sample on how to use it to parse such a date:
	std::string strDate = "2014-02-07";
	int year, month, day;

	sscanf( strDate.c_str(), "%d-%d-%d", &year, &month, &day );

	std::cout << day << "." << month << "." << year << std::endl;

Open in new window

The output is 7.2.2014

Hope this helps,

ZOPPO
0
 
LVL 1

Author Comment

by:naseeam
ID: 39842019
Yes, this helps a lot.  I need to make sure I know how to use my string date with your code.

In our base class we have a pointer that points to date in ISO 8601 format as follows:

protected:
 
    void * value;   /* points to date, i.e. "2014-02-07"  */

In the constructor, we reserve space for the ISO 8601 date as follows:

 /* reserve 11 bytes for date, include null terminator */
value = new char [ _buffLength + 1 ];

>> std : : string strDate = "2014-02-07";
value points to the date in ISO 8601 format.  How do I connect my date string to your code ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 39842062
Using char* instead of std::string is even simple:
	const int _buffLength = 10;
	char* value = new char [ _buffLength + 1 ];

	// just for test
	strcpy( value, "2014-02-07" );

	int year, month, day;
	sscanf( value, "%d-%d-%d", &year, &month, &day );

Open in new window

But in your last post you declared value as void*. I'm not sure why you want to do it this way, it's much better to use a char* instead. Is this a must by intention? If yes you'll have to cast, i.e.:
	const int _buffLength = 10;
	void* value = new char [ _buffLength + 1 ];

	// just for test
	strcpy( (char*)value, "2014-02-07" );

	int year, month, day;
	sscanf( (char*)value, "%d-%d-%d", &year, &month, &day );

Open in new window

ZOPPO
0
 
LVL 1

Author Comment

by:naseeam
ID: 39842188
// value * void.  This is defined in our base class.  I believe it's pointer to void because we have following classes that derive from this base class:

class Bool
class Long
class LongLong
class short
class string

so value points to other data besides string.
0
 
LVL 1

Author Comment

by:naseeam
ID: 39842198
>> so value points to other data besides string.

I mean there are multiple instantiations of value.  In one instantiation, it will point to boolean date.  In another instantiation, it'll point to short data.   And so on.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 39842201
ok - then you'll have to case it as shown in the second sample, the rest should be clear I hope ...
0
 
LVL 1

Author Closing Comment

by:naseeam
ID: 39842217
Great answers to follow up questions.  This will solve my problem.  Fast response time.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 39842219
:o)

Thanks

Have a nice weekend ...

ZOPPO
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question