fix(user/zeropad.sh):Only zero-pad files with leading digits
[BK-2020-03.git] / unitproc / bkt-list_days_of_year.sh
1 #!/usr/bin/env bash
2
3 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
4 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
5 try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
6 list_days_of_year() {
7 # Desc: Lists days of year in ISO-8601 YYYY-mm-dd format
8 # Usage: script.sh [str year]
9 # Input: arg1: year (4 digit gregorian calendar year)
10 # Output: stdout: iso-8601 dates (newline delimited)
11 # Depends: bash (5.1.16), GNU Coreutils (8.32; date, printf)
12 # Version: 0.0.1
13 # Ref/Attrib: [1] Linux Bash: How to print leading zeroes on a variable https://bytefreaks.net/gnulinux/bash/linux-bash-how-to-print-leading-zeroes-on-a-variable
14
15 # Check arg
16 ## Validate argument count
17 if [[ $# -ne 1 ]]; then die "FATAL:Invalid number of arguments:$#"; fi;
18 year="$1";
19
20 ## Validate arg is a 4-digit year
21 pattern='^[0-9]{4}$';
22 if [[ ! $year =~ $pattern ]]; then die "FATAL:Not a 4-digit year."; fi;
23 if [[ ! $year -gt 1582 ]]; then die "FATAL:Not a gregorian calendar year."; fi;
24
25 yyyy=$year;
26 for (( month = 1; month <= 12; month++ )); do
27 mm="$(printf %02d "$month")"; # two-digit month; See [1]
28 #yell "DEBUG:mm:$mm";
29 for (( day = 1; day <= 31; day++ )); do
30 dd="$(printf %02d "$day")"; # two-digit day; See [1]
31 #yell "DEBUG:dd:$dd";
32 date_iso8601="$yyyy-$mm-$dd";
33 if date --date="$date_iso8601" 1>/dev/random 2>&1; then
34 printf "%s\n" "$date_iso8601";
35 fi;
36 done;
37 done;
38 }; # iso-8601 dates (yyyy-mm-dd) to stdout
39
40 # Test
41 wc -l < <(list_days_of_year 1583); # should report 365 lines
42 wc -l < <(list_days_of_year 2023); # should report 365 lines
43 wc -l < <(list_days_of_year 2024); # should report 366 lines (leap year)
44 wc -l < <(list_days_of_year 2025); # should report 365 lines
45 wc -l < <(list_days_of_year 9999); # should report 365 lines
46
47 # Author: Steven Baltakatei Sandoval
48 # License: GPLv3+
49
50 # Ref/Attrib
51 # # date (GNU coreutils) 8.32
52 # # Copyright (C) 2020 Free Software Foundation, Inc.
53 # # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
54 # # This is free software: you are free to change and redistribute it.
55 # # There is NO WARRANTY, to the extent permitted by law.
56
57 # # Written by David MacKenzie.
58
59
60 # # printf (GNU coreutils) 8.32
61 # # Copyright (C) 2020 Free Software Foundation, Inc.
62 # # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
63 # # This is free software: you are free to change and redistribute it.
64 # # There is NO WARRANTY, to the extent permitted by law.
65
66 # # Written by David MacKenzie.
67
68
69 # # GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
70 # # Copyright (C) 2020 Free Software Foundation, Inc.
71 # # License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
72
73 # # This is free software; you are free to change and redistribute it.
74 # # There is NO WARRANTY, to the extent permitted by law.