Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

 
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

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

Suggested Solutions

Title # Comments Views Activity
Create string splitting user defined function in C++ 23 314
how to understand recursion 12 229
Arduino EDI - Programming language 3 103
Least Squares Curve Fitting 4 96
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

766 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