Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

year not displaying on linux

Tags:

c

linux

unix

Hope someone can help. I am fixing a problem in someone’s’ C code that was written a long time ago and he has since moved on.

The piece of code outputs the timestamp of a particular file. The code works fine when run on windows but when it is run on Linux it displays the Year incorrectly. The year is not displaying on linux, it shows 35222. Does anyone have any idea what is the problem here?

Thanks

Windows output:

Source file: test.dtl, Created: Mon, 27 May, 2013 at 16:13:20

Linux output:

Source file: test.dtl, Created: Mon, 27 May, 35222 at 16:13:20

The function in C code:

void SummaryReport ( report_t *report, char *dtlName)
{      
       LogEntry(L"SummaryReport entry\n");

       int                  i;
       wchar_t              *rootStrType,*localStr,timeStr[48];
       wchar_t              fileBuff[64];

       struct tm *timeVals;
       timeVals = localtime (&logHdr.date);
       wcsftime (timeStr,47,L"%a, %#d %b, %Y at %X",timeVals);

       /*  Print the header information  */
       DisplayReportFile (report);
       ReportEntry (report,L" Filesystem Audit Summary Report\n\n");
       ReportEntry (report,L"Source file: %s, Created: %ls\n\n",dtlName,timeStr);
       ReportEntry (report,L"Server: %ls",srvrName);
…
}
like image 854
user1257114 Avatar asked Nov 03 '22 19:11

user1257114


1 Answers

Verified on a minimal example and it "works-for-me". Does this show the right time?

#include <wchar.h>
#include <time.h>


int main() {
        wchar_t timeStr[48];
        struct tm *timeVals;
        time_t now = time(NULL);
        timeVals = localtime(&now);
        wcsftime(timeStr, 47, L"%a, %#d %b, %Y at %X", timeVals);
        wprintf(timeStr);
        return 0;
}

If yes, check the file itself - if you're sharing the filesystem, maybe there's some weird issue with the file timestamp itself? (or with understanding the fs metadata)

like image 194
viraptor Avatar answered Nov 10 '22 19:11

viraptor