Strange crash with strftime()
Can anyone see what I'm doing wrong here..?
Code:
#include <time.h>
struct tm local_time;
char timebuf[128];
time_t n = time (NULL);
memcpy (&local_time, localtime(&n), sizeof(struct tm));
// This works fine
strftime (timebuf, sizeof(timebuf), " ", &local_time);
// But this crashes with any of the formatting codes (in between the quote marks)
strftime (timebuf, sizeof(timebuf), "%T", &local_time);
According to the Debug window, local_time contains valid info after the call to memcpy() - and yet strftime() fails, no matter what I put in between the quotation marks :confused:
Re: Strange crash with strftime()
I am wondering if the call to memcpy is the issue. What happens if you use the value returned by localtime() directly?
Re: Strange crash with strftime()
Is this C or C++? localtime() can return NULL. This compiles and runs OK using MS VS2019:
Code:
#include <time.h>
#include <stdio.h>
int main()
{
struct tm local_time;
char timebuf[128];
time_t n = time(NULL);
struct tm* lt = localtime(&n);
if (lt != NULL) {
memcpy(&local_time, lt, sizeof(struct tm));
strftime(timebuf, sizeof(timebuf), "%d/%m/%Y", &local_time);
printf("%s\n", timebuf);
strftime(timebuf, sizeof(timebuf), "%T", &local_time);
printf("%s\n", timebuf);
} else
puts("Bad local time\n");
}
displaying:
Code:
06/03/2020
21:21:03
PS Earlier versions of the CRT didn't support %T. Using a non-supported format code can cause strftime to crash. Try "%H:%M:%S" instead of "%T". If that works, you need to update!
Re: Strange crash with strftime()
Thanks guys - using the value returned by localtime() directly doesn't help. At first I wasn't sure how 'valid' the pointer was which is why I decided to copy the data with memcpy() (e.g. can the data become invalidated if something else calls localtime() before I've finished with the pointer??)
Anyway... after some further research I've discovered that strftime() has a reputation for behaving differently with different compilers. This app uses the old VS2008 (mainly because it uses libraries that were built with VS2008) but up until now I haven't found a single formatting parameter that works (they all crash it with the error Invalid format directive).
Is there anywhere where I can find out which formatting parameters were considered valid back then?
Re: Strange crash with strftime()
These are the VS2008 strftime() valid formatting codes. If these don't work then you might have a CRT issue and may have to re-install the VS2008 CRT.
Note that you don't actually need the memcpy in this case. localtime() uses a single static struct per thread and if valid returns a pointer to this internal struct. Hence multiple calls to localtime() will overwrite the data in memory to which the returned pointer refers. The only time you need to copy the contents is if you use multiple calls to localtime() and need to keep previous results.
Code:
The formatting codes for strftime are listed below:
%a
Abbreviated weekday name
%A
Full weekday name
%b
Abbreviated month name
%B
Full month name
%c
Date and time representation appropriate for locale
%d
Day of month as decimal number (01 – 31)
%H
Hour in 24-hour format (00 – 23)
%I
Hour in 12-hour format (01 – 12)
%j
Day of year as decimal number (001 – 366)
%m
Month as decimal number (01 – 12)
%M
Minute as decimal number (00 – 59)
%p
Current locale's A.M./P.M. indicator for 12-hour clock
%S
Second as decimal number (00 – 59)
%U
Week of year as decimal number, with Sunday as first day of week (00 – 53)
%w
Weekday as decimal number (0 – 6; Sunday is 0)
%W
Week of year as decimal number, with Monday as first day of week (00 – 53)
%x
Date representation for current locale
%X
Time representation for current locale
%y
Year without century, as decimal number (00 – 99)
%Y
Year with century, as decimal number
%z, %Z
Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown
%%
Percent sign
Re: Strange crash with strftime()
Quote:
Originally Posted by
2kaud
localtime() uses a single static struct per thread and if valid returns a pointer to this internal struct
That's great 2kaud. I figured it'd be an internal struct but I hadn't realised there'd be different structs for each thread (that gives me a bit more confidence !!)
And according to your list it looks like %T isn't supported (and %F might also be causing problems for me). So I'll need to figure out a %F replacement if possible...
(I'm wondering if %F might be locale aware maybe ?? )
Re: Strange crash with strftime()
localtime() directly returns the necessary value.