feat: consistent date formatting, year display, and iso/epoch time flags

This commit is contained in:
Lewis Wynne 2026-04-02 20:13:53 +01:00
parent 6cf2b7d967
commit 6a588aaa9c
2 changed files with 81 additions and 19 deletions

49
nag
View file

@ -335,6 +335,8 @@ _SUBCOMMAND=""
_SUBCOMMAND_ARGUMENTS=() _SUBCOMMAND_ARGUMENTS=()
_USE_DEBUG=0 _USE_DEBUG=0
_YES=0 _YES=0
_RAW_TIME=0
_EPOCH_TIME=0
while ((${#})) while ((${#}))
do do
@ -356,6 +358,12 @@ do
-f) -f)
_YES=1 _YES=1
;; ;;
--iso)
_RAW_TIME=1
;;
--epoch)
_EPOCH_TIME=1
;;
*) *)
# The first non-option argument is assumed to be the subcommand name. # The first non-option argument is assumed to be the subcommand name.
# All subsequent arguments are added to $_SUBCOMMAND_ARGUMENTS. # All subsequent arguments are added to $_SUBCOMMAND_ARGUMENTS.
@ -754,9 +762,8 @@ _get_snooze_display() {
local _expiry="${_entry#*$'\t'}" local _expiry="${_entry#*$'\t'}"
if (( _expiry > _now )) if (( _expiry > _now ))
then then
local _until_date _format_time "${_expiry}"
_until_date="$(date -d "@${_expiry}" "+%b %-d")" REPLY=" (snoozed until ${REPLY})"
REPLY=" (snoozed until ${_until_date})"
fi fi
else else
REPLY=" (snoozed)" REPLY=" (snoozed)"
@ -879,6 +886,18 @@ _tag_list() {
_format_time() { _format_time() {
local _timestamp="${1}" local _timestamp="${1}"
if (( _EPOCH_TIME ))
then
REPLY="${_timestamp}"
return
fi
if (( _RAW_TIME ))
then
REPLY="$(date -d "@${_timestamp}" "+%Y-%m-%d %H:%M:%S")"
return
fi
# Single date call to get all needed components. # Single date call to get all needed components.
local _date_parts local _date_parts
_date_parts="$(date -d "@${_timestamp}" "+%-I|%M|%p|%Y-%m-%d|%A|%a %b %-d")" _date_parts="$(date -d "@${_timestamp}" "+%-I|%M|%p|%Y-%m-%d|%A|%a %b %-d")"
@ -929,13 +948,22 @@ _format_time() {
_date_prefix="Tomorrow" _date_prefix="Tomorrow"
elif (( _days_away <= 6 )) elif (( _days_away <= 6 ))
then then
_date_prefix="This ${_day_name}" _date_prefix="${_day_name}"
elif (( _days_away <= 13 )) elif (( _days_away <= 13 ))
then then
_date_prefix="Next ${_day_name}" _date_prefix="Next ${_day_name}"
else
local _alarm_year
_alarm_year="$(date -d "${_alarm_date}" +%Y)"
local _this_year
_this_year="$(date +%Y)"
if [[ "${_alarm_year}" != "${_this_year}" ]]
then
_date_prefix="${_short_date} ${_alarm_year}"
else else
_date_prefix="${_short_date}" _date_prefix="${_short_date}"
fi fi
fi
REPLY="${_date_prefix}, ${_time_fmt}" REPLY="${_date_prefix}, ${_time_fmt}"
} }
@ -1482,6 +1510,8 @@ Usage:
Options: Options:
-e Edit alarms file directly. -e Edit alarms file directly.
-f Skip all prompts. -f Skip all prompts.
--iso Show times as YYYY-MM-DD HH:MM:SS.
--epoch Show times as unix timestamps.
-v Show version. -v Show version.
Environment: Environment:
@ -2459,7 +2489,8 @@ _snooze_by_tag() {
if [[ -n "${_duration_str}" ]] if [[ -n "${_duration_str}" ]]
then then
_until_ts="$(_parse_time "${_duration_str}")" _until_ts="$(_parse_time "${_duration_str}")"
_until_date="$(date -d "@${_until_ts}" "+%b %-d")" _format_time "${_until_ts}"
_until_date="${REPLY}"
fi fi
for _line in "${_ALARMS[@]}" for _line in "${_ALARMS[@]}"
@ -2506,7 +2537,8 @@ snooze() {
if [[ -n "${_duration_str}" ]] if [[ -n "${_duration_str}" ]]
then then
_until_ts="$(_parse_time "${_duration_str}")" _until_ts="$(_parse_time "${_duration_str}")"
_until_date="$(date -d "@${_until_ts}" "+%b %-d")" _format_time "${_until_ts}"
_until_date="${REPLY}"
fi fi
local _line local _line
@ -2570,9 +2602,8 @@ snooze() {
_until_ts="$(_parse_time "${_duration_str}")" _until_ts="$(_parse_time "${_duration_str}")"
printf "%s\t%s\\n" "${_target}" "${_until_ts}" >> "${_SNOOZED_FILE}" printf "%s\t%s\\n" "${_target}" "${_until_ts}" >> "${_SNOOZED_FILE}"
_release_lock _release_lock
local _until_date _format_time "${_until_ts}"
_until_date="$(date -d "@${_until_ts}" "+%b %-d")" printf "Snoozed [%s] %s until %s.\\n" "${_target}" "${_message}" "${REPLY}"
printf "Snoozed [%s] %s until %s.\\n" "${_target}" "${_message}" "${_until_date}"
else else
printf "%s\\n" "${_target}" >> "${_SNOOZED_FILE}" printf "%s\\n" "${_target}" >> "${_SNOOZED_FILE}"
_release_lock _release_lock

View file

@ -84,13 +84,13 @@ load test_helper
[[ "${output}" =~ "Tomorrow,".*"tomorrow alarm" ]] [[ "${output}" =~ "Tomorrow,".*"tomorrow alarm" ]]
} }
@test "list shows This <day> for alarm 3 days away" { @test "list shows <day> for alarm 3 days away" {
local _ts=$(( $(date -d "$(date +%Y-%m-%d)" +%s) + 86400 * 3 + 43200 )) local _ts=$(( $(date -d "$(date +%Y-%m-%d)" +%s) + 86400 * 3 + 43200 ))
local _day="$(date -d "@${_ts}" +%A)" local _day="$(date -d "@${_ts}" +%A)"
write_alarm "$(printf '1\t\t%s\t\tthis alarm' "${_ts}")" write_alarm "$(printf '1\t\t%s\t\tthis alarm' "${_ts}")"
run_nag run_nag
[ "${status}" -eq 0 ] [ "${status}" -eq 0 ]
[[ "${output}" =~ "This ${_day},".*"this alarm" ]] [[ "${output}" =~ "${_day},".*"this alarm" ]]
} }
@test "list shows Next <day> for alarm 10 days away" { @test "list shows Next <day> for alarm 10 days away" {
@ -124,11 +124,22 @@ load test_helper
run_nag at "tomorrow 3pm" "take a break" run_nag at "tomorrow 3pm" "take a break"
local _expiry_ts=$(( $(date +%s) + 86400 * 7 )) local _expiry_ts=$(( $(date +%s) + 86400 * 7 ))
printf "1\t%s\\n" "${_expiry_ts}" > "${NAG_DIR}/snoozed" printf "1\t%s\\n" "${_expiry_ts}" > "${NAG_DIR}/snoozed"
local _expiry_date
_expiry_date="$(date -d "@${_expiry_ts}" "+%b %-d")"
run_nag run_nag
[ "${status}" -eq 0 ] [ "${status}" -eq 0 ]
[[ "${output}" =~ "snoozed until ${_expiry_date}" ]] # Should use the same format as list dates (e.g. "Next Monday, 3pm").
[[ "${output}" =~ "snoozed until " ]]
[[ "${output}" =~ "am)" ]] || [[ "${output}" =~ "pm)" ]]
}
@test "list shows year for alarm in a different year" {
local _next_year=$(( $(date +%Y) + 1 ))
local _ts
_ts="$(date -d "${_next_year}-06-15 14:00:00" +%s)"
write_alarm "$(printf '1\t\t%s\t\tnext year alarm' "${_ts}")"
run_nag
[ "${status}" -eq 0 ]
[[ "${output}" =~ "${_next_year}" ]]
[[ "${output}" =~ "next year alarm" ]]
} }
@test "list does not show (snoozed) for unsnoozed alarm" { @test "list does not show (snoozed) for unsnoozed alarm" {
@ -137,3 +148,23 @@ load test_helper
[ "${status}" -eq 0 ] [ "${status}" -eq 0 ]
[[ ! "${output}" =~ "snoozed" ]] [[ ! "${output}" =~ "snoozed" ]]
} }
@test "--iso flag shows YYYY-MM-DD HH:MM:SS format" {
local _ts=$(( $(date -d "$(date +%Y-%m-%d)" +%s) + 86400 + 54000 ))
write_alarm "$(printf '1\t\t%s\t\traw alarm' "${_ts}")"
local _expected
_expected="$(date -d "@${_ts}" "+%Y-%m-%d %H:%M:%S")"
run "${_NAG}" -f --iso
[ "${status}" -eq 0 ]
[[ "${output}" =~ "${_expected}" ]]
[[ "${output}" =~ "raw alarm" ]]
}
@test "--epoch flag shows unix timestamps" {
local _ts=$(( $(date -d "$(date +%Y-%m-%d)" +%s) + 86400 + 54000 ))
write_alarm "$(printf '1\t\t%s\t\tepoch alarm' "${_ts}")"
run "${_NAG}" -f --epoch
[ "${status}" -eq 0 ]
[[ "${output}" =~ "${_ts}" ]]
[[ "${output}" =~ "epoch alarm" ]]
}