LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-03-2011, 01:06 PM   #1
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Rep: Reputation: 18
Unhappy bug on localtime() function - linux problem? :O


Hi all,

Happy new year, happy new bugs.

I'm developing a log program with c++ on an embedded platform (ARM9). The program receive some status and write everything on a log (.txt) file.
Each line must contain the current date time and the status info, and there will be 4 or more lines per second to be 'logged'.

My problem starts when I try to get current date time. Sometimes the localtime() function doesn't work properly (1% or less of all tries):

Code:
 ...
1294044158 - | 03/01/2011 16:42:38:000 | 03/01/2011 16:42:38:000
1294044159 - | 03/01/2011 16:42:39:000 | 03/01/2011 16:42:39:000
1294044159 - | 03/01/2011 16:42:39:000 | 03/01/2011 16:42:39:000
1294044161 - | 03/01/2011 08:42:41:000 | 03/01/2011 08:42:41:000  <-- ERROR
1294044163 - | 03/01/2011 16:42:43:000 | 03/01/2011 16:42:43:000
1294044164 - | 03/01/2011 16:42:44:000 | 03/01/2011 16:42:44:000
 ...
1294044361 - | 03/01/2011 16:46:01:000 | 03/01/2011 16:46:01:000
1294044361 - | 03/01/2011 16:46:01:000 | 03/01/2011 16:46:01:000
1294044362 - | 03/01/2011 16:46:02:000 | 03/01/2011 16:46:02:000
1294044362 - | 03/01/2011 16:46:02:000 | 03/01/2011 16:46:02:000
1294044362 - | 03/01/2011 16:46:02:000 | 03/01/2011 16:46:02:000
1294044363 - | 03/01/2011 08:46:03:000 | 03/01/2011 08:46:03:000  <-- ERROR
1294044363 - | 03/01/2011 16:46:03:000 | 03/01/2011 16:46:03:000
1294044363 - | 03/01/2011 16:46:03:000 | 03/01/2011 16:46:03:000
1294044363 - | 03/01/2011 16:46:03:000 | 03/01/2011 16:46:03:000
 ...
I build a function to return the current date time (I put some debugs to find the problem):

Code:
int getCurrentDateTime(tm* dateTime) {
    time_t now;    
    tm* pLt = NULL;
    if ((now = time(NULL)) == (time_t) -1) {
        printf("error on time()\n"); //DEBUG
    }
    printf("%ld - ", now); //printing the time_t value
    pLt = localtime(&now);
    if (pLt != NULL) {
        //DEBUG - Checking the pLt structure
        printf("| %02d/%02d/%04d %02d:%02d:%02d:%03d | ", pLt->tm_mday,
            pLt->tm_mon + (1), pLt->tm_year + (1900), pLt->tm_hour,
            pLt->tm_min, pLt->tm_sec, 0);
        memcpy(dateTime, pLt, sizeof (tm));
        mktime(dateTime);
        //DEBUG - Checking the dateTime structure
        printf("%02d/%02d/%04d %02d:%02d:%02d:%03d\n", dateTime->tm_mday,
            dateTime->tm_mon + (1), dateTime->tm_year + (1900), dateTime->tm_hour,
            dateTime->tm_min, dateTime->tm_sec, 0);
    } else {
        printf("null pointer\n");
    }
    return OK;
}

The pattern is the hour, it's always 8 hours late. Anyone knows what is happening?
Am I missing something?


PS: The kernel version is 2.6.32.2.

Thanks in advance

Last edited by Lobinho; 01-03-2011 at 01:12 PM.
 
Old 01-03-2011, 01:27 PM   #2
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
With ctime() this error also occurs, but not at the same time as localtime().

Code:
printf("| %s | ", ctime(&now));
Example:
1294046684 - | Mon Jan 3 17:24:44 2011 <-- with ctime() OK
| | 03/01/2011 17:24:44:000 | 03/01/2011 17:24:44:000 <-- with localtime() OK
1294046684 - | Mon Jan 3 09:24:44 2011 <-- with ctime() ERROR
| | 03/01/2011 17:24:44:000 | 03/01/2011 17:24:44:000 <-- with localtime() OK
1294046685 - | Mon Jan 3 17:24:45 2011 <-- with ctime() OK
| | 03/01/2011 17:24:45:000 | 03/01/2011 17:24:45:000 <-- with localtime() OK
 
1 members found this post helpful.
Old 01-03-2011, 01:47 PM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Three things. Not from the Orient, but three things nonetheless.
  1. What time zone are you in? Any chance that sometimes localtime() and ctime() are actng as though you're in UTC, and sometimes not?
  2. The purpose of mktime() is to take the date/time fields in the struct and make something of type time_t as the resultant value of the function. Why bother to call mktime() if you're not going to use the value of the function?
  3. I have no experience with embedded system programming (none that I'm willing to admit to, anyway), but isn't it a good idea to make your code as reentrant as possible? Use localtime_r() or ctime_r(). Note that with localtime_r(), you'll need a local variable of type tm, not *tm. Similarly, for ctime_r, you'll need an actual array of characters as a local variable for the string to be written into.
 
1 members found this post helpful.
Old 01-04-2011, 04:54 AM   #4
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
Thanks for your response wje_lq,

1. My UTC is -3, but I'm not setting the timezone.
2. I was calling mktime() just hoping to normalize the structure and solve the problem, but this didn't work and doesn't make sense do that.
3. Excuse my ignorance, but what do you mean when you say that I should make the code more reentrant? I tried to understand the difference between a normal function and a reentrant function, but couldn't find a good explanation.

Anyway I'll follow your suggestion that make sense. I was reading this specification:
Quote:
The localtime_r() function shall convert the time in seconds since the Epoch pointed to by timer into a broken-down time stored in the structure to which result points. The localtime_r() function shall also return a pointer to that same structure.

Unlike localtime(), the reentrant version is not required to set tzname.

If the reentrant version does not set tzname, it shall not set daylight and shall not set timezone.
from http://pubs.opengroup.org/onlinepubs...localtime.html.
 
Old 01-04-2011, 05:09 AM   #5
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
I finished my tests and guess the problem is solved.

I guess I understood the meaning of make the code reentrant. And this is critical :O

http://en.wikipedia.org/wiki/Reentrant_%28subroutine%29

my function now is:
Code:
int getCurrentDateTime(tm* dateTime) {
    time_t now = time(NULL);
    localtime_r(&now, dateTime);
    return OK;
}

I think I'll have to take a look over all my code.

Thanks wje_lq.

Last edited by Lobinho; 01-04-2011 at 05:11 AM. Reason: added code
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bug in Linux time() function ? Or in Linux OS calls? sinu_nayak2001 Linux - Newbie 3 03-11-2010 12:02 AM
localtime() function is having problem lovelylinux Linux - Software 1 09-21-2007 02:09 AM
localtime() function is having problem with getenv(), received signal SIGSEGV, so seg lovelylinux Linux - Software 3 09-20-2007 10:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration