darwin-shell 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,214 @@
1
+ #!/usr/bin/env bash
2
+ # darwin-shell core: environment defaults, error contract, tool resolution and
3
+ # dry-run. This module does not load modules (bootstrap does) and does not
4
+ # parse .env, colorize output or do file operations.
5
+ # All user-facing text is an English msgid rendered through i18n.sh.
6
+
7
+ # i18n is the only dependency and sits below this module. Load it before the
8
+ # guard so a failed dependency is retryable on the next source attempt.
9
+ if ! declare -F dsh::i18n::t >/dev/null 2>&1; then
10
+ if declare -F dsh::use >/dev/null 2>&1; then
11
+ dsh::use i18n || return $?
12
+ else
13
+ _dsh_private_core_self="${BASH_SOURCE[0]}"
14
+ source "${_dsh_private_core_self%/*}/i18n.sh" || return $?
15
+ fi
16
+ fi
17
+
18
+ if [[ -n "${_dsh_private_core_sourced:-}" ]]; then
19
+ return 0
20
+ fi
21
+ _dsh_private_core_sourced=1
22
+ readonly _dsh_private_core_sourced
23
+
24
+ # Exit codes shared by every module. Sourced libraries must never exit on
25
+ # behalf of the caller, so callers use these codes in return statements.
26
+ readonly DSH_RC_OK=0
27
+ readonly DSH_RC_FAILURE=1
28
+ readonly DSH_RC_USAGE=2
29
+ readonly DSH_RC_TRANSIENT=3
30
+ readonly DSH_RC_AUTH=4
31
+ readonly DSH_RC_SKIP=5
32
+ readonly DSH_RC_EXISTS=6
33
+ readonly DSH_RC_LOCK_BUSY=7
34
+ readonly DSH_RC_TIMEOUT=124
35
+ readonly DSH_RC_INTERRUPT=130
36
+
37
+ # Environment defaults. Only variables that are unset are initialized.
38
+ : "${DARWIN_SH_DRY_RUN:=0}"
39
+ : "${DARWIN_SH_TOOL_TIMEOUT_SECONDS:=5}"
40
+
41
+ declare -gA _dsh_private_core_warned=()
42
+ declare -gA _dsh_private_core_tools=(
43
+ [jq]='jq'
44
+ [flock]='flock'
45
+ [timeout]='timeout gtimeout'
46
+ [python]='python3 python'
47
+ [date]='date gdate'
48
+ [sha256]='sha256sum shasum'
49
+ )
50
+
51
+ _dsh_private_core_help() {
52
+ case "${1:-}" in
53
+ fail)
54
+ dsh::i18n::t 'Usage: dsh::core::fail <code> <msgid> [args...]'
55
+ printf '\n'
56
+ dsh::i18n::t 'Write a diagnostic to stderr and return <code>. Never exits.'
57
+ ;;
58
+ have_cmd)
59
+ dsh::i18n::t 'Usage: dsh::core::have_cmd <command>'
60
+ printf '\n'
61
+ dsh::i18n::t 'Return 0 when <command> is available, 1 otherwise. No output.'
62
+ ;;
63
+ require_cmd)
64
+ dsh::i18n::t 'Usage: dsh::core::require_cmd <command>...'
65
+ printf '\n'
66
+ dsh::i18n::t 'Return 0 when all commands exist; otherwise fail with code 2.'
67
+ ;;
68
+ tool)
69
+ dsh::i18n::t 'Usage: dsh::core::tool <logical-name>'
70
+ printf '\n'
71
+ dsh::i18n::t 'Print the command implementing <logical-name>. Candidates are tried in order;'
72
+ dsh::i18n::t 'DARWIN_SH_TOOL_<NAME> (uppercase) overrides the chain. Returns 3 when none exist.'
73
+ ;;
74
+ warn_once)
75
+ dsh::i18n::t 'Usage: dsh::core::warn_once <key> <msgid> [args...]'
76
+ printf '\n'
77
+ dsh::i18n::t 'Emit a stderr warning once per key. Used for explicit degradation paths.'
78
+ ;;
79
+ dry_run)
80
+ dsh::i18n::t 'Usage: dsh::core::dry_run'
81
+ printf '\n'
82
+ dsh::i18n::t 'Return 0 when DARWIN_SH_DRY_RUN is truthy, 1 otherwise. No output.'
83
+ ;;
84
+ *)
85
+ dsh::i18n::t 'dsh::core: no help for %1$s' "${1:-<empty>}" >&2
86
+ return 2
87
+ ;;
88
+ esac
89
+ }
90
+
91
+ # Public: report an error without terminating the caller. The message is an
92
+ # English msgid plus positional values; never exits.
93
+ dsh::core::fail() {
94
+ if [[ "${1:-}" == "--help" ]]; then
95
+ _dsh_private_core_help fail
96
+ return $?
97
+ fi
98
+ local code="${1:-$DSH_RC_FAILURE}"
99
+ if [[ ! "$code" =~ ^[0-9]+$ ]]; then
100
+ dsh::i18n::t 'dsh::core::fail: invalid code: %1$s' "$code" >&2
101
+ return "$DSH_RC_USAGE"
102
+ fi
103
+ shift
104
+ local rendered
105
+ rendered="$(dsh::i18n::t -n "$@")"
106
+ dsh::i18n::t 'dsh: error: %1$s' "$rendered" >&2
107
+ return "$code"
108
+ }
109
+
110
+ # Public: silent availability probe (no external process is spawned).
111
+ dsh::core::have_cmd() {
112
+ if [[ "${1:-}" == "--help" ]]; then
113
+ _dsh_private_core_help have_cmd
114
+ return $?
115
+ fi
116
+ local cmd="${1:-}"
117
+ [[ -n "$cmd" ]] || return 1
118
+ command -v -- "$cmd" >/dev/null 2>&1
119
+ }
120
+
121
+ # Public: fail-fast dependency check with an actionable diagnostic.
122
+ dsh::core::require_cmd() {
123
+ if [[ "${1:-}" == "--help" ]]; then
124
+ _dsh_private_core_help require_cmd
125
+ return $?
126
+ fi
127
+ local cmd
128
+ for cmd in "$@"; do
129
+ if ! dsh::core::have_cmd "$cmd"; then
130
+ dsh::core::fail "$DSH_RC_USAGE" 'required command not found: %1$s' "$cmd"
131
+ return $?
132
+ fi
133
+ done
134
+ return 0
135
+ }
136
+
137
+ # Public: resolve a logical tool name to an actual command on PATH.
138
+ dsh::core::tool() {
139
+ if [[ "${1:-}" == "--help" ]]; then
140
+ _dsh_private_core_help tool
141
+ return $?
142
+ fi
143
+ local logical="${1:-}"
144
+ if [[ -z "$logical" ]]; then
145
+ dsh::core::fail "$DSH_RC_USAGE" 'tool: logical name required'
146
+ return $?
147
+ fi
148
+ local chain="${_dsh_private_core_tools[$logical]:-}"
149
+ if [[ -z "$chain" ]]; then
150
+ dsh::core::fail "$DSH_RC_USAGE" 'tool: unknown logical name: %1$s' "$logical"
151
+ return $?
152
+ fi
153
+
154
+ local upper="${logical^^}"
155
+ local override_name="DARWIN_SH_TOOL_${upper}"
156
+ local override="${!override_name:-}"
157
+ if [[ -n "$override" ]]; then
158
+ if dsh::core::have_cmd "$override"; then
159
+ printf '%s\n' "$override"
160
+ return 0
161
+ fi
162
+ dsh::core::warn_once "tool-override-$logical" \
163
+ '%1$s=%2$s is unavailable; falling back to [%3$s]' \
164
+ "$override_name" "$override" "$chain"
165
+ fi
166
+
167
+ # Intentional word splitting: the chain is a fixed, module-owned list of
168
+ # command names, not user input.
169
+ # shellcheck disable=SC2086
170
+ local candidate
171
+ for candidate in $chain; do
172
+ if dsh::core::have_cmd "$candidate"; then
173
+ printf '%s\n' "$candidate"
174
+ return 0
175
+ fi
176
+ done
177
+ dsh::core::fail "$DSH_RC_TRANSIENT" \
178
+ 'tool: no implementation for: %1$s (tried [%2$s])' "$logical" "$chain"
179
+ return $?
180
+ }
181
+
182
+ # Public: emit a warning once per key, so a degraded path is auditable but
183
+ # does not flood stderr in a loop. State is process-local: call this in the
184
+ # current shell, not inside $(...), which would discard the marker.
185
+ dsh::core::warn_once() {
186
+ if [[ "${1:-}" == "--help" ]]; then
187
+ _dsh_private_core_help warn_once
188
+ return $?
189
+ fi
190
+ local key="${1:-}"
191
+ if [[ -z "$key" ]]; then
192
+ return 0
193
+ fi
194
+ shift
195
+ if [[ -n "${_dsh_private_core_warned[$key]:-}" ]]; then
196
+ return 0
197
+ fi
198
+ _dsh_private_core_warned[$key]=1
199
+ local rendered
200
+ rendered="$(dsh::i18n::t -n "$@")"
201
+ dsh::i18n::t 'dsh: warn: %1$s' "$rendered" >&2
202
+ }
203
+
204
+ # Public: dry-run probe. State-changing modules consult this before acting.
205
+ dsh::core::dry_run() {
206
+ if [[ "${1:-}" == "--help" ]]; then
207
+ _dsh_private_core_help dry_run
208
+ return $?
209
+ fi
210
+ case "$DARWIN_SH_DRY_RUN" in
211
+ 1|true|TRUE|yes|YES|on|ON) return 0 ;;
212
+ *) return 1 ;;
213
+ esac
214
+ }
@@ -0,0 +1,367 @@
1
+ #!/usr/bin/env bash
2
+ # darwin-shell datetime: date formatting, calendar validation and arithmetic.
3
+ # Pure-bash work stays in bash; GNU/BSD date differences are isolated in one
4
+ # style probe. TZ is applied per invocation only, never exported globally.
5
+
6
+ # Dependencies sit below this module. Load them before the guard so a failed
7
+ # dependency is retryable on the next source attempt.
8
+ if ! declare -F dsh::i18n::t >/dev/null 2>&1; then
9
+ if declare -F dsh::use >/dev/null 2>&1; then
10
+ dsh::use i18n || return $?
11
+ else
12
+ _dsh_private_datetime_self="${BASH_SOURCE[0]}"
13
+ source "${_dsh_private_datetime_self%/*}/i18n.sh" || return $?
14
+ unset _dsh_private_datetime_self
15
+ fi
16
+ fi
17
+ if ! declare -F dsh::core::fail >/dev/null 2>&1; then
18
+ if declare -F dsh::use >/dev/null 2>&1; then
19
+ dsh::use core || return $?
20
+ else
21
+ _dsh_private_datetime_self="${BASH_SOURCE[0]}"
22
+ source "${_dsh_private_datetime_self%/*}/core.sh" || return $?
23
+ unset _dsh_private_datetime_self
24
+ fi
25
+ fi
26
+
27
+ if [[ -n "${_dsh_private_datetime_sourced:-}" ]]; then
28
+ return 0
29
+ fi
30
+ _dsh_private_datetime_sourced=1
31
+ readonly _dsh_private_datetime_sourced
32
+
33
+ _dsh_private_datetime_help() {
34
+ case "${1:-}" in
35
+ now)
36
+ dsh::i18n::t 'Usage: dsh::datetime::now'
37
+ printf '\n'
38
+ dsh::i18n::t 'Print the current epoch seconds; DARWIN_SH_NOW overrides the clock.'
39
+ ;;
40
+ today)
41
+ dsh::i18n::t 'Usage: dsh::datetime::today [compact|standard]'
42
+ printf '\n'
43
+ dsh::i18n::t 'Print today as YYYY-MM-DD, or compact YYYYMMDD.'
44
+ ;;
45
+ stamp)
46
+ dsh::i18n::t 'Usage: dsh::datetime::stamp'
47
+ printf '\n'
48
+ dsh::i18n::t 'Print the current local time as YYYY-MM-DD HH:MM:SS.'
49
+ ;;
50
+ to_compact|to_standard|normalize)
51
+ dsh::i18n::t 'Usage: dsh::datetime::%1$s <date>' "$1"
52
+ printf '\n'
53
+ case "$1" in
54
+ to_compact) dsh::i18n::t 'Convert YYYY-MM-DD to YYYYMMDD; already-compact input passes through.' ;;
55
+ to_standard) dsh::i18n::t 'Convert YYYYMMDD to YYYY-MM-DD; already-standard input passes through.' ;;
56
+ normalize) dsh::i18n::t 'Normalize a date to YYYY-MM-DD; reject malformed input with code 2.' ;;
57
+ esac
58
+ ;;
59
+ valid)
60
+ dsh::i18n::t 'Usage: dsh::datetime::valid <date>'
61
+ printf '\n'
62
+ dsh::i18n::t 'Return 0 when <date> is a real calendar date, 1 otherwise.'
63
+ ;;
64
+ compare)
65
+ dsh::i18n::t 'Usage: dsh::datetime::compare <a> <b>'
66
+ printf '\n'
67
+ dsh::i18n::t 'Print -1, 0 or 1 after normalizing both dates.'
68
+ ;;
69
+ add)
70
+ dsh::i18n::t 'Usage: dsh::datetime::add <date> <+N unit|-N unit>'
71
+ printf '\n'
72
+ dsh::i18n::t 'Add or subtract days, weeks, months or years; print YYYY-MM-DD.'
73
+ ;;
74
+ month_range)
75
+ dsh::i18n::t 'Usage: dsh::datetime::month_range <YYYY-MM>'
76
+ printf '\n'
77
+ dsh::i18n::t 'Print the first and last day of a month as two YYYY-MM-DD lines.'
78
+ ;;
79
+ *)
80
+ dsh::i18n::t 'dsh::datetime: no help for %1$s' "${1:-<empty>}" >&2
81
+ return "$DSH_RC_USAGE"
82
+ ;;
83
+ esac
84
+ }
85
+
86
+ # Every date invocation goes through here so DARWIN_SH_TZ is scoped to the
87
+ # child process and never changes the caller's environment.
88
+ _dsh_private_datetime_run_date() {
89
+ local date_bin="$1"
90
+ shift
91
+ if [[ -n "${DARWIN_SH_TZ:-}" ]]; then
92
+ TZ="$DARWIN_SH_TZ" "$date_bin" "$@"
93
+ else
94
+ "$date_bin" "$@"
95
+ fi
96
+ }
97
+
98
+ _dsh_private_datetime_is_gnu() {
99
+ local date_bin="$1"
100
+ _dsh_private_datetime_run_date "$date_bin" -d '@0' +%s >/dev/null 2>&1
101
+ }
102
+
103
+ # Public: print epoch seconds, or DARWIN_SH_NOW when it is a valid integer.
104
+ dsh::datetime::now() {
105
+ if [[ "${1:-}" == "--help" ]]; then
106
+ _dsh_private_datetime_help now
107
+ return $?
108
+ fi
109
+ if (( $# != 0 )); then
110
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::%1$s: too many arguments' now
111
+ return $?
112
+ fi
113
+ if [[ -n "${DARWIN_SH_NOW:-}" ]]; then
114
+ if [[ ! "$DARWIN_SH_NOW" =~ ^[0-9]+$ ]]; then
115
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::now: invalid DARWIN_SH_NOW: %1$s' "$DARWIN_SH_NOW"
116
+ return $?
117
+ fi
118
+ printf '%s\n' "$DARWIN_SH_NOW"
119
+ return 0
120
+ fi
121
+ local date_bin
122
+ date_bin="$(dsh::core::tool date)" || return $?
123
+ _dsh_private_datetime_run_date "$date_bin" +%s
124
+ }
125
+
126
+ _dsh_private_datetime_epoch_ymd() {
127
+ local epoch="$1" date_bin
128
+ date_bin="$(dsh::core::tool date)" || return $?
129
+ if _dsh_private_datetime_is_gnu "$date_bin"; then
130
+ _dsh_private_datetime_run_date "$date_bin" -d "@$epoch" +%Y-%m-%d
131
+ else
132
+ _dsh_private_datetime_run_date "$date_bin" -j -f '%s' "$epoch" +%Y-%m-%d
133
+ fi
134
+ }
135
+
136
+ # Public: print today's date in the requested format.
137
+ dsh::datetime::today() {
138
+ if [[ "${1:-}" == "--help" ]]; then
139
+ _dsh_private_datetime_help today
140
+ return $?
141
+ fi
142
+ if (( $# > 1 )); then
143
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::%1$s: too many arguments' today
144
+ return $?
145
+ fi
146
+ local style="${1:-standard}" epoch ymd
147
+ case "$style" in
148
+ standard|compact) ;;
149
+ *)
150
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::today: invalid format: %1$s' "$style"
151
+ return $?
152
+ ;;
153
+ esac
154
+ epoch="$(dsh::datetime::now)" || return $?
155
+ ymd="$(_dsh_private_datetime_epoch_ymd "$epoch")" || return $?
156
+ if [[ "$style" == compact ]]; then
157
+ dsh::datetime::to_compact "$ymd"
158
+ else
159
+ printf '%s\n' "$ymd"
160
+ fi
161
+ }
162
+
163
+ # Public: print a local timestamp with second precision.
164
+ dsh::datetime::stamp() {
165
+ if [[ "${1:-}" == "--help" ]]; then
166
+ _dsh_private_datetime_help stamp
167
+ return $?
168
+ fi
169
+ if (( $# != 0 )); then
170
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::%1$s: too many arguments' stamp
171
+ return $?
172
+ fi
173
+ local epoch date_bin
174
+ epoch="$(dsh::datetime::now)" || return $?
175
+ date_bin="$(dsh::core::tool date)" || return $?
176
+ if _dsh_private_datetime_is_gnu "$date_bin"; then
177
+ _dsh_private_datetime_run_date "$date_bin" -d "@$epoch" '+%Y-%m-%d %H:%M:%S'
178
+ else
179
+ _dsh_private_datetime_run_date "$date_bin" -j -f '%s' "$epoch" '+%Y-%m-%d %H:%M:%S'
180
+ fi
181
+ }
182
+
183
+ # Public: convert a standard date to compact form.
184
+ dsh::datetime::to_compact() {
185
+ if [[ "${1:-}" == "--help" ]]; then
186
+ _dsh_private_datetime_help to_compact
187
+ return $?
188
+ fi
189
+ if (( $# != 1 )); then
190
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::%1$s: missing argument' to_compact
191
+ return $?
192
+ fi
193
+ if [[ "$1" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})$ ]]; then
194
+ printf '%s\n' "${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"
195
+ elif [[ "$1" =~ ^[0-9]{8}$ ]]; then
196
+ printf '%s\n' "$1"
197
+ else
198
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::to_compact: invalid date: %1$s' "$1"
199
+ return $?
200
+ fi
201
+ }
202
+
203
+ # Public: convert a compact date to standard form.
204
+ dsh::datetime::to_standard() {
205
+ if [[ "${1:-}" == "--help" ]]; then
206
+ _dsh_private_datetime_help to_standard
207
+ return $?
208
+ fi
209
+ if (( $# != 1 )); then
210
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::%1$s: missing argument' to_standard
211
+ return $?
212
+ fi
213
+ if [[ "$1" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})$ ]]; then
214
+ printf '%s\n' "$1"
215
+ elif [[ "$1" =~ ^([0-9]{4})([0-9]{2})([0-9]{2})$ ]]; then
216
+ printf '%s-%s-%s\n' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}"
217
+ else
218
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::to_standard: invalid date: %1$s' "$1"
219
+ return $?
220
+ fi
221
+ }
222
+
223
+ # Public: normalize either accepted form to YYYY-MM-DD.
224
+ dsh::datetime::normalize() {
225
+ if [[ "${1:-}" == "--help" ]]; then
226
+ _dsh_private_datetime_help normalize
227
+ return $?
228
+ fi
229
+ if (( $# != 1 )); then
230
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::%1$s: missing argument' normalize
231
+ return $?
232
+ fi
233
+ local out
234
+ out="$(dsh::datetime::to_standard "$1")" || {
235
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::normalize: invalid date: %1$s' "$1"
236
+ return $?
237
+ }
238
+ printf '%s\n' "$out"
239
+ }
240
+
241
+ # Public: validate a real calendar date, independent of locale and date flags.
242
+ dsh::datetime::valid() {
243
+ if [[ "${1:-}" == "--help" ]]; then
244
+ _dsh_private_datetime_help valid
245
+ return $?
246
+ fi
247
+ if (( $# != 1 )); then
248
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::%1$s: missing argument' valid
249
+ return $?
250
+ fi
251
+ local value="$1" y m d max
252
+ if [[ "$value" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})$ ]]; then
253
+ y="${BASH_REMATCH[1]}"
254
+ m="${BASH_REMATCH[2]}"
255
+ d="${BASH_REMATCH[3]}"
256
+ elif [[ "$value" =~ ^[0-9]{8}$ ]]; then
257
+ y="${value:0:4}"
258
+ m="${value:4:2}"
259
+ d="${value:6:2}"
260
+ else
261
+ return 1
262
+ fi
263
+ y=$((10#$y))
264
+ m=$((10#$m))
265
+ d=$((10#$d))
266
+ (( y >= 1 && m >= 1 && m <= 12 )) || return 1
267
+ case "$m" in
268
+ 1|3|5|7|8|10|12) max=31 ;;
269
+ 4|6|9|11) max=30 ;;
270
+ 2)
271
+ if (( (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 )); then
272
+ max=29
273
+ else
274
+ max=28
275
+ fi
276
+ ;;
277
+ esac
278
+ (( d >= 1 && d <= max ))
279
+ }
280
+
281
+ # Public: compare two dates after normalization. Output -1, 0 or 1.
282
+ dsh::datetime::compare() {
283
+ if [[ "${1:-}" == "--help" ]]; then
284
+ _dsh_private_datetime_help compare
285
+ return $?
286
+ fi
287
+ if (( $# != 2 )); then
288
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::%1$s: expected <a> <b>' compare
289
+ return $?
290
+ fi
291
+ local a b
292
+ a="$(dsh::datetime::normalize "$1")" || return $?
293
+ b="$(dsh::datetime::normalize "$2")" || return $?
294
+ local LC_ALL=C
295
+ if [[ "$a" < "$b" ]]; then
296
+ printf -- '-1\n'
297
+ elif [[ "$a" > "$b" ]]; then
298
+ printf '1\n'
299
+ else
300
+ printf '0\n'
301
+ fi
302
+ }
303
+
304
+ # Public: add a signed amount of days/weeks/months/years to a date.
305
+ dsh::datetime::add() {
306
+ if [[ "${1:-}" == "--help" ]]; then
307
+ _dsh_private_datetime_help add
308
+ return $?
309
+ fi
310
+ if (( $# != 2 )); then
311
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::%1$s: expected <date> <expr>' add
312
+ return $?
313
+ fi
314
+ local date="$1" expr="$2" ymd sign amount unit letter date_bin
315
+ ymd="$(dsh::datetime::normalize "$date")" || return $?
316
+ if [[ ! "$expr" =~ ^([+-])([0-9]+)[[:space:]]+(day|days|week|weeks|month|months|year|years)$ ]]; then
317
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::add: invalid expression: %1$s' "$expr"
318
+ return $?
319
+ fi
320
+ sign="${BASH_REMATCH[1]}"
321
+ amount="${BASH_REMATCH[2]}"
322
+ unit="${BASH_REMATCH[3]}"
323
+ case "$unit" in
324
+ day|days) letter=d ;;
325
+ week|weeks) letter=w ;;
326
+ month|months) letter=m ;;
327
+ year|years) letter=y ;;
328
+ esac
329
+ date_bin="$(dsh::core::tool date)" || return $?
330
+ if _dsh_private_datetime_is_gnu "$date_bin"; then
331
+ _dsh_private_datetime_run_date "$date_bin" -d "$ymd $sign$amount $unit" +%Y-%m-%d
332
+ else
333
+ _dsh_private_datetime_run_date "$date_bin" -j -v"${sign}${amount}${letter}" -f '%Y-%m-%d' "$ymd" +%Y-%m-%d
334
+ fi
335
+ }
336
+
337
+ # Public: print the first and last day of a month as two YYYY-MM-DD lines.
338
+ dsh::datetime::month_range() {
339
+ if [[ "${1:-}" == "--help" ]]; then
340
+ _dsh_private_datetime_help month_range
341
+ return $?
342
+ fi
343
+ if (( $# != 1 )); then
344
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::%1$s: missing argument' month_range
345
+ return $?
346
+ fi
347
+ local month="$1" year mon start end date_bin
348
+ if [[ ! "$month" =~ ^([0-9]{4})-([0-9]{2})$ ]]; then
349
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::month_range: invalid month: %1$s' "$month"
350
+ return $?
351
+ fi
352
+ year="${BASH_REMATCH[1]}"
353
+ mon="${BASH_REMATCH[2]}"
354
+ mon=$((10#$mon))
355
+ if (( mon < 1 || mon > 12 )); then
356
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::datetime::month_range: invalid month: %1$s' "$month"
357
+ return $?
358
+ fi
359
+ start="${year}-$(printf '%02d' "$mon")-01"
360
+ date_bin="$(dsh::core::tool date)" || return $?
361
+ if _dsh_private_datetime_is_gnu "$date_bin"; then
362
+ end="$(_dsh_private_datetime_run_date "$date_bin" -d "$start +1 month -1 day" +%Y-%m-%d)" || return $?
363
+ else
364
+ end="$(_dsh_private_datetime_run_date "$date_bin" -j -v+1m -v-1d -f '%Y-%m-%d' "$start" +%Y-%m-%d)" || return $?
365
+ fi
366
+ printf '%s\n%s\n' "$start" "$end"
367
+ }