#!/usr/bin/env groovy import java.io.BufferedWriter import java.io.FileInputStream import java.io.FileWriter import java.io.OutputStreamWriter import java.util.Calendar import java.util.Date import java.text.SimpleDateFormat //import jdbernard.timestamper.Timeline SimpleDateFormat paramDateFormat = new SimpleDateFormat("yyyy-MM-dd") def cl = new CliBuilder(usage: 'pretty_format.groovy [-i input_file] [-o output_file] [OPTIONS]') cl.h(argName: 'help', 'Show usage information.') cl.i(argName: 'input_file', longOpt: 'input-file', args: 1, 'Input timeline file (if not present, read from STDIN)') cl.o(argName: 'output_file', longOpt: 'output-file', args: 1, 'File to write formatted output to (if not present, output is written to STOUT).') cl.s(argName: 'start_date', longOpt: 'start-date', args: 1, 'Exclude all entries before this date (YYYY-MM-DD).') cl.e(argName: 'end_date', longOpt: 'end-date', args: 1, 'Exclude all entries after this date (YYYY-MM-DD).') cl.p(argName: 'entry_period', longOpt: 'period', args: 1, 'Shorthand notation for the period of entries to include in the output file. The format ' + 'takes the following format: MUN where M = number of units between current time and start time, N = number of units between start time and ' + 'end time, and U = time unit. Valid values for U are: D - day, W - week, M - month, and Y - year. For example: 0W1 means this week ' + 'and the next. 2D5 means a five day period, starting two days from today. -1Y1 means last year, this year, and the next year.') def opt = cl.parse(args) if (!opt) System.exit(1); is = System.in; os = new BufferedWriter(new OutputStreamWriter(System.out)); start = false; end = false; if (opt.i) is = new FileInputStream(opt.i); if (opt.o) os = new BufferedWriter(new FileWriter(opt.o)); if (opt.s) start = paramDateFormat.parse(opt.s); if (opt.e) end = paramDateFormat.parse(opt.e); if (opt.p) { int offset def unit def lower_unit int length (opt.p =~ /([\-n]?)(\d+)([dwmyDWMY])(\d+)/).each { all, nx, x, y, z -> offset = x.toInteger(); unit = y; length = z.toInteger() - 1; if (nx) offset = -offset; } start = Calendar.getInstance() start.set(Calendar.HOUR_OF_DAY, start.getMinimum(Calendar.HOUR_OF_DAY)); start.set(Calendar.MINUTE, start.getMinimum(Calendar.MINUTE)); start.set(Calendar.SECOND, start.getMinimum(Calendar.SECOND)); end = Calendar.getInstance() end.set(Calendar.HOUR_OF_DAY, end.getMaximum(Calendar.HOUR_OF_DAY)); end.set(Calendar.MINUTE, end.getMaximum(Calendar.MINUTE)); end.set(Calendar.SECOND, end.getMaximum(Calendar.SECOND)); switch (unit) { case 'd': case 'D': default: unit = Calendar.DAY_OF_YEAR lower_unit = Calendar.HOUR_OF_DAY break case 'w': case 'W': unit = Calendar.WEEK_OF_YEAR lower_unit = Calendar.DAY_OF_WEEK break case 'm': case 'M': unit = Calendar.MONTH lower_unit = Calendar.DAY_OF_MONTH break case 'y': case 'Y': unit = Calendar.YEAR lower_unit = Calendar.DAY_OF_YEAR break } start.add(unit, offset) start.set(lower_unit, start.getActualMinimum(lower_unit)) end.setTime(start.getTime()) end.add(unit, length) end.set(lower_unit, end.getActualMaximum(lower_unit)) start = start.getTime() end = end.getTime() } //Timeline timeline = Timeline.readFromStream(is); print "Start: ${paramDateFormat.format(start)}\n" print "End: ${paramDateFormat.format(end)}\n"