chiark / gitweb /
journal: log user units for coredumps and show them in systemctl status
[elogind.git] / src / timedate / timedatectl.c
index 2b006ce924a552cf1577d265cce4c4a2d648ffa2..f5b5f0ca84e9accc702b1a4f8e4164de42ee8bd2 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdbool.h>
 #include <unistd.h>
 #include <getopt.h>
+#include <locale.h>
 #include <string.h>
 #include <sys/timex.h>
 
@@ -33,6 +34,7 @@
 #include "hwclock.h"
 #include "strv.h"
 #include "pager.h"
+#include "time-dst.h"
 
 static bool arg_adjust_system_clock = false;
 static bool arg_no_pager = false;
@@ -81,27 +83,55 @@ 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(b, sizeof(b), "%a, %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) > 0);
-        char_array_0(b);
-        printf("      Local time: %s\n", b);
+        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(b, sizeof(b), "%a, %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)) > 0);
-        char_array_0(b);
-        printf("  Universal time: %s\n", b);
+        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);
         r = hwclock_get_time(&tm);
@@ -109,20 +139,62 @@ static void print_status_info(StatusInfo *i) {
                 /* Calculcate the week-day */
                 mktime(&tm);
 
-                assert_se(strftime(b, sizeof(b), "%a, %Y-%m-%d %H:%M:%S", &tm) > 0);
-                char_array_0(b);
-                printf("        RTC time: %s\n", b);
+                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);
         }
 
-        printf("        Timezone: %s\n"
+        zero(tm);
+        assert_se(strftime(a, sizeof(a), "%Z, %z", localtime_r(&sec, &tm)) > 0);
+        char_array_0(a);
+        printf("        Timezone: %s (%s)\n"
                "     NTP enabled: %s\n"
                "NTP synchronized: %s\n"
                " RTC in local TZ: %s\n",
                strna(i->timezone),
+               a,
                yes_no(i->ntp),
                yes_no(ntp_synced()),
                yes_no(i->local_rtc));
 
+        r = time_get_dst(sec, "/etc/localtime",
+                         &tc, &zc, &is_dstc,
+                         &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);
+                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);
+                char_array_0(b);
+                printf(" Last DST change: DST %s at\n"
+                       "                  %s\n"
+                       "                  %s\n",
+                       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);
+                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);
+                char_array_0(b);
+                printf(" Next DST change: DST %s (the clock jumps %s) at\n"
+                       "                  %s\n"
+                       "                  %s\n",
+                       is_dstn ? "begins" : "ends", jump_str(dn, s, sizeof(s)), a, b);
+
+                free(zc);
+                free(zn);
+        }
+
         if (i->local_rtc)
                 fputs("\n" ANSI_HIGHLIGHT_ON
                       "Warning: The RTC is configured to maintain time in the local time zone. This\n"
@@ -349,17 +421,10 @@ static int set_ntp(DBusConnection *bus, char **args, unsigned n) {
                         DBUS_TYPE_INVALID);
 }
 
-static int zone_compare(const void *_a, const void *_b) {
-        const char **a = (const char**) _a, **b = (const char**) _b;
-
-        return strcmp(*a, *b);
-}
-
 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);
@@ -416,14 +481,12 @@ static int list_timezones(DBusConnection *bus, char **args, unsigned n) {
         }
 
         if (zones)
-                zones[n_zones] = 0;
-
-        qsort(zones, n_zones, sizeof(char*), zone_compare);
+                zones[n_zones] = NULL;
 
         pager_open_if_enabled();
 
-        STRV_FOREACH(i, zones)
-                puts(*i);
+        strv_sort(zones);
+        strv_print(zones);
 
         return 0;
 }
@@ -486,7 +549,6 @@ static int parse_argv(int argc, char *argv[]) {
 
                 case ARG_VERSION:
                         puts(PACKAGE_STRING);
-                        puts(DISTRIBUTION);
                         puts(SYSTEMD_FEATURES);
                         return 0;
 
@@ -612,6 +674,7 @@ int main(int argc, char *argv[]) {
 
         dbus_error_init(&error);
 
+        setlocale(LC_ALL, "");
         log_parse_environment();
         log_open();