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:
@@ -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(©_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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user