Question on how does `date` command in busybox work well with Y2038 fix

79 Views Asked by At

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.

  1. I ran date -s "2038-01-19 03:14:10", it worked fine with fix of Y2038 (I added printf in date_main() to show the tv_sec got from time() called).
  2. I ran date then, I can read the right date and time.
  3. I wrote a C program did the same thing as date_main does 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.

  1. date && sleep 2 && date called date 2 times, I assumed the tv_sec returned should be correct with 2 seconds. But they are NOT.
  2. my program of timedate followed the SAME logic as date_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.

0

There are 0 best solutions below