strptime and optional parameters

Discussion in 'C Programming' started by mathieu, Feb 11, 2011.

  1. mathieu

    mathieu Guest

    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
     
    mathieu, Feb 11, 2011
    #1
    1. Advertising

  2. mathieu <> writes:

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

    --
    Ben.
     
    Ben Bacarisse, Feb 11, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Do
    Replies:
    1
    Views:
    67,363
    Kevin Spencer
    Oct 16, 2003
  2. Hanover

    SQLCommand with Optional Parameters?

    Hanover, Sep 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,472
    rviray
    Sep 28, 2005
  3. Joerg Toellner
    Replies:
    4
    Views:
    1,174
    Joerg Toellner
    Jul 16, 2003
  4. kongkolvyu
    Replies:
    4
    Views:
    491
    Old Wolf
    Jan 4, 2006
  5. PMCG

    WebMethod and WSDL with optional parameters

    PMCG, Mar 2, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    795
    Jan Tielens
    Mar 2, 2004
Loading...

Share This Page