From 517c8c6150ed6e327eca72ef6fd98c43ccfa60b1 Mon Sep 17 00:00:00 2001 From: Kareem Khazem Date: Fri, 11 Apr 2025 14:19:26 +0100 Subject: [PATCH] Add TODOTXT_DATE_FORMAT environment variable TODOTXT_DATE_FORMAT sets the format of creation and completion dates. Prior to this commit, the format was hard-coded to %Y-%m-%d. This commit fixes #456. --- tests/t1011-add-format-date.sh | 51 ++++++++++++++++++++++++++++++++++ todo.sh | 10 +++++-- 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100755 tests/t1011-add-format-date.sh diff --git a/tests/t1011-add-format-date.sh b/tests/t1011-add-format-date.sh new file mode 100755 index 00000000..b0d7443f --- /dev/null +++ b/tests/t1011-add-format-date.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +test_description='test TODOTXT_DATE_FORMAT environment variable + +Ensure that the date format of creation and completion dates is correct +' +. ./test-lib.sh + + +unset TODOTXT_DATE_FORMAT +test_todo_session 'default date format with env var unset' <>> todo.sh -t add Solve the halting problem +1 2009-02-13 Solve the halting problem +TODO: 1 added. + +>>> todo.sh list +1 2009-02-13 Solve the halting problem +-- +TODO: 1 of 1 tasks shown +EOF + +export TODOTXT_DATE_FORMAT="%Y-%m-%dT%H:%M:%S%z" +test_todo_session 'ISO-8601 with seconds and timezone' <>> todo.sh -t add Solve the halting problem +2 2009-02-13T04:40:00+0000 Solve the halting problem +TODO: 2 added. + +>>> todo.sh list +1 2009-02-13 Solve the halting problem +2 2009-02-13T04:40:00+0000 Solve the halting problem +-- +TODO: 2 of 2 tasks shown +EOF + +test_todo_session 'ISO-8601 completion date' <>> todo.sh -t done 2 +2 x 2009-02-13T04:40:00+0000 2009-02-13T04:40:00+0000 Solve the halting problem +TODO: 2 marked as done. +x 2009-02-13T04:40:00+0000 2009-02-13T04:40:00+0000 Solve the halting problem +TODO: $HOME/todo.txt archived. + +>>> todo.sh listall +1 2009-02-13 Solve the halting problem +0 x 2009-02-13T04:40:00+0000 2009-02-13T04:40:00+0000 Solve the halting problem +-- +TODO: 1 of 1 tasks shown +DONE: 1 of 1 tasks shown +total 2 of 2 tasks shown +EOF + +test_done diff --git a/todo.sh b/todo.sh index 0fe19756..16a5e361 100755 --- a/todo.sh +++ b/todo.sh @@ -151,6 +151,7 @@ $indentedJoinedConfigFileLocations TODOTXT_DEFAULT_ACTION="" run this when called with no arguments TODOTXT_SORT_COMMAND="sort ..." customize list output TODOTXT_FINAL_FILTER="sed ..." customize list after color, P@+ hiding + TODOTXT_DATE_FORMAT="%Y-%m-%d" customize creation/completion date format TODOTXT_SOURCEVAR=\$DONE_FILE use another source for listcon, listproj TODOTXT_SIGIL_BEFORE_PATTERN="" optionally allow chars preceding +p / @c TODOTXT_SIGIL_VALID_PATTERN=.* tweak the allowed chars for +p and @c @@ -524,6 +525,7 @@ OVR_TODOTXT_AUTO_ARCHIVE="$TODOTXT_AUTO_ARCHIVE" OVR_TODOTXT_FORCE="$TODOTXT_FORCE" OVR_TODOTXT_PRESERVE_LINE_NUMBERS="$TODOTXT_PRESERVE_LINE_NUMBERS" OVR_TODOTXT_PLAIN="$TODOTXT_PLAIN" +OVR_TODOTXT_DATE_FORMAT="$TODOTXT_DATE_FORMAT" OVR_TODOTXT_DATE_ON_ADD="$TODOTXT_DATE_ON_ADD" OVR_TODOTXT_PRIORITY_ON_ADD="$TODOTXT_PRIORITY_ON_ADD" OVR_TODOTXT_DISABLE_FILTER="$TODOTXT_DISABLE_FILTER" @@ -642,6 +644,7 @@ TODOTXT_PLAIN=${TODOTXT_PLAIN:-0} TODOTXT_FORCE=${TODOTXT_FORCE:-0} TODOTXT_PRESERVE_LINE_NUMBERS=${TODOTXT_PRESERVE_LINE_NUMBERS:-1} TODOTXT_AUTO_ARCHIVE=${TODOTXT_AUTO_ARCHIVE:-1} +TODOTXT_DATE_FORMAT=${TODOTXT_DATE_FORMAT:-%Y-%m-%d} TODOTXT_DATE_ON_ADD=${TODOTXT_DATE_ON_ADD:-0} TODOTXT_PRIORITY_ON_ADD=${TODOTXT_PRIORITY_ON_ADD:-} TODOTXT_DEFAULT_ACTION=${TODOTXT_DEFAULT_ACTION:-} @@ -747,6 +750,9 @@ fi if [ -n "$OVR_TODOTXT_PLAIN" ]; then TODOTXT_PLAIN="$OVR_TODOTXT_PLAIN" fi +if [ -n "$OVR_TODOTXT_DATE_FORMAT" ]; then + TODOTXT_DATE_FORMAT="$OVR_TODOTXT_DATE_FORMAT" +fi if [ -n "$OVR_TODOTXT_DATE_ON_ADD" ]; then TODOTXT_DATE_ON_ADD="$OVR_TODOTXT_DATE_ON_ADD" fi @@ -818,7 +824,7 @@ _addto() if [[ "$TODOTXT_DATE_ON_ADD" -eq 1 ]]; then local now - now=$(date '+%Y-%m-%d') + now=$(date "+$TODOTXT_DATE_FORMAT") input=$(echo "$input" | sed -e 's/^\(([A-Z]) \)\{0,1\}/\1'"$now /") fi if [[ -n "$TODOTXT_PRIORITY_ON_ADD" ]]; then @@ -1261,7 +1267,7 @@ case $action in # Check if this item has already been done if [ "${todo:0:2}" != "x " ]; then - now=$(date '+%Y-%m-%d') + now=$(date "+$TODOTXT_DATE_FORMAT") # remove priority once item is done sed -i.bak "${item}s/^(.) //" "$TODO_FILE" sed -i.bak "${item}s|^|x $now |" "$TODO_FILE"