Solved

Using sscanf ( ), how to extract date from buffer that contain combined date and time  ?

Posted on 2014-02-11
5
757 Views
Last Modified: 2014-02-12
iso8601_date_time_buf is pointer to const char.  It's length is Twenty bytes.  It contains combined date and time in format:   YYYY-MM-DDTHH:MM:SS
T is delimiter between date and time.  

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

Open in new window


A delimiter is followed by first two %d.  Do I need a delimiter after 3rd %d because day is in the middle of the buffer.
0
Comment
Question by:naseeam
  • 3
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
Comment Utility
Just follow the elimter scheme as in the string you want to parse, e.g.

int year;
int month;
int day;
int hours;
int minutes;
int seconds;
				
sscanf (iso8601_date_time_buf, "%d-%d-%dT%d:%d:%d", &year, &month, &day, &hours, &minutes, &seconds );

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Just in cae I misunderstood your question: Using 'sscanf()', it's necessary to parse all the variable parts of your input string, otherwise you'll run into trouble.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Or, a bit more general, independant from the parsing method you choose (and there are plenty): You have to parse all variable fields in order to achieve a correct result, since otherwise all methods are doomed to fail at these very fields by definition.
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 100 total points
Comment Utility
using c++ you would use stringstream like

char iso8601_date_time_buf[] = "2014-02-12 14:18:00";
std::istringstream iss(iso8601_date_time_buf);
int d, m, y, h, mi, s;
char c;
if (iss >> y >> c >> m >> c >> d >> c >> h >> c >> mi >> c >> s)
{

Open in new window


you also could parse the date only and omit the time part or check each part separately:

if (iss>>y>>c)
{
     if (iss>>m>>c)
     {
            if (iss>>d)
            {

Open in new window


that would allow to give a specific error in the else parts.

Sara
0
 

Author Closing Comment

by:naseeam
Comment Utility
Question answered.  sscanf ( ) explained very well.

Thanks for alternate solution, istringstream class.
0

Featured Post

Highfive Gives IT Their Time Back

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!

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now