|
| 1 | +import std/[ times, strutils] |
| 2 | +import docopt |
| 3 | + |
| 4 | +proc perr(s: string, i: int) = |
| 5 | + writeLine(stderr, "ERROR: " & s) |
| 6 | + flushFile(stderr) |
| 7 | + quit i |
| 8 | + |
| 9 | +proc pinf(s: string, verbose: bool) = |
| 10 | + if verbose: |
| 11 | + writeLine(stderr, "INFO: " & s) |
| 12 | + flushFile(stderr) |
| 13 | + |
| 14 | +let doc = """ |
| 15 | + datediff - Show dates difference in specific format |
| 16 | +
|
| 17 | + + optionaly select a timezone with TZ environment variable |
| 18 | + Use tzselect(8) to find your TZ zone |
| 19 | +
|
| 20 | + USAGE: |
| 21 | + datediff [options] [--] <DATE1> <DATE2> |
| 22 | + datediff --help |
| 23 | +
|
| 24 | + OPTIONS: |
| 25 | + -f --format=FORMAT Format for date [default: yyyy-MM-dd] |
| 26 | + -v --verbose Show what is going on |
| 27 | + --help Show this help |
| 28 | + -s --seconds Output in: Seconds |
| 29 | + -m --minutes Output in: Minutes |
| 30 | + -h --hours Output in: Hours |
| 31 | + -d --days Output in: Days (This is default) |
| 32 | + -w --weeks Output in: Weeks |
| 33 | +
|
| 34 | + FORMATTING: |
| 35 | + FORMAT is parsing DATE inputs as follows: |
| 36 | +
|
| 37 | + d Numeric value representing the day of the month, it will be either one or two digits long. |
| 38 | + 1/04/2012 -> 1 |
| 39 | + 21/04/2012 -> 21 |
| 40 | + dd Same as above, but is always two digits. |
| 41 | + 1/04/2012 -> 01 |
| 42 | + 21/04/2012 -> 21 |
| 43 | + h The hours in one digit if possible. Ranging from 1-12. |
| 44 | + 5pm -> 5 |
| 45 | + 2am -> 2 |
| 46 | + hh The hours in two digits always. If the hour is one digit, 0 is prepended. |
| 47 | + 5pm -> 05 |
| 48 | + 11am -> 11 |
| 49 | + H The hours in one digit if possible, ranging from 0-23. |
| 50 | + 5pm -> 17 |
| 51 | + 2am -> 2 |
| 52 | + HH The hours in two digits always. 0 is prepended if the hour is one digit. |
| 53 | + 5pm -> 17 |
| 54 | + 2am -> 02 |
| 55 | + m The minutes in one digit if possible. |
| 56 | + 5:30 -> 30 |
| 57 | + 2:01 -> 1 |
| 58 | + mm Same as above but always two digits, 0 is prepended if the minute is one digit. |
| 59 | + 5:30 -> 30 |
| 60 | + 2:01 -> 01 |
| 61 | + M The month in one digit if possible. |
| 62 | + September -> 9 |
| 63 | + December -> 12 |
| 64 | + MM The month in two digits always. 0 is prepended if the month value is one digit. |
| 65 | + September -> 09 |
| 66 | + December -> 12 |
| 67 | + s Seconds as one digit if possible. |
| 68 | + 00:00:06 -> 6 |
| 69 | + ss Same as above but always two digits. 0 is prepended if the second is one digit. |
| 70 | + 00:00:06 -> 06 |
| 71 | + yy The last two digits of the year. When parsing, the current century is assumed. |
| 72 | + 2012 AD -> 12 |
| 73 | + yyyy The year, padded to at least four digits. Is always positive, even when the year is BC. When the year is more than four digits, '+' is prepended. |
| 74 | + 2012 AD -> 2012 |
| 75 | + 24 AD -> 0024 |
| 76 | + 24 BC -> 00024 |
| 77 | + 12345 AD -> +12345 |
| 78 | + YYYY The year without any padding. Is always positive, even when the year is BC. |
| 79 | + 2012 AD -> 2012 |
| 80 | + 24 AD -> 24 |
| 81 | + 24 BC -> 24 |
| 82 | + 12345 AD -> 12345 |
| 83 | +
|
| 84 | + More patterns can be found here: |
| 85 | + https://nim-lang.org/docs/times.html#parsing-and-formatting-dates |
| 86 | +
|
| 87 | + EXAMPLES: |
| 88 | + datediff 2000-01-01 2001-01-01 |
| 89 | + # 366 |
| 90 | + datediff -s -f "yyyy" 2022 2025 |
| 91 | + # 94694400 |
| 92 | + TZ='America/Anchorage' datediff -h -f "yyyy-MM-dd-HH" 2022-03-13-01 2022-03-13-04 |
| 93 | + # 2 # Because of DST change |
| 94 | + """.dedent() |
| 95 | + |
| 96 | +let args = docopt(doc, version = "0.1.0", optionsFirst = true) |
| 97 | + |
| 98 | +var |
| 99 | + verboseBool = false |
| 100 | + timeFormat : TimeFormat |
| 101 | + dateTime1 : DateTime |
| 102 | + dateTime2 : DateTime |
| 103 | + |
| 104 | +if not args["<DATE1>"]: |
| 105 | + perr("Missing input <DATE1>", 1) |
| 106 | +if not args["<DATE2>"]: |
| 107 | + perr("Missing input <DATE2>", 1) |
| 108 | + |
| 109 | +if args["--verbose"]: |
| 110 | + verboseBool = true |
| 111 | + pinf("Verbose output", verboseBool) |
| 112 | + |
| 113 | +if not args["--format"]: |
| 114 | + perr("Did not found FORMAT. See --help", 2) |
| 115 | +else: |
| 116 | + pinf("Specified format is: " & $args["--format"], verboseBool) |
| 117 | + try: |
| 118 | + timeFormat = initTimeFormat($args["--format"]) |
| 119 | + except: |
| 120 | + perr(getCurrentExceptionMsg(), 3) |
| 121 | + |
| 122 | +pinf("Parsing DATE1: " & $args["<DATE1>"], verboseBool) |
| 123 | +try: |
| 124 | + dateTime1 = parse($args["<DATE1>"], timeFormat) |
| 125 | +except: |
| 126 | + perr(getCurrentExceptionMsg(), 3) |
| 127 | + |
| 128 | +pinf("Parsing DATE2: " & $args["<DATE2>"], verboseBool) |
| 129 | +try: |
| 130 | + dateTime2 = parse($args["<DATE2>"], timeFormat) |
| 131 | +except: |
| 132 | + perr(getCurrentExceptionMsg(), 3) |
| 133 | + |
| 134 | +pinf("Duration...", verboseBool) |
| 135 | +let dur = abs(dateTime1 - dateTime2) |
| 136 | + |
| 137 | +# Duration can be: inWeeks inDays inHours inMinutes inSeconds inMilliseconds inMicroseconds inNanoseconds |
| 138 | + |
| 139 | +if args["--seconds"]: |
| 140 | + echo dur.inSeconds |
| 141 | +elif args["--minutes"]: |
| 142 | + echo dur.inMinutes |
| 143 | +elif args["--hours"]: |
| 144 | + echo dur.inHours |
| 145 | +elif args["--weeks"]: |
| 146 | + echo dur.inWeeks |
| 147 | +else: |
| 148 | + echo dur.inDays |
0 commit comments