strptime and optional parameters

M

mathieu

Hi,

I am trying to use strptime to read a time formated using format: "%Y
%m%d%H%M%S"
However in my case only %Y is really compulsory. Is there a way to
specify a parameter is not compulsory ?
For instance my input could simply be: "%Y%m%d" or even "%Y%m" (this
has to be left to right)

Thanks
 
B

Ben Bacarisse

mathieu said:
I am trying to use strptime

strptime is not a standard C function though it is in POSIX.
to read a time formated using format: "%Y
%m%d%H%M%S"

That would be ambiguous. It's usually better to have some punctuation
in the format.
However in my case only %Y is really compulsory. Is there a way to
specify a parameter is not compulsory ?
For instance my input could simply be: "%Y%m%d" or even "%Y%m" (this
has to be left to right)

I think the best solution is to use the standard C function sscanf
because it can tell you how many inputs "matched". This would be more
fussy were it not for the fact that you are using only numeric fields.

sscanf allows you to match fixed-width fields which may be what you
intended with your punctuation-free format.

How you organise things depends on whether there is single obvious
default value for missing fields, or whether you need to set these in
some what that depends on the number of fields found. Also, you may or
may not want to validate some of or all of the input. Given all these
options, I'm reluctant to offer a code fragment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top