]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
time: split get_timezone() into main function and zone1970.tab function
authorDan Streetman <ddstreet@canonical.com>
Wed, 30 Jun 2021 11:17:22 +0000 (07:17 -0400)
committerDan Streetman <ddstreet@canonical.com>
Wed, 30 Jun 2021 17:42:58 +0000 (13:42 -0400)
This allows for adding another function to read from a different timezone
source, which is added in the next commit.

src/basic/time-util.c

index c6bdf01d12207028a0af05ecf78607f97d2fad50..c874cfec8941e33efae8a0d88cecf7494fbf8091 100644 (file)
@@ -1262,7 +1262,7 @@ int parse_nsec(const char *t, nsec_t *nsec) {
         return 0;
 }
 
-int get_timezones(char ***ret) {
+static int get_timezones_from_zone1970_tab(char ***ret) {
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_strv_free_ char **zones = NULL;
         int r;
@@ -1270,34 +1270,48 @@ int get_timezones(char ***ret) {
         assert(ret);
 
         f = fopen("/usr/share/zoneinfo/zone1970.tab", "re");
-        if (f) {
-                for (;;) {
-                        _cleanup_free_ char *line = NULL, *cc = NULL, *co = NULL, *tz = NULL;
+        if (!f)
+                return -errno;
 
-                        r = read_line(f, LONG_LINE_MAX, &line);
-                        if (r < 0)
-                                return r;
-                        if (r == 0)
-                                break;
+        for (;;) {
+                _cleanup_free_ char *line = NULL, *cc = NULL, *co = NULL, *tz = NULL;
 
-                        const char *p = line;
+                r = read_line(f, LONG_LINE_MAX, &line);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        break;
 
-                        /* Line format is:
-                         * 'country codes' 'coordinates' 'timezone' 'comments' */
-                        r = extract_many_words(&p, NULL, 0, &cc, &co, &tz, NULL);
-                        if (r < 0)
-                                continue;
+                const char *p = line;
 
-                        /* Lines that start with # are comments. */
-                        if (*cc == '#')
-                                continue;
+                /* Line format is:
+                 * 'country codes' 'coordinates' 'timezone' 'comments' */
+                r = extract_many_words(&p, NULL, 0, &cc, &co, &tz, NULL);
+                if (r < 0)
+                        continue;
 
-                        r = strv_extend(&zones, tz);
-                        if (r < 0)
-                                return r;
-                }
-        } else if (errno != ENOENT)
-                return -errno;
+                /* Lines that start with # are comments. */
+                if (*cc == '#')
+                        continue;
+
+                r = strv_extend(&zones, tz);
+                if (r < 0)
+                        return r;
+        }
+
+        *ret = TAKE_PTR(zones);
+        return 0;
+}
+
+int get_timezones(char ***ret) {
+        _cleanup_strv_free_ char **zones = NULL;
+        int r;
+
+        assert(ret);
+
+        r = get_timezones_from_zone1970_tab(&zones);
+        if (r < 0 && r != -ENOENT)
+                return r;
 
         /* Always include UTC */
         r = strv_extend(&zones, "UTC");