-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdate-week
More file actions
executable file
·151 lines (142 loc) · 4.68 KB
/
date-week
File metadata and controls
executable file
·151 lines (142 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
about()
{
cat <<'EOF'
---
summary: Wrapper for GNU date which parses ISO Week dates (e.g. `2025W022` from `date-week -d 2025-01-07 '+%YW%V%u'`).
version: 0.0.5
status: unstable
dateCreated: 2025-01-07T21:33:51Z
dateModified: 2025-10-23T13:47:53Z
uuid: 785d13d6-3195-4c4d-a0e9-c4696412ae74
...
EOF
}
DATEFORMAT_WEEKDATE='%YW%V%u';
main()
{
local dateArgs=();
local inputDate='';
local dateFormat=;
while [[ $# -gt 0 ]]; do
case "$1" in
--format)
if [[ -n $dateFormat ]]; then
echo "$dateFormat";
else
echo "$DATEFORMAT_WEEKDATE";
fi
return;;
-d) inputDate="$2"; shift;;
-W|--format=weekdate) dateFormat="$DATEFORMAT_WEEKDATE";;
+*) dateFormat="${1#+}";;
--format=*) dateFormat="${1#--format=}";; # for discoverability beside =weekdate
-v) ;; # ignore, handled outside main
*) dateArgs+=("$1");;
esac
shift;
done
dateArgs+=("+${dateFormat}");
local PAT_ISO_WEEK_NAMED='(?<year>[+-]?[0-9]+)-?W(?<week>[0-5][0-9])-?(?<day>[0-7])?(?<after>$|[^0-9][[:alnum:]:.-]*)';
local PAT_ISO_WEEK_TEST='W[0-5][0-9][0-7]?([^0-9]|$)';
if [[ -n $inputDate ]]; then
if [[ $inputDate =~ $PAT_ISO_WEEK_TEST ]]; then
whisper 2 "[date-week] input matches PAT_ISO_WEEK_TEST /$PAT_ISO_WEEK_TEST/";
if [[ ${inputDate:0:1} == W ]]; then
local thisYear="$(date '+%Y')";
inputDate="${thisYear}${inputDate}";
fi
whisper 1 "[date-week] inputDate(${#inputDate}): ${inputDate}";
local oIFS="$IFS";
# may require perl 5.10+ for '$+{name}' references (relatedLink: https://stackoverflow.com/a/288989)
local substitution='s/'"$PAT_ISO_WEEK_NAMED"'/$+{year}\t$+{week}\t$+{day}\t$+{after}/';
local IFS=$'\t';
local inputDateParts=($(echo -n "$inputDate" | perl -pe "$substitution"));
IFS="$oIFS";
whisper 1 "[date-week] inputDateParts(${#inputDateParts[@]}): ${inputDateParts[*]}";
if [[ "${inputDateParts[0]}" != "$inputDate" ]]; then
local year=${inputDateParts[0]};
local weeks=${inputDateParts[1]#0};
local days=${inputDateParts[2]};
local after=${inputDateParts[3]};
if [[ -z $days ]]; then
days=1; # Starting week with Monday
fi
local firstDayOfYearDate="${year}-01-01";
local firstDayOfYearWeekday="$(date -d "${firstDayOfYearDate}" '+%u')";
let 'days=days + (weeks-1)*7 - firstDayOfYearWeekday';
dateArgs+=(-d "${firstDayOfYearDate}${after} +${days}days");
else
>&2 echo "[date-week] WARNING: no change in input, despite matching week pattern in input.";
fi
else
dateArgs+=(-d "$inputDate");
fi
fi
whisper 1 "[date-week] (${#dateArgs[@]} args) \$ date ${dateArgs[*]}";
date "${dateArgs[@]}";
}
showHelp()
{
echo "USAGE: $(basename "$0") [-d DATE] [+FORMAT] […]";
echo "SUMMARY: Wrapper for GNU date which parses ISO Week dates (e.g. '2025W023').";
echo "OPTIONS:";
echo " -d DATE, +FORMAT, …";
echo " GNU date options are passed through.";
echo;
echo " -W, --format=weekdate";
echo " Use FORMAT '+${DATEFORMAT_WEEKDATE}'.";
echo;
echo " --format";
echo " Show date-week strftime format string and exit.";
echo;
echo " --test";
echo " Run tests and exit.";
echo;
echo " -V, --about";
echo " Show script metadata and exit.";
echo;
echo " -h, --help";
echo " Show this help and exit.";
echo;
echo " -v, --verbose";
echo " Show more info. Can be used multiple times.";
}
runTests()
{
debug()
{
local name="$1";
shift;
local logPrefix="[date-week:test(${name})]";
>&2 echo "${logPrefix} \$ $*";
local result="$("$@")";
local status=$?;
>&2 echo "${logPrefix} status $status, output: ${result}";
>&2 echo;
return $status;
}
local debugOutputFormat='%Y-%m-%dT%H:%M:%S%z';
debug "after" main -d '2025W022T08:00-08' "+${debugOutputFormat}";
local now="$(date "+${debugOutputFormat}")";
local nowWeek="$(date -d "$now" '+%YW%V%uT%H:%M:%S%z')";
whisper 1 "[date-week:test] now: ${now}, inputting '${nowWeek}'";
debug "this week" main -d "$nowWeek" "+${debugOutputFormat}";
debug "week only" main -d 'W05' "+${debugOutputFormat}";
debug "dashes" main -d '2025-W15-3' "+${debugOutputFormat}";
debug "08" main -d '2025W08' "+${debugOutputFormat}";
}
g_verbosity=0;
knock() { [[ $g_verbosity -ge $1 ]]; }
whisper() { knock $1 || return; shift; >&2 echo "$@"; }
# Handle args which switch/affect outside of 'main' function.
for arg in "$@"; do
case "$arg" in
-v|--verbose) let g_verbosity++;;
-V|--about) about; exit;; # "V" for "version"
-h|--help) showHelp; exit;;
--test) runTests; exit;;
esac
done
main "$@";
exit;