diff options
| author | Jo-Philipp Wich | 2022-05-30 21:44:34 +0000 |
|---|---|---|
| committer | Jo-Philipp Wich | 2022-05-30 21:44:34 +0000 |
| commit | 30a7d47039cf2cee419a48c0dfa02303f94c30c4 (patch) | |
| tree | e3fec324bc039395c927b09a8497de10b2c4357a | |
| parent | fb9a6b2ba85bb434e6634808fd4530ac2fb2c2c0 (diff) | |
| download | firewall4-30a7d47039cf2cee419a48c0dfa02303f94c30c4.tar.gz | |
fw4: fix datetime parsing
- Rework `parse_date()` to mirror the fw3 parsing logic which allows
omitting each part of the timestamp except the year
- Introduce `fw4.datestamp()` helper which formats the given timestamp
either as datetime or date, depending on whether time information is
present
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
| -rw-r--r-- | root/usr/share/ucode/fw4.uc | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/root/usr/share/ucode/fw4.uc b/root/usr/share/ucode/fw4.uc index be347d7..a06d097 100644 --- a/root/usr/share/ucode/fw4.uc +++ b/root/usr/share/ucode/fw4.uc @@ -1238,26 +1238,21 @@ return { }, parse_date: function(val) { - let m = match(val, /^([0-9-]+)T([0-9:]+)$/); - let d = m ? match(m[1], /^([0-9]{1,4})(-([0-9]{1,2})(-([0-9]{1,2}))?)?$/) : null; - let t = this.parse_time(m[2]); + let d = match(val, /^([0-9]{4})(-([0-9]{1,2})(-([0-9]{1,2})(T([0-9:]+))?)?)?$/); - d[3] ||= 1; - d[5] ||= 1; - - if (d == null || d[1] < 1970 || d[1] > 2038 || d[3] < 1 || d[3] > 12 || d[5] < 1 || d[5] > 31) + if (d == null || d[1] < 1970 || d[1] > 2038 || d[3] > 12 || d[5] > 31) return null; - if (m[2] && !t) + let t = this.parse_time(d[7] ?? "0"); + + if (t == null) return null; return { year: +d[1], - month: +d[3], - day: +d[5], - hour: t ? +t[1] : 0, - min: t ? +t[3] : 0, - sec: t ? +t[5] : 0 + month: +d[3] || 1, + day: +d[5] || 1, + ...t }; }, @@ -1643,6 +1638,10 @@ return { return sprintf('"%04d-%02d-%02d"', stamp.year, stamp.month, stamp.day); }, + datestamp: function(stamp) { + return exists(stamp, 'hour') ? this.datetime(stamp) : this.date(stamp); + }, + time: function(stamp) { return sprintf('"%02d:%02d:%02d"', stamp.hour, stamp.min, stamp.sec); }, |