250-date-k-flag.patch 2.6 KB

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