I am working on an embedded Linux system (5.10.24) which has fixed Y2038.
I did following tests in target system (with busybox 1.36), and got puzzled on the tests.
- I ran
date -s "2038-01-19 03:14:10", it worked fine with fix of Y2038 (I added printf indate_main()to show thetv_secgot fromtime()called). - I ran
datethen, I can read the right date and time. - I wrote a C program did the same thing as
date_maindoes in busybox, and ran it in target to get the date and time, but I got wrong data.
Here comes the log.
#
# date -s "2038-01-19 03:14:10"
tm.year: 138, tm.mon: 0, tm.mday: 19
tm.year: 138, tm.mon: 0, tm.mday: 19
Tue Jan 19 03:14:10 UTC 2038
#
# date && sleep 2 && date
XXXXXXXXXXXXXXXXX 2139132224
tm.year: 138, tm.mon: 0, tm.mday: 19
tm.year: 138, tm.mon: 0, tm.mday: 19
Tue Jan 19 03:14:16 UTC 2038
XXXXXXXXXXXXXXXXX 2142846048
tm.year: 138, tm.mon: 0, tm.mday: 19
tm.year: 138, tm.mon: 0, tm.mday: 19
Tue Jan 19 03:14:18 UTC 2038
#
#
# /tmp/timedate
And: Sat Sep 24 01:41:40 UTC 2033
Asciitime: 24:8:133
From the logs there were 2 things strange.
date && sleep 2 && datecalled date 2 times, I assumed thetv_secreturned should be correct with 2 seconds. But they are NOT.- my program of
timedatefollowed the SAME logic asdate_main()of busybox to get date and time, but the date shown is totally wrong.
Here is my change to date_main() in busybox.
259 memset(&ts, 0, sizeof(ts)); /// Added
260 time(&ts.tv_sec);
261 printf("XXXXXXXXXXXXXXXXX %ld\n", ts.tv_sec); /// Added.
Here is my timedate.c,
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <asm/unistd.h>
#include <stdint.h>
#define COMMON_BUFSIZE 128
int main()
{
struct timeval tv;
char buf_fmt_dt2str[64];
char date_buf[128] = {0};
char *fmt_dt2str = buf_fmt_dt2str;
struct tm tm_time;
time(&tv.tv_sec);
localtime_r(&tv.tv_sec, &tm_time);
fmt_dt2str = (char*)"%a %b %e %H:%M:%S %Z %Y";
strftime(date_buf, COMMON_BUFSIZE, fmt_dt2str, &tm_time);
printf("And: %s\n", date_buf);
printf("Asciitime: %d:%d:%d\n", tm_time.tm_mday, tm_time.tm_mon, tm_time.tm_year);
return 0;
}
I am puzzled COMPLETELY with the testing result.