time-util: extract parse_calendar_date() from sysupdate

Move the YYYY-MM-DD date parsing and validation logic from
sysupdate-resource.c into a shared parse_calendar_date() function
in time-util, so it can be reused by other subsystems.
This commit is contained in:
Dylan M. Taylor
2026-03-18 09:39:06 -04:00
parent fb2cf9f557
commit 770958e24a
4 changed files with 59 additions and 17 deletions
+29
View File
@@ -1892,3 +1892,32 @@ TimestampStyle timestamp_style_from_string(const char *s) {
return TIMESTAMP_US_UTC;
return t;
}
int parse_calendar_date(const char *s, usec_t *ret) {
struct tm parsed_tm = {}, copy_tm;
usec_t usec;
const char *k;
int r;
assert(s);
k = strptime(s, "%Y-%m-%d", &parsed_tm);
if (!k || *k)
return -EINVAL;
copy_tm = parsed_tm;
r = mktime_or_timegm_usec(&copy_tm, /* utc= */ true, &usec);
if (r < 0)
return r;
/* Refuse non-normalized dates, e.g. Feb 30 */
if (copy_tm.tm_mday != parsed_tm.tm_mday ||
copy_tm.tm_mon != parsed_tm.tm_mon ||
copy_tm.tm_year != parsed_tm.tm_year)
return -EINVAL;
if (ret)
*ret = usec;
return 0;
}
+1
View File
@@ -180,6 +180,7 @@ const char* etc_localtime(void);
int mktime_or_timegm_usec(struct tm *tm, bool utc, usec_t *ret);
int localtime_or_gmtime_usec(usec_t t, bool utc, struct tm *ret);
int parse_calendar_date(const char *s, usec_t *ret);
uint32_t usec_to_jiffies(usec_t usec);
usec_t jiffies_to_usec(uint32_t jiffies);