Re: Strftime Mingw don't work

Discussion in 'C Programming' started by Lew Pitcher, Jan 2, 2013.

  1. Lew Pitcher

    Lew Pitcher Guest

    On Wednesday 02 January 2013 11:55, in comp.lang.c, wrote:

    >
    >
    > Hi all
    >
    > I try format date with compiler Mingw con function strftime.
    > time_t t;
    > char buffer[100];
    >
    > The option tha I use is:
    > strftime(buffer,sizeof(buffer),DayDate="%d.%B.%Y.%A Time%H:%M:%S
    > WeekYear=%U %W" ,(localtime ((time (&t), &t))));


    Hmmmm... this looks suspect to me

    First off, where is DayDate declared, and what data type is it?

    Second, the localtime() call takes two parameters: a const pointer to a
    time_t element, and a pointer to a struct tm. You provide localtime() with
    the proper time_t pointer element, but do not provide a struct tm pointer;
    instead, you reuse the time_t pointer. IMHO, besides the data-type
    mismatch, this code is dangerous, as you have no indication of how the
    localtime() call uses these two parameters; it could overwrite the data
    element pointed to by one pointer /while/ reading the data element pointed
    to by the other pointer, causing data corruption during the call.

    > printf ("BUFFER=%s\n", buffer);
    >
    > But the option: %U or %W don't work!


    What do you mean "option %U or %W don't work"?
    What do you get as results from strftime()?
    What do you expect as results from strftime()?

    > What is? Is a problem? is a bug?


    Probably a bug. In your code.

    Try to give us a better example. Preferrably verbatum from your code, with
    the resulting output, and your expected behaviour/output.

    Thanks
    --
    Lew Pitcher
    "In Skills, We Trust"
     
    Lew Pitcher, Jan 2, 2013
    #1
    1. Advertising

  2. Lew Pitcher

    James Kuyper Guest

    On 01/02/2013 12:18 PM, Lew Pitcher wrote:
    > On Wednesday 02 January 2013 11:55, in comp.lang.c, wrote:

    ....
    >> time_t t;
    >> char buffer[100];
    >>
    >> The option tha I use is:
    >> strftime(buffer,sizeof(buffer),DayDate="%d.%B.%Y.%A Time%H:%M:%S
    >> WeekYear=%U %W" ,(localtime ((time (&t), &t))));

    ....
    > Second, the localtime() call takes two parameters: a const pointer to a
    > time_t element, and a pointer to a struct tm. ...


    Not according to 7.27.3.4p1:
    "struct tm *localtime(const time_t *timer);"


    > ... You provide localtime() with
    > the proper time_t pointer element, but do not provide a struct tm pointer;
    > instead, you reuse the time_t pointer. ...


    I suspect that his syntax confused you. Because he inserted an extra
    pair of parentheses, that comma serves as a comma operator, not as a
    argument separator. He provided localtime with only one argument, of
    type time_t*, the correct type for that function. It's confusing syntax,
    but I think it's correct syntax.

    > ... IMHO, besides the data-type
    > mismatch, this code is dangerous, as you have no indication of how the
    > localtime() call uses these two parameters; it could overwrite the data
    > element pointed to by one pointer /while/ reading the data element pointed
    > to by the other pointer, causing data corruption during the call.


    The only parameter taken by localtime() is declared const time_t*, so it
    should not be used to write, only to read, which is consistent with the
    Description: "The localtime function converts the calendar time pointed
    to by timer into a broken-down time, expressed as local time."
     
    James Kuyper, Jan 2, 2013
    #2
    1. Advertising

  3. "Bertram" <> writes:
    > Ok, solved.


    Would you care to share some of the details, so others might benefit?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 3, 2013
    #3
  4. "Bertram" <> writes:
    > "Keith Thompson" <> ha scritto nel messaggio
    > news:...
    >> "Bertram" <> writes:
    >>> Ok, solved.

    >>
    >> Would you care to share some of the details, so others might benefit?

    >
    > I only change command:
    >
    > time_t t;
    > strftime(buffer,sizeof(buffer),"%d.%B.%Y.%A %H:%M:%S %j" ,(localtime ((time
    > (&t), &t))));
    >
    > with your example:
    > time_t t= time(NULL);
    > struct tm *t1 = localtime(&t);
    > strftime(buffer,sizeof (buffer),"%d.%B.%Y.%A %H:%M:%S %j %U", t1);
    >
    > This last seems more linear


    Agreed -- but the original code you posted, as far as I can tell, should
    have worked. You said the %U and %W formats "don't work", but you never
    told us *how* they don't work (how the output you got differs from what
    you expected). Could you have made other changes elsewhere in your
    program that fixed whatever problem you were seeing?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 4, 2013
    #4
  5. Lew Pitcher

    James Kuyper Guest

    On 01/04/2013 05:08 AM, Bertram wrote:
    ....
    > I only change command:
    >
    > time_t t;
    > strftime(buffer,sizeof(buffer),"%d.%B.%Y.%A %H:%M:%S %j" ,(localtime ((time
    > (&t), &t))));
    >
    > with your example:
    > time_t t= time(NULL);
    > struct tm *t1 = localtime(&t);
    > strftime(buffer,sizeof (buffer),"%d.%B.%Y.%A %H:%M:%S %j %U", t1);
    >
    > This last seems more linear


    That's a big improvement in readability, but it shouldn't change the
    actual behavior of your program. If the behavior of your program did
    change, it's because of something else that you haven't shown us.

    The biggest advantage of breaking that statement up into three separate
    statements is that it allows you to check for the possibility of
    failure. All three of those functions can fail, and if the second one
    fails, the third one has undefined behavior.
    --
    James Kuyper
     
    James Kuyper, Jan 4, 2013
    #5
  6. "Bertram" <> writes:
    > The program will work with this instructions:
    > time_t t;
    > strftime(buffer,sizeof(buffer),"%d.%B.%Y.%A %H:%M:%S %j" ,(localtime ((time
    > (&t), &t))));
    >
    > but I think with these instructions is more readable:
    >
    > time_t t= time(NULL);
    > struct tm *t1 = localtime(&t);
    > strftime(buffer,sizeof (buffer),"%d.%B.%Y.%A %H:%M:%S %j %U", t1);


    Yes, we know that; that's what we've been telling you.

    But in your original message, you showed us some code that you
    said wasn't working, but that, as far as I can tell, *should*
    have worked. What I'm trying to find out from you (mostly for the
    benefit of future readers who might learn something) is what your
    original code actually looked like (you only showed us a fragment),
    and exactly *how* it failed to work.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 4, 2013
    #6
  7. Lew Pitcher

    James Kuyper Guest

    On 01/04/2013 03:17 PM, Bertram wrote:
    > The program will work with this instructions:
    > time_t t;
    > strftime(buffer,sizeof(buffer),"%d.%B.%Y.%A %H:%M:%S %j" ,(localtime ((time
    > (&t), &t))));
    >
    > but I think with these instructions is more readable:


    Oddly enough, that's why I referred to it as "a big improvement in
    readability".

    > time_t t= time(NULL);
    > struct tm *t1 = localtime(&t);
    > strftime(buffer,sizeof (buffer),"%d.%B.%Y.%A %H:%M:%S %j %U", t1);


    The key point is, you said that "But the option: %U or %W don't work!".
    You have so far, despite repeated requests, failed to identify why you
    thought it didn't work. You've twice told us what change you made to the
    code - but the change you made is purely for readability - it shouldn't
    have had any effect on the output you see. Therefore, if you did see a
    change in the behavior, you must have made changes somewhere else, in a
    part of the code you haven't shown us.

    Keith has been asking you to answer questions about this for the benefit
    of anyone else who might run into the same problem. I think you should
    answer our questions for your own sake. Your comments pretty strongly
    suggest that you don't know what you changed that made %U and %W work
    the way you expected them to - therefore, you probably don't correctly
    understand why those changes had that effect. Answer our questions, and
    we might be able to help you understand what's going on.

    The key questions are these: Why did you think %U and %W didn't work
    before? What code did you write to test it? What did you expect that
    code to do? Why? What did that code actually do? What did you change
    (other than the part that you've already shown us) that made %U and %W
    actually work?
    --
    James Kuyper
     
    James Kuyper, Jan 5, 2013
    #7
  8. Lew Pitcher

    James Kuyper Guest

    On 01/05/2013 06:15 AM, Bertram wrote:
    > The option %U and %W, function regularly.


    So, are you trying to tell us that you were mistaken in your original
    message when you wrote "%U or %W don't work!"? If so, you could have
    said so more clearly.
    --
    James Kuyper
     
    James Kuyper, Jan 5, 2013
    #8
    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. Bryan O'Malley

    strftime not working as expected

    Bryan O'Malley, Feb 3, 2004, in forum: C++
    Replies:
    8
    Views:
    2,630
    Jerry Coffin
    Feb 17, 2004
  2. Sam Smith
    Replies:
    0
    Views:
    1,189
    Sam Smith
    Oct 18, 2004
  3. Bimal
    Replies:
    1
    Views:
    465
    Tim Prince
    Aug 20, 2003
  4. xeno fears
    Replies:
    1
    Views:
    502
    Martin v. Löwis
    Oct 4, 2009
  5. Keith Thompson

    Re: Strftime Mingw don't work

    Keith Thompson, Jan 2, 2013, in forum: C Programming
    Replies:
    0
    Views:
    324
    Keith Thompson
    Jan 2, 2013
Loading...

Share This Page