]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
time-util: shorten code a bit
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 13 Feb 2023 19:27:52 +0000 (04:27 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 23 Feb 2023 23:55:27 +0000 (08:55 +0900)
No functional change, just refactoring.

src/basic/time-util.c

index 5ffd6bc57497ee624f7853ffda7aae1ff2ea7162..725bfe9fed6f6e0a6dcd8dd79f330ef9f681bbd5 100644 (file)
@@ -634,8 +634,9 @@ static int parse_timestamp_impl(const char *t, usec_t *ret, bool with_tz) {
         const char *k, *utc = NULL, *tzn = NULL;
         struct tm tm, copy;
         time_t x;
-        usec_t usec, x_usec, plus = 0, minus = 0;
+        usec_t usec, plus = 0, minus = 0;
         int r, weekday = -1, dst = -1;
+        unsigned fractional = 0;
 
         /* Allowed syntaxes:
          *
@@ -751,7 +752,6 @@ static int parse_timestamp_impl(const char *t, usec_t *ret, bool with_tz) {
         }
 
         x = (time_t) (usec / USEC_PER_SEC);
-        x_usec = 0;
 
         if (!localtime_or_gmtime_r(&x, &tm, utc))
                 return -EINVAL;
@@ -860,19 +860,12 @@ static int parse_timestamp_impl(const char *t, usec_t *ret, bool with_tz) {
         return -EINVAL;
 
 parse_usec:
-        {
-                unsigned add;
-
-                k++;
-                r = parse_fractional_part_u(&k, 6, &add);
-                if (r < 0)
-                        return -EINVAL;
-
-                if (*k)
-                        return -EINVAL;
-
-                x_usec = add;
-        }
+        k++;
+        r = parse_fractional_part_u(&k, 6, &fractional);
+        if (r < 0)
+                return -EINVAL;
+        if (*k != '\0')
+                return -EINVAL;
 
 from_tm:
         if (weekday >= 0 && tm.tm_wday != weekday)
@@ -882,7 +875,7 @@ from_tm:
         if (x < 0)
                 return -EINVAL;
 
-        usec = usec_add(x * USEC_PER_SEC, x_usec);
+        usec = usec_add(x * USEC_PER_SEC, fractional);
 
 finish:
         usec = usec_add(usec, plus);