X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Ftimedate%2Ftimedatectl.c;h=ef2ea0830006bfd1cfee75fb4745a008ebe5a5f5;hb=29f6aadd5302320d5a7a3868b607eb83687bdfd0;hp=b7f2422a16ee2172318e2a70b28ebe578dc4d689;hpb=f18ca9dcdeda247e208f7143e834fd2fb2070d80;p=elogind.git diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c index b7f2422a1..ef2ea0830 100644 --- a/src/timedate/timedatectl.c +++ b/src/timedate/timedatectl.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -50,7 +51,7 @@ static void pager_open_if_enabled(void) { if (arg_no_pager) return; - pager_open(); + pager_open(false); } static void polkit_agent_open_if_enabled(void) { @@ -82,29 +83,53 @@ static bool ntp_synced(void) { return true; } +static const char *jump_str(int delta_minutes, char *s, size_t size) { + if (delta_minutes == 60) + return "one hour forward"; + if (delta_minutes == -60) + return "one hour backwards"; + if (delta_minutes < 0) { + snprintf(s, size, "%i minutes backwards", -delta_minutes); + return s; + } + if (delta_minutes > 0) { + snprintf(s, size, "%i minutes forward", delta_minutes); + return s; + } + return ""; +} + static void print_status_info(StatusInfo *i) { usec_t n; char a[FORMAT_TIMESTAMP_MAX]; char b[FORMAT_TIMESTAMP_MAX]; + char s[32]; struct tm tm; time_t sec; char *zc, *zn; time_t t, tc, tn; + int dn; bool is_dstc, is_dstn; int r; assert(i); + /* enforce the values of /etc/localtime */ + if (getenv("TZ")) { + fprintf(stderr, "Warning: ignoring the TZ variable, reading the system's timezone setting only.\n\n"); + unsetenv("TZ"); + } + n = now(CLOCK_REALTIME); sec = (time_t) (n / USEC_PER_SEC); zero(tm); - assert_se(strftime(a, sizeof(a), "%a, %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) > 0); + assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) > 0); char_array_0(a); printf(" Local time: %s\n", a); zero(tm); - assert_se(strftime(a, sizeof(a), "%a, %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)) > 0); + assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)) > 0); char_array_0(a); printf(" Universal time: %s\n", a); @@ -114,16 +139,15 @@ static void print_status_info(StatusInfo *i) { /* Calculcate the week-day */ mktime(&tm); - assert_se(strftime(a, sizeof(a), "%a, %Y-%m-%d %H:%M:%S", &tm) > 0); + assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S", &tm) > 0); char_array_0(a); printf(" RTC time: %s\n", a); } zero(tm); - assert_se(strftime(a, sizeof(a), "%z", localtime_r(&sec, &tm)) > 0); + assert_se(strftime(a, sizeof(a), "%Z, %z", localtime_r(&sec, &tm)) > 0); char_array_0(a); - printf(" Timezone: %s\n" - " UTC offset: %s\n" + printf(" Timezone: %s (%s)\n" " NTP enabled: %s\n" "NTP synchronized: %s\n" " RTC in local TZ: %s\n", @@ -135,35 +159,37 @@ static void print_status_info(StatusInfo *i) { r = time_get_dst(sec, "/etc/localtime", &tc, &zc, &is_dstc, - &tn, &zn, &is_dstn); - if (r >= 0) { + &tn, &dn, &zn, &is_dstn); + if (r < 0) + printf(" DST active: n/a\n"); + else { printf(" DST active: %s\n", yes_no(is_dstc)); t = tc - 1; zero(tm); - assert_se(strftime(a, sizeof(a), "%a, %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0); + assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0); char_array_0(a); zero(tm); - assert_se(strftime(b, sizeof(b), "%a, %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm)) > 0); + assert_se(strftime(b, sizeof(b), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm)) > 0); char_array_0(b); - printf(" Last DST change: %s → %s, one hour %s\n" + printf(" Last DST change: DST %s at\n" " %s\n" " %s\n", - strna(zn), strna(zc), is_dstc ? "forward" : "backwards", a, b); + is_dstc ? "began" : "ended", a, b); t = tn - 1; zero(tm); - assert_se(strftime(a, sizeof(a), "%a, %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0); + assert_se(strftime(a, sizeof(a), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm)) > 0); char_array_0(a); zero(tm); - assert_se(strftime(b, sizeof(b), "%a, %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm)) > 0); + assert_se(strftime(b, sizeof(b), "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm)) > 0); char_array_0(b); - printf(" Next DST change: %s → %s, one hour %s\n" + printf(" Next DST change: DST %s (the clock jumps %s) at\n" " %s\n" " %s\n", - strna(zc), strna(zn), is_dstn ? "forward" : "backwards", a, b); + is_dstn ? "begins" : "ends", jump_str(dn, s, sizeof(s)), a, b); free(zc); free(zn); @@ -399,7 +425,6 @@ static int list_timezones(DBusConnection *bus, char **args, unsigned n) { _cleanup_fclose_ FILE *f = NULL; _cleanup_strv_free_ char **zones = NULL; size_t n_zones = 0; - char **i; assert(args); assert(n == 1); @@ -461,8 +486,7 @@ static int list_timezones(DBusConnection *bus, char **args, unsigned n) { pager_open_if_enabled(); strv_sort(zones); - STRV_FOREACH(i, zones) - puts(*i); + strv_print(zones); return 0; } @@ -525,7 +549,6 @@ static int parse_argv(int argc, char *argv[]) { case ARG_VERSION: puts(PACKAGE_STRING); - puts(DISTRIBUTION); puts(SYSTEMD_FEATURES); return 0; @@ -651,6 +674,7 @@ int main(int argc, char *argv[]) { dbus_error_init(&error); + setlocale(LC_ALL, ""); log_parse_environment(); log_open();