Solved

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

Posted on 2014-02-11
5
832 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 400 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 34

Assisted Solution

by:sarabande
sarabande earned 100 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
Which Linux flavors will this run on? 6 102
Issues with C++ Class 19 109
Why isn't object file created? 6 94
How to read a c++ line with strings and variable numbers of elements 6 45
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 clear a vector as well as how to detect empty vectors in C++.
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.

730 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