Groovy web console

subscribe to the feed Subscribe
to this
site

coverage gaps

Published 7 days ago by Khanh
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
//println normalizeGaps('01-Jan-1978--31-Dec-1980; 01-Jan-1984--31-Dec-1984')
println(calculateRanges('1976-04-01', '01-Jan-1978--31-Dec-1980; 01-Jan-1984--31-Dec-1984', 'Current', ''))

List calculateRanges(start, gaps, end, embargo) {
    List result = []
    return result
}

List normalizeGaps(gaps) {
    String[] gapArr = gaps.split(';')
      //deduplicate
    List uniqGaps = []
    for (gap in gapArr) {
          gap = gap.trim()
          if (!uniqGaps.contains(gap)) {
            uniqGaps.add(gap)
          }  
    }

    //process uniq gaps
    List processedGaps = []
    for (gap in uniqGaps) {
        String[] arr = gap.split('--')
        if (arr.length >= 2) {
            String begin = toString(addDays(toDate(arr[0]), -1))
            String end = toString(addDays(toDate(arr[1]), 1))
            if (begin && end) {
                processedGaps.add([begin, end])
            }
        }
    }
    return processedGaps
}

Date toDate(String input) {
    String pattern = 'dd-MMM-yyyy'
    try {
        Date dateObj = new java.text.SimpleDateFormat(pattern).parse(input)  
        return dateObj
    } catch (java.text.ParseException e) {
        return null   
    }
}

String toString(date) {
    if (date) {
        def pattern = 'yyyy-MM-dd'
        def str = new java.text.SimpleDateFormat(pattern).format(date)
        return str
    } else {
        return null
    }
}

Date addDays(Date date, int amount) {
    if (date) {
        Calendar c = Calendar.getInstance()
        c.setTime(date)
        c.add(Calendar.DATE, amount)
        return c.getTime()
    }
    return null
}