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

Posted on 2014-02-11
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.
Question by:naseeam
  • 3
LVL 86

Accepted Solution

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

LVL 86

Expert Comment

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.
LVL 86

Expert Comment

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.
LVL 33

Assisted Solution

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.


Author Closing Comment

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

Thanks for alternate solution, istringstream class.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TTimer no longer functioning 7 84
Getting IP address 8 71
How to print into std::string like sprintf using C++11? 12 48
Path to  STL Map header file 1 41
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

919 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

17 Experts available now in Live!

Get 1:1 Help Now