Solved

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

Posted on 2014-02-07
9
358 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
 

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
 

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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
 

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
 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now