From 4b44a5203379d91c169b87c43a52fec94f709e78 Mon Sep 17 00:00:00 2001 From: Jephte CLAIN Date: Fri, 2 Jan 2015 11:19:15 +0400 Subject: [PATCH] =?UTF-8?q?ajout=20des=20fonctions=20de=20gestion=20des=20?= =?UTF-8?q?dates=20=C3=A0=20awk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ulib/.ulibver | 2 +- lib/ulib/base | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/ulib/.ulibver b/lib/ulib/.ulibver index 8b763f8..84c3b5a 100644 --- a/lib/ulib/.ulibver +++ b/lib/ulib/.ulibver @@ -1 +1 @@ -007015000 +007016000 diff --git a/lib/ulib/base b/lib/ulib/base index 890f5e6..a112644 100644 --- a/lib/ulib/base +++ b/lib/ulib/base @@ -1800,6 +1800,10 @@ function ensure_hostname() { } __AWKDEF_FUNCTIONS=' +function num(s) { + if (s ~ /^[0-9]+$/) return int(s) + else return s +} function quote_html(s) { gsub(/&/, "\\&", s) gsub(/"/, "\\"", s) @@ -1850,6 +1854,52 @@ function unquote_mysqlcsv(s) { gsub(/\\\\/, "\\", s) return s } + +function __parse_date_fr(date, parts, y, m, d) { + if (match(date, /([0-9][0-9]?)\/([0-9][0-9]?)\/([0-9][0-9][0-9][0-9])/, parts)) { + y = int(parts[3]) + m = int(parts[2]) + d = int(parts[1]) + return mktime(sprintf("%04i %02i %02i 00 00 00 +0400", y, m, d)) + } + return -1 +} +function __parse_date_mysql(date, parts, y, m, d) { + if (match(date, /([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])/, parts)) { + y = int(parts[1]) + m = int(parts[2]) + d = int(parts[3]) + return mktime(sprintf("%04i %02i %02i 00 00 00 +0400", y, m, d)) + } + return -1 +} +function __parse_date_any(date, serial) { + serial = __parse_date_fr(date) + if (serial == -1) serial = __parse_date_mysql(date) + return serial +} +function date_serial(date) { + return __parse_date_any(date) +} +function date_parse(date, serial) { + serial = __parse_date_any(date) + if (serial == -1) return date + return strftime("%d/%m/%Y", serial) +} +function date_monday(date, serial, dow) { + serial = __parse_date_any(date) + if (serial == -1) return date + dow = strftime("%u", serial) + serial -= (dow - 1) * 86400 + return strftime("%d/%m/%Y", serial) +} +function date_add(date, nbdays, serial) { + serial = __parse_date_any(date) + if (serial == -1) return date + serial += nbdays * 86400 + return strftime("%d/%m/%Y", serial) +} + function array_new(dest) { dest[0] = 0 # forcer awk à considérer dest comme un tableau delete dest