Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Strftime Mingw don't work (http://www.velocityreviews.com/forums/t956066-re-strftime-mingw-dont-work.html)

Lew Pitcher 01-02-2013 05:18 PM

Re: Strftime Mingw don't work
 
On Wednesday 02 January 2013 11:55, in comp.lang.c, nospam@nospam.org 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"

James Kuyper 01-02-2013 05:34 PM

Re: Strftime Mingw don't work
 
On 01/02/2013 12:18 PM, Lew Pitcher wrote:
> On Wednesday 02 January 2013 11:55, in comp.lang.c, nospam@nospam.org 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."

Keith Thompson 01-03-2013 04:25 PM

Re: Strftime Mingw don't work
 
"Bertram" <nospam@nospam.org> writes:
> Ok, solved.


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

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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 01-04-2013 04:39 PM

Re: Strftime Mingw don't work
 
"Bertram" <nospam@nospam.org> writes:
> "Keith Thompson" <kst-u@mib.org> ha scritto nel messaggio
> news:lnhamy5j3i.fsf@nuthaus.mib.org...
>> "Bertram" <nospam@nospam.org> 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) kst-u@mib.org <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"

James Kuyper 01-04-2013 06:48 PM

Re: Strftime Mingw don't work
 
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

Keith Thompson 01-04-2013 10:03 PM

Re: Strftime Mingw don't work
 
"Bertram" <nospam@nospam.org> 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) kst-u@mib.org <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"

James Kuyper 01-05-2013 03:25 AM

Re: Strftime Mingw don't work
 
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 01-05-2013 06:01 PM

Re: Strftime Mingw don't work
 
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


All times are GMT. The time now is 10:03 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.