Skip to content

Commit 517c8c6

Browse files
committed
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.
1 parent b20f9b4 commit 517c8c6

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

tests/t1011-add-format-date.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
3+
test_description='test TODOTXT_DATE_FORMAT environment variable
4+
5+
Ensure that the date format of creation and completion dates is correct
6+
'
7+
. ./test-lib.sh
8+
9+
10+
unset TODOTXT_DATE_FORMAT
11+
test_todo_session 'default date format with env var unset' <<EOF
12+
>>> todo.sh -t add Solve the halting problem
13+
1 2009-02-13 Solve the halting problem
14+
TODO: 1 added.
15+
16+
>>> todo.sh list
17+
1 2009-02-13 Solve the halting problem
18+
--
19+
TODO: 1 of 1 tasks shown
20+
EOF
21+
22+
export TODOTXT_DATE_FORMAT="%Y-%m-%dT%H:%M:%S%z"
23+
test_todo_session 'ISO-8601 with seconds and timezone' <<EOF
24+
>>> todo.sh -t add Solve the halting problem
25+
2 2009-02-13T04:40:00+0000 Solve the halting problem
26+
TODO: 2 added.
27+
28+
>>> todo.sh list
29+
1 2009-02-13 Solve the halting problem
30+
2 2009-02-13T04:40:00+0000 Solve the halting problem
31+
--
32+
TODO: 2 of 2 tasks shown
33+
EOF
34+
35+
test_todo_session 'ISO-8601 completion date' <<EOF
36+
>>> todo.sh -t done 2
37+
2 x 2009-02-13T04:40:00+0000 2009-02-13T04:40:00+0000 Solve the halting problem
38+
TODO: 2 marked as done.
39+
x 2009-02-13T04:40:00+0000 2009-02-13T04:40:00+0000 Solve the halting problem
40+
TODO: $HOME/todo.txt archived.
41+
42+
>>> todo.sh listall
43+
1 2009-02-13 Solve the halting problem
44+
0 x 2009-02-13T04:40:00+0000 2009-02-13T04:40:00+0000 Solve the halting problem
45+
--
46+
TODO: 1 of 1 tasks shown
47+
DONE: 1 of 1 tasks shown
48+
total 2 of 2 tasks shown
49+
EOF
50+
51+
test_done

todo.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ $indentedJoinedConfigFileLocations
151151
TODOTXT_DEFAULT_ACTION="" run this when called with no arguments
152152
TODOTXT_SORT_COMMAND="sort ..." customize list output
153153
TODOTXT_FINAL_FILTER="sed ..." customize list after color, P@+ hiding
154+
TODOTXT_DATE_FORMAT="%Y-%m-%d" customize creation/completion date format
154155
TODOTXT_SOURCEVAR=\$DONE_FILE use another source for listcon, listproj
155156
TODOTXT_SIGIL_BEFORE_PATTERN="" optionally allow chars preceding +p / @c
156157
TODOTXT_SIGIL_VALID_PATTERN=.* tweak the allowed chars for +p and @c
@@ -524,6 +525,7 @@ OVR_TODOTXT_AUTO_ARCHIVE="$TODOTXT_AUTO_ARCHIVE"
524525
OVR_TODOTXT_FORCE="$TODOTXT_FORCE"
525526
OVR_TODOTXT_PRESERVE_LINE_NUMBERS="$TODOTXT_PRESERVE_LINE_NUMBERS"
526527
OVR_TODOTXT_PLAIN="$TODOTXT_PLAIN"
528+
OVR_TODOTXT_DATE_FORMAT="$TODOTXT_DATE_FORMAT"
527529
OVR_TODOTXT_DATE_ON_ADD="$TODOTXT_DATE_ON_ADD"
528530
OVR_TODOTXT_PRIORITY_ON_ADD="$TODOTXT_PRIORITY_ON_ADD"
529531
OVR_TODOTXT_DISABLE_FILTER="$TODOTXT_DISABLE_FILTER"
@@ -642,6 +644,7 @@ TODOTXT_PLAIN=${TODOTXT_PLAIN:-0}
642644
TODOTXT_FORCE=${TODOTXT_FORCE:-0}
643645
TODOTXT_PRESERVE_LINE_NUMBERS=${TODOTXT_PRESERVE_LINE_NUMBERS:-1}
644646
TODOTXT_AUTO_ARCHIVE=${TODOTXT_AUTO_ARCHIVE:-1}
647+
TODOTXT_DATE_FORMAT=${TODOTXT_DATE_FORMAT:-%Y-%m-%d}
645648
TODOTXT_DATE_ON_ADD=${TODOTXT_DATE_ON_ADD:-0}
646649
TODOTXT_PRIORITY_ON_ADD=${TODOTXT_PRIORITY_ON_ADD:-}
647650
TODOTXT_DEFAULT_ACTION=${TODOTXT_DEFAULT_ACTION:-}
@@ -747,6 +750,9 @@ fi
747750
if [ -n "$OVR_TODOTXT_PLAIN" ]; then
748751
TODOTXT_PLAIN="$OVR_TODOTXT_PLAIN"
749752
fi
753+
if [ -n "$OVR_TODOTXT_DATE_FORMAT" ]; then
754+
TODOTXT_DATE_FORMAT="$OVR_TODOTXT_DATE_FORMAT"
755+
fi
750756
if [ -n "$OVR_TODOTXT_DATE_ON_ADD" ]; then
751757
TODOTXT_DATE_ON_ADD="$OVR_TODOTXT_DATE_ON_ADD"
752758
fi
@@ -818,7 +824,7 @@ _addto()
818824

819825
if [[ "$TODOTXT_DATE_ON_ADD" -eq 1 ]]; then
820826
local now
821-
now=$(date '+%Y-%m-%d')
827+
now=$(date "+$TODOTXT_DATE_FORMAT")
822828
input=$(echo "$input" | sed -e 's/^\(([A-Z]) \)\{0,1\}/\1'"$now /")
823829
fi
824830
if [[ -n "$TODOTXT_PRIORITY_ON_ADD" ]]; then
@@ -1261,7 +1267,7 @@ case $action in
12611267

12621268
# Check if this item has already been done
12631269
if [ "${todo:0:2}" != "x " ]; then
1264-
now=$(date '+%Y-%m-%d')
1270+
now=$(date "+$TODOTXT_DATE_FORMAT")
12651271
# remove priority once item is done
12661272
sed -i.bak "${item}s/^(.) //" "$TODO_FILE"
12671273
sed -i.bak "${item}s|^|x $now |" "$TODO_FILE"

0 commit comments

Comments
 (0)