chiark / gitweb /
systemctl: when "systemctl status" is called without arguments show a short overall...
[elogind.git] / src / timedate / timedatectl.c
index 2267733d9126a242d06b3ac34ccd1e2d142127c9..089a5fe3efbc21c22eecb2b56acb8e6b712cba56 100644 (file)
@@ -98,10 +98,11 @@ static void print_status_info(const StatusInfo *i) {
         char s[32];
         struct tm tm;
         time_t sec;
-        char *zc, *zn;
+        bool have_time = false;
+        _cleanup_free_ char *zc = NULL, *zn = NULL;
         time_t t, tc, tn;
-        int dn;
-        bool is_dstc, is_dstn;
+        int dn = 0;
+        bool is_dstc = false, is_dstn = false;
         int r;
 
         assert(i);
@@ -112,17 +113,29 @@ static void print_status_info(const StatusInfo *i) {
                 unsetenv("TZ");
         }
 
-        sec = (time_t) (i->time / USEC_PER_SEC);
+        if (i->time != 0) {
+                sec = (time_t) (i->time / USEC_PER_SEC);
+                have_time = true;
+        } else if (arg_transport == BUS_TRANSPORT_LOCAL) {
+                sec = time(NULL);
+                have_time = true;
+        } else
+                fprintf(stderr, "Warning: could not get time from timedated and not operating locally.\n\n");
 
-        zero(tm);
-        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);
+        if (have_time) {
+                zero(tm);
+                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);
-        char_array_0(a);
-        printf("  Universal 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);
+                char_array_0(a);
+                printf("  Universal time: %s\n", a);
+        } else {
+                printf("      Local time: %s\n", "n/a");
+                printf("  Universal time: %s\n", "n/a");
+        }
 
         if (i->rtc_time > 0) {
                 time_t rtc_sec;
@@ -133,7 +146,7 @@ static void print_status_info(const StatusInfo *i) {
                 char_array_0(a);
                 printf("        RTC time: %s\n", a);
         } else
-                printf("        RTC time: n/a\n");
+                printf("        RTC time: %s\n", "n/a");
 
         zero(tm);
         assert_se(strftime(a, sizeof(a), "%Z, %z", localtime_r(&sec, &tm)) > 0);
@@ -151,8 +164,8 @@ static void print_status_info(const StatusInfo *i) {
                          &tc, &zc, &is_dstc,
                          &tn, &dn, &zn, &is_dstn);
         if (r < 0)
-                printf("      DST active: n/a\n");
-        else {
+                printf("      DST active: %s\n", "n/a");
+        else if (have_time) {
                 printf("      DST active: %s\n", yes_no(is_dstc));
 
                 t = tc - 1;
@@ -180,10 +193,8 @@ static void print_status_info(const StatusInfo *i) {
                        "                  %s\n"
                        "                  %s\n",
                        is_dstn ? "begins" : "ends", jump_str(dn, s, sizeof(s)), a, b);
-
-                free(zc);
-                free(zn);
-        }
+        } else
+                printf("      DST active: %s\n", yes_no(is_dstc));
 
         if (i->rtc_local)
                 fputs("\n" ANSI_HIGHLIGHT_ON
@@ -194,7 +205,6 @@ static void print_status_info(const StatusInfo *i) {
 }
 
 static int show_status(sd_bus *bus, char **args, unsigned n) {
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         StatusInfo info = {};
         static const struct bus_properties_map map[]  = {
                 { "Timezone",        "s", NULL, offsetof(StatusInfo, timezone) },
@@ -215,8 +225,10 @@ static int show_status(sd_bus *bus, char **args, unsigned n) {
                                    "/org/freedesktop/timedate1",
                                    map,
                                    &info);
-        if (r < 0)
+        if (r < 0) {
+                log_error("Failed to query server: %s", strerror(-r));
                 goto fail;
+        }
 
         print_status_info(&info);
 
@@ -411,21 +423,20 @@ static int help(void) {
 
         printf("%s [OPTIONS...] COMMAND ...\n\n"
                "Query or change system time and date settings.\n\n"
-               "  -h --help              Show this help\n"
-               "     --version           Show package version\n"
-               "     --no-pager          Do not pipe output into a pager\n"
-               "     --no-ask-password   Do not prompt for password\n"
-               "  -H --host=[USER@]HOST  Operate on remote host\n"
-               "  -M --machine=CONTAINER Operate on local container\n"
-               "     --adjust-system-clock\n"
-               "                         Adjust system clock when changing local RTC mode\n\n"
+               "  -h --help                Show this help\n"
+               "     --version             Show package version\n"
+               "     --no-pager            Do not pipe output into a pager\n"
+               "     --no-ask-password     Do not prompt for password\n"
+               "  -H --host=[USER@]HOST    Operate on remote host\n"
+               "  -M --machine=CONTAINER   Operate on local container\n"
+               "     --adjust-system-clock Adjust system clock when changing local RTC mode\n\n"
                "Commands:\n"
-               "  status                 Show current time settings\n"
-               "  set-time TIME          Set system time\n"
-               "  set-timezone ZONE      Set system timezone\n"
-               "  list-timezones         Show known timezones\n"
-               "  set-local-rtc BOOL     Control whether RTC is in local time\n"
-               "  set-ntp BOOL           Control whether NTP is enabled\n",
+               "  status                   Show current time settings\n"
+               "  set-time TIME            Set system time\n"
+               "  set-timezone ZONE        Set system timezone\n"
+               "  list-timezones           Show known timezones\n"
+               "  set-local-rtc BOOL       Control whether RTC is in local time\n"
+               "  set-ntp BOOL             Control whether NTP is enabled\n",
                program_invocation_short_name);
 
         return 0;