[package] busybox: change the date -k patch to be more portable
[openwrt.git] / package / busybox / patches / 911-date-k-flag.patch
1 diff -urN busybox-1.19.4/coreutils/date.c busybox-1.19.4.new/coreutils/date.c
2 --- busybox-1.19.4/coreutils/date.c     2012-02-04 20:24:55.000000000 +0100
3 +++ busybox-1.19.4.new/coreutils/date.c 2012-11-23 16:48:21.945200539 +0100
4 @@ -123,6 +123,7 @@
5  //usage:       IF_FEATURE_DATE_ISOFMT(
6  //usage:     "\n       -D FMT          Use FMT for -d TIME conversion"
7  //usage:       )
8 +//usage:     "\n       -k              Set Kernel timezone from localtime and exit"
9  //usage:     "\n"
10  //usage:     "\nRecognized TIME formats:"
11  //usage:     "\n       hh:mm[:ss]"
12 @@ -135,6 +136,7 @@
13  //usage:       "Wed Apr 12 18:52:41 MDT 2000\n"
14  
15  #include "libbb.h"
16 +#include <sys/time.h>
17  #if ENABLE_FEATURE_DATE_NANO
18  # include <sys/syscall.h>
19  #endif
20 @@ -145,8 +147,9 @@
21         OPT_UTC       = (1 << 2), /* u */
22         OPT_DATE      = (1 << 3), /* d */
23         OPT_REFERENCE = (1 << 4), /* r */
24 -       OPT_TIMESPEC  = (1 << 5) * ENABLE_FEATURE_DATE_ISOFMT, /* I */
25 -       OPT_HINT      = (1 << 6) * ENABLE_FEATURE_DATE_ISOFMT, /* D */
26 +       OPT_KERNELTZ  = (1 << 5), /* k */
27 +       OPT_TIMESPEC  = (1 << 6) * ENABLE_FEATURE_DATE_ISOFMT, /* I */
28 +       OPT_HINT      = (1 << 7) * ENABLE_FEATURE_DATE_ISOFMT, /* D */
29  };
30  
31  static void maybe_set_utc(int opt)
32 @@ -164,12 +167,15 @@
33         /*      "universal\0" No_argument       "u" */
34                 "date\0"      Required_argument "d"
35                 "reference\0" Required_argument "r"
36 +               "set-kernel-tz\0" No_argument   "k"
37                 ;
38  #endif
39  
40  int date_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
41  int date_main(int argc UNUSED_PARAM, char **argv)
42  {
43 +       time_t tt;
44 +       struct timezone tz;
45         struct timespec ts;
46         struct tm tm_time;
47         char buf_fmt_dt2str[64];
48 @@ -184,7 +190,7 @@
49         opt_complementary = "d--s:s--d"
50                 IF_FEATURE_DATE_ISOFMT(":R--I:I--R");
51         IF_LONG_OPTS(applet_long_options = date_longopts;)
52 -       opt = getopt32(argv, "Rs:ud:r:"
53 +       opt = getopt32(argv, "Rs:ud:r:k"
54                         IF_FEATURE_DATE_ISOFMT("I::D:"),
55                         &date_str, &date_str, &filename
56                         IF_FEATURE_DATE_ISOFMT(, &isofmt_arg, &fmt_str2dt));
57 @@ -241,6 +247,31 @@
58         if (*argv)
59                 bb_show_usage();
60  
61 +       /* Setting of kernel timezone was requested */
62 +       if (opt & OPT_KERNELTZ) {
63 +               tt = time(NULL);
64 +               localtime_r(&tt, &tm_time);
65 +
66 +               /* workaround warp_clock() on first invocation */
67 +               memset(&tz, 0, sizeof(tz));
68 +               settimeofday(NULL, &tz);
69 +
70 +               memset(&tz, 0, sizeof(tz));
71 +#ifdef __USE_BSD
72 +               tz.tz_minuteswest = -(tm_time.tm_gmtoff / 60);
73 +#else
74 +               tz.tz_minuteswest = -(tm_time.__tm_gmtoff / 60);
75 +#endif
76 +
77 +               if (settimeofday(NULL, &tz))
78 +               {
79 +                       bb_perror_msg("can't set kernel time zone");
80 +                       return EXIT_FAILURE;
81 +               }
82 +
83 +               return EXIT_SUCCESS;
84 +       }
85 +
86         /* Now we have parsed all the information except the date format
87          * which depends on whether the clock is being set or read */
88