t0006: add support for approxidate test date adjustment

t0006 uses a hard-coded test date and provides no convenient way to override it temporarily. Add an optional parameter to check_approxidate to adjust the time as needed, and demonstrate the feature with a new test. Signed-off-by: Tuomas Ahola <taahol@utu.fi> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tuomas Ahola committed May 21, 2026 at 13:54 UTC d5ba64d8b5bb76402a64af2b780780de952de2b9
1 file changed +32 -1
t/t0006-date.sh
+32 -1
@@ -155,12 +155,41 @@ check_parse '2100-00-00 00:00:00 -11' bad
155 check_parse '2100-00-00 00:00:00 +11' bad
156 REQUIRE_64BIT_TIME=
157
158 +add_time_offset() {
159 + case "$3" in
160 + hours)
161 + unit=$(( 60*60 ))
162 + ;;
163 + days)
164 + unit=$(( 24*60*60 ))
165 + ;;
166 + esac
167 + offset=$(( $2 * unit ))
168 + echo $(( $1 + offset ))
169 +}
170 +
171 check_approxidate() {
172 + old_date=$GIT_TEST_DATE_NOW
173 + if test "$3" = "failure"
174 + then
175 + expection="$3"
176 + else
177 + expection=${4:-success}
178 + offset="$3"
179 + fi
180 + if test -n "$offset"
181 + then
182 + GIT_TEST_DATE_NOW=$(add_time_offset $old_date $offset)
183 + caption="$1; offset $offset"
184 + else
185 + caption=$1
186 + fi
187 echo "$1 -> $2 +0000" >expect
160 - test_expect_${3:-success} "parse approxidate ($1)" "
188 + test_expect_$expection "parse approxidate ($caption)" "
189 test-tool date approxidate '$1' >actual &&
190 test_cmp expect actual
191 "
192 + GIT_TEST_DATE_NOW=$old_date
193 }
194
195 check_approxidate now '2009-08-30 19:20:00'
@@ -184,6 +213,8 @@ check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
213 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
214 check_approxidate 'January 5th today pm' '2009-01-30 12:00:00'
215 check_approxidate '10am noon' '2009-08-29 12:00:00'
216 +check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
217 +check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' '+2 days'
218
219 check_approxidate 'last tuesday' '2009-08-25 19:20:00'
220 check_approxidate 'July 5th' '2009-07-05 19:20:00'