CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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?

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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!
    Last edited by 2kaud; March 6th, 2020 at 04:42 PM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Strange crash with strftime()

    Quote Originally Posted by 2kaud View Post
    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 ?? )
    Last edited by John E; March 7th, 2020 at 07:31 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  7. #7
    Join Date
    Mar 2020
    Posts
    5

    Re: Strange crash with strftime()

    localtime() directly returns the necessary value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured