250-date-k-flag.patch 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. @@ -139,9 +140,8 @@
  12. #include "libbb.h"
  13. #include "common_bufsiz.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. @@ -149,8 +149,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. #if ENABLE_LONG_OPTS
  32. @@ -162,6 +163,7 @@ 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. @@ -181,6 +183,8 @@ static void maybe_set_utc(int opt)
  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. @@ -193,7 +197,7 @@ int date_main(int argc UNUSED_PARAM, cha
  49. char *isofmt_arg = NULL;
  50. opt = getopt32long(argv, "^"
  51. - "Rs:ud:r:"
  52. + "Rs:ud:r:k"
  53. IF_FEATURE_DATE_ISOFMT("I::D:")
  54. "\0"
  55. "d--s:s--d"
  56. @@ -256,6 +260,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 */