Solved

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

Posted on 2014-02-07
9
357 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
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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 30

Accepted Solution

by:
Zoppo earned 500 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:naseeam
Comment Utility
// 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
Comment Utility
>> 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 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
Great answers to follow up questions.  This will solve my problem.  Fast response time.
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
:o)

Thanks

Have a nice weekend ...

ZOPPO
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
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.

771 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

9 Experts available now in Live!

Get 1:1 Help Now