Showing 1 changed files with 29 additions and 4 deletions
+29 -4
analyse-votes-AN
... ...
@@ -603,10 +603,35 @@ function check_groupes () {
603 603
 }
604 604
 
605 605
 if test "$periode" = $true_flag; then
606
-    first=$(sqlite_request "select num from scrutins where date like '% du ${periode_value%:*}' order by num asc" | head -1)
607
-    last=$(sqlite_request "select num from scrutins where date like '% du ${periode_value#*:}' order by num asc" | tail -1)
608
-    test -z "$first" && echo "date de début inconnue: ${periode_value#*:}" >&2 && rm -f $in_ram_database && exit 1
609
-    test -z "$last" && echo "date de fin inconnue: ${periode_value%:*}" >&2 && rm -f $in_ram_database && exit 1
606
+    function get_date () {
607
+        sqlite_request "select distinct(date) from scrutins order by num asc" | awk -v d="$1" -v comp=$2 '
608
+            function norm_date (date) {
609
+                split(date, a, "/")
610
+                return sprintf("%s%s%s",
611
+                    length(a[3]) == 4 ? a[3] : length(a[3]) == 2 ? "20" a[3] : strftime("%Y", systime()),
612
+                    length(a[2]) == 2 ? a[2] : "0" a[2],
613
+                    length(a[1]) == 2 ? a[1] : "0" a[1])
614
+            }
615
+            function output (date) {
616
+                print date
617
+                found = 1
618
+                exit
619
+            }
620
+            BEGIN { d = norm_date(d) }
621
+            {
622
+                s = norm_date($1)
623
+                if (NR == 1 && s > d && comp == "first") output($1)
624
+                if (s >= d && comp == "first") output($1)
625
+                if (s == d && comp == "last")  output($1)
626
+                if (s >  d && comp == "last")  output(previous)
627
+                previous = $1
628
+            }
629
+            END {
630
+                if (!found) print previous
631
+            }'
632
+    }
633
+    first=$(sqlite_request "select min(num) from scrutins where date = '$(get_date ${periode_value%:*} first)'")
634
+    last=$(sqlite_request "select max(num) from scrutins where date = '$(get_date ${periode_value#*:} last)'")
610 635
 elif test "$dossier" != $true_flag; then
611 636
     test -z "$last" && last=$(dernier_scrutin_public)
612 637
     test -z "$first" && first=1