feat: consistent date formatting, year display, and iso/epoch time flags
This commit is contained in:
parent
6cf2b7d967
commit
6a588aaa9c
2 changed files with 81 additions and 19 deletions
49
nag
49
nag
|
|
@ -335,6 +335,8 @@ _SUBCOMMAND=""
|
|||
_SUBCOMMAND_ARGUMENTS=()
|
||||
_USE_DEBUG=0
|
||||
_YES=0
|
||||
_RAW_TIME=0
|
||||
_EPOCH_TIME=0
|
||||
|
||||
while ((${#}))
|
||||
do
|
||||
|
|
@ -356,6 +358,12 @@ do
|
|||
-f)
|
||||
_YES=1
|
||||
;;
|
||||
--iso)
|
||||
_RAW_TIME=1
|
||||
;;
|
||||
--epoch)
|
||||
_EPOCH_TIME=1
|
||||
;;
|
||||
*)
|
||||
# The first non-option argument is assumed to be the subcommand name.
|
||||
# All subsequent arguments are added to $_SUBCOMMAND_ARGUMENTS.
|
||||
|
|
@ -754,9 +762,8 @@ _get_snooze_display() {
|
|||
local _expiry="${_entry#*$'\t'}"
|
||||
if (( _expiry > _now ))
|
||||
then
|
||||
local _until_date
|
||||
_until_date="$(date -d "@${_expiry}" "+%b %-d")"
|
||||
REPLY=" (snoozed until ${_until_date})"
|
||||
_format_time "${_expiry}"
|
||||
REPLY=" (snoozed until ${REPLY})"
|
||||
fi
|
||||
else
|
||||
REPLY=" (snoozed)"
|
||||
|
|
@ -879,6 +886,18 @@ _tag_list() {
|
|||
_format_time() {
|
||||
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.
|
||||
local _date_parts
|
||||
_date_parts="$(date -d "@${_timestamp}" "+%-I|%M|%p|%Y-%m-%d|%A|%a %b %-d")"
|
||||
|
|
@ -929,13 +948,22 @@ _format_time() {
|
|||
_date_prefix="Tomorrow"
|
||||
elif (( _days_away <= 6 ))
|
||||
then
|
||||
_date_prefix="This ${_day_name}"
|
||||
_date_prefix="${_day_name}"
|
||||
elif (( _days_away <= 13 ))
|
||||
then
|
||||
_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
|
||||
_date_prefix="${_short_date}"
|
||||
fi
|
||||
fi
|
||||
|
||||
REPLY="${_date_prefix}, ${_time_fmt}"
|
||||
}
|
||||
|
|
@ -1482,6 +1510,8 @@ Usage:
|
|||
Options:
|
||||
-e Edit alarms file directly.
|
||||
-f Skip all prompts.
|
||||
--iso Show times as YYYY-MM-DD HH:MM:SS.
|
||||
--epoch Show times as unix timestamps.
|
||||
-v Show version.
|
||||
|
||||
Environment:
|
||||
|
|
@ -2459,7 +2489,8 @@ _snooze_by_tag() {
|
|||
if [[ -n "${_duration_str}" ]]
|
||||
then
|
||||
_until_ts="$(_parse_time "${_duration_str}")"
|
||||
_until_date="$(date -d "@${_until_ts}" "+%b %-d")"
|
||||
_format_time "${_until_ts}"
|
||||
_until_date="${REPLY}"
|
||||
fi
|
||||
|
||||
for _line in "${_ALARMS[@]}"
|
||||
|
|
@ -2506,7 +2537,8 @@ snooze() {
|
|||
if [[ -n "${_duration_str}" ]]
|
||||
then
|
||||
_until_ts="$(_parse_time "${_duration_str}")"
|
||||
_until_date="$(date -d "@${_until_ts}" "+%b %-d")"
|
||||
_format_time "${_until_ts}"
|
||||
_until_date="${REPLY}"
|
||||
fi
|
||||
|
||||
local _line
|
||||
|
|
@ -2570,9 +2602,8 @@ snooze() {
|
|||
_until_ts="$(_parse_time "${_duration_str}")"
|
||||
printf "%s\t%s\\n" "${_target}" "${_until_ts}" >> "${_SNOOZED_FILE}"
|
||||
_release_lock
|
||||
local _until_date
|
||||
_until_date="$(date -d "@${_until_ts}" "+%b %-d")"
|
||||
printf "Snoozed [%s] %s until %s.\\n" "${_target}" "${_message}" "${_until_date}"
|
||||
_format_time "${_until_ts}"
|
||||
printf "Snoozed [%s] %s until %s.\\n" "${_target}" "${_message}" "${REPLY}"
|
||||
else
|
||||
printf "%s\\n" "${_target}" >> "${_SNOOZED_FILE}"
|
||||
_release_lock
|
||||
|
|
|
|||
|
|
@ -84,13 +84,13 @@ load test_helper
|
|||
[[ "${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 _day="$(date -d "@${_ts}" +%A)"
|
||||
write_alarm "$(printf '1\t\t%s\t\tthis alarm' "${_ts}")"
|
||||
run_nag
|
||||
[ "${status}" -eq 0 ]
|
||||
[[ "${output}" =~ "This ${_day},".*"this alarm" ]]
|
||||
[[ "${output}" =~ "${_day},".*"this alarm" ]]
|
||||
}
|
||||
|
||||
@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"
|
||||
local _expiry_ts=$(( $(date +%s) + 86400 * 7 ))
|
||||
printf "1\t%s\\n" "${_expiry_ts}" > "${NAG_DIR}/snoozed"
|
||||
local _expiry_date
|
||||
_expiry_date="$(date -d "@${_expiry_ts}" "+%b %-d")"
|
||||
run_nag
|
||||
[ "${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" {
|
||||
|
|
@ -137,3 +148,23 @@ load test_helper
|
|||
[ "${status}" -eq 0 ]
|
||||
[[ ! "${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" ]]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue