250-date-k-flag.patch 2.7 KB

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