Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-02-07
9
Medium Priority
?
368 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 31

Accepted Solution

by:
Zoppo earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

916 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