Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-02-11
5
Medium Priority
?
896 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
[X]
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
  • 3
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1600 total points
ID: 39852157
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
ID: 39852174
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
ID: 39852217
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 35

Assisted Solution

by:sarabande
sarabande earned 400 total points
ID: 39853159
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
 
LVL 1

Author Closing Comment

by:naseeam
ID: 39853250
Question answered.  sscanf ( ) explained very well.

Thanks for alternate solution, istringstream class.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

670 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