We are using /usr/xpg4/bin as default path in our profile. We are printing the output of variable "curr_date" here:

   lt = time(NULL);
   ltime=localtime(localtime(&lt));
   strftime(curr_date,sizeof(curr_date),"%m/%d/%y%C",ltime);

We get the output as "06/27/13Thu Jun 27 02:39:34 PDT" instead of "06/27/1320".

Do you know what should be the format specifiers that should work here?

Thanks

有帮助吗?

解决方案

The use of /usr/xpg4/bin in your $PATH only selects the standard compliant commands, it does not change function calls in your programs to use the standards compliant versions.

As described in the Solaris standards(5) man page there are various #defines and compiler flags you need to use to specify compliance for various standards.

For instance, taking your code snippet and expanding it to this standalone test program:

#include <sys/types.h>
#include <time.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    time_t lt;
    struct tm *ltime;
    char curr_date[80];

    lt = time(NULL);
    ltime = localtime(&lt);
    strftime(curr_date, sizeof(curr_date), "%m/%d/%y%C", ltime); 
    printf("%s\n", curr_date);
    return 0;
}

Then compiling with the different flags shows the different behavior:

% cc -o /tmp/strftime /tmp/strftime.c
% /tmp/strftime
06/30/13Sun Jun 30 20:28:00 PDT 2013

% cc -xc99 -D_XOPEN_SOURCE=600 -o /tmp/strftime /tmp/strftime.c
% /tmp/strftime
06/30/1320

The default mode is backwards compatible with the traditional Solaris code, the second form requests compliance with the C99 and XPG6 (Unix03) standards.

其他提示

Have a good look at the code between call to strftime() and printing curr_date. You're overwriting curr_data somewhere, because the start of what you print is correct. Might also be something fishy with memory management of curr_data; how is it defined, did you allocate memory for curr_data?

Set a breakpoint right after strftime() and you'll see it holds the expected/correct string.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top