#! /bin/bash
# Last edited on 2017-05-12 22:41:59 by stolfilocal

# Usage: "$0 < {datafile} "

# Prints to standard output a line "{inidate} {initime} {findate} {fintime}"
# where {inidate} {initime} is the earliest date+time in the series file {datafile}
# with a defined price, and {findate} {fintime} is the latest.  
# 
# Assumes that each line of {datafile} begins with a date and time
# separated by spaces.

datafile="$1"; shift # Name of base file.

tmp="/tmp/$$"

cat ${datafile} \
  | egrep -e '^20[0-9][0-9]-[-0-9]+[ ]' \
  | egrep -v -e ' [.] *$' \
  | gawk -v FS='|' \
      ' //{ 
          dt=$1; 
          gsub(/^[ ]+/, "", dt);
          gsub(/[ ]+$/, "", dt);
          gsub(/[ ][ ]+/, " ", dt);
          print dt;
        } 
      ' \
  | sort \
  | gawk \
      ' BEGIN{ 
          idate = "????-??-??"; itime = "??:??:??";
          fdate = "????-??-??"; ftime = "??:??:??";
        }
        /^20/ { 
          if (NF != 2) { bug("bad NF"); }
          da = $1; ti = $2;
          if (idate == "????-??-??") { idate = da; itime = ti; }
          fdate = da; ftime = ti;
          next;
        }
        // { bug("bad format"); }
        END { printf "%s %s %s %s\n", idate, itime, fdate, ftime; }
        function bug(msg) { printf "**BUG %n: %s\n", FNR, msg > "/dev/stderr"; exit(1); }
      '
