[SOLVED] bug on localtime() function - linux problem? :O
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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):
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
Three things. Not from the Orient, but three things nonetheless.
What time zone are you in? Any chance that sometimes localtime() and ctime() are actng as though you're in UTC, and sometimes not?
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?
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. 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.