|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +function bashunit::date::to_epoch() { |
| 4 | + local input="$1" |
| 5 | + |
| 6 | + # Already epoch seconds (all digits) |
| 7 | + case "$input" in |
| 8 | + *[!0-9]*) ;; # contains non-digits, continue to ISO parsing |
| 9 | + *) |
| 10 | + echo "$input" |
| 11 | + return 0 |
| 12 | + ;; |
| 13 | + esac |
| 14 | + |
| 15 | + # Format conversion (GNU vs BSD date) |
| 16 | + local epoch |
| 17 | + # Try GNU date first (-d flag) |
| 18 | + epoch=$(date -d "$input" +%s 2>/dev/null) && { |
| 19 | + echo "$epoch" |
| 20 | + return 0 |
| 21 | + } |
| 22 | + # Try BSD date (-j -f flag) with ISO 8601 datetime + timezone offset |
| 23 | + epoch=$(date -j -f "%Y-%m-%dT%H:%M:%S%z" "$input" +%s 2>/dev/null) && { |
| 24 | + echo "$epoch" |
| 25 | + return 0 |
| 26 | + } |
| 27 | + # Try BSD date with ISO 8601 datetime format |
| 28 | + epoch=$(date -j -f "%Y-%m-%dT%H:%M:%S" "$input" +%s 2>/dev/null) && { |
| 29 | + echo "$epoch" |
| 30 | + return 0 |
| 31 | + } |
| 32 | + # Try BSD date with space-separated datetime format |
| 33 | + epoch=$(date -j -f "%Y-%m-%d %H:%M:%S" "$input" +%s 2>/dev/null) && { |
| 34 | + echo "$epoch" |
| 35 | + return 0 |
| 36 | + } |
| 37 | + # Try BSD date with date-only format |
| 38 | + epoch=$(date -j -f "%Y-%m-%d" "$input" +%s 2>/dev/null) && { |
| 39 | + echo "$epoch" |
| 40 | + return 0 |
| 41 | + } |
| 42 | + |
| 43 | + # Unsupported format |
| 44 | + echo "$input" |
| 45 | + return 1 |
| 46 | +} |
| 47 | + |
| 48 | +function assert_date_equals() { |
| 49 | + bashunit::assert::should_skip && return 0 |
| 50 | + |
| 51 | + local expected |
| 52 | + expected="$(bashunit::date::to_epoch "$1")" |
| 53 | + local actual |
| 54 | + actual="$(bashunit::date::to_epoch "$2")" |
| 55 | + |
| 56 | + if [[ "$actual" -ne "$expected" ]]; then |
| 57 | + local test_fn |
| 58 | + test_fn="$(bashunit::helper::find_test_function_name)" |
| 59 | + local label |
| 60 | + label="$(bashunit::helper::normalize_test_function_name "$test_fn")" |
| 61 | + bashunit::assert::mark_failed |
| 62 | + bashunit::console_results::print_failed_test "${label}" "${actual}" "to be equal to" "${expected}" |
| 63 | + return |
| 64 | + fi |
| 65 | + |
| 66 | + bashunit::state::add_assertions_passed |
| 67 | +} |
| 68 | + |
| 69 | +function assert_date_before() { |
| 70 | + bashunit::assert::should_skip && return 0 |
| 71 | + |
| 72 | + local expected |
| 73 | + expected="$(bashunit::date::to_epoch "$1")" |
| 74 | + local actual |
| 75 | + actual="$(bashunit::date::to_epoch "$2")" |
| 76 | + |
| 77 | + if ! [[ "$actual" -lt "$expected" ]]; then |
| 78 | + local test_fn |
| 79 | + test_fn="$(bashunit::helper::find_test_function_name)" |
| 80 | + local label |
| 81 | + label="$(bashunit::helper::normalize_test_function_name "$test_fn")" |
| 82 | + bashunit::assert::mark_failed |
| 83 | + bashunit::console_results::print_failed_test "${label}" "${actual}" "to be before" "${expected}" |
| 84 | + return |
| 85 | + fi |
| 86 | + |
| 87 | + bashunit::state::add_assertions_passed |
| 88 | +} |
| 89 | + |
| 90 | +function assert_date_after() { |
| 91 | + bashunit::assert::should_skip && return 0 |
| 92 | + |
| 93 | + local expected |
| 94 | + expected="$(bashunit::date::to_epoch "$1")" |
| 95 | + local actual |
| 96 | + actual="$(bashunit::date::to_epoch "$2")" |
| 97 | + |
| 98 | + if ! [[ "$actual" -gt "$expected" ]]; then |
| 99 | + local test_fn |
| 100 | + test_fn="$(bashunit::helper::find_test_function_name)" |
| 101 | + local label |
| 102 | + label="$(bashunit::helper::normalize_test_function_name "$test_fn")" |
| 103 | + bashunit::assert::mark_failed |
| 104 | + bashunit::console_results::print_failed_test "${label}" "${actual}" "to be after" "${expected}" |
| 105 | + return |
| 106 | + fi |
| 107 | + |
| 108 | + bashunit::state::add_assertions_passed |
| 109 | +} |
| 110 | + |
| 111 | +function assert_date_within_range() { |
| 112 | + bashunit::assert::should_skip && return 0 |
| 113 | + |
| 114 | + local from |
| 115 | + from="$(bashunit::date::to_epoch "$1")" |
| 116 | + local to |
| 117 | + to="$(bashunit::date::to_epoch "$2")" |
| 118 | + local actual |
| 119 | + actual="$(bashunit::date::to_epoch "$3")" |
| 120 | + |
| 121 | + if [[ "$actual" -lt "$from" ]] || [[ "$actual" -gt "$to" ]]; then |
| 122 | + local test_fn |
| 123 | + test_fn="$(bashunit::helper::find_test_function_name)" |
| 124 | + local label |
| 125 | + label="$(bashunit::helper::normalize_test_function_name "$test_fn")" |
| 126 | + bashunit::assert::mark_failed |
| 127 | + bashunit::console_results::print_failed_test "${label}" "${actual}" "to be between" "${from} and ${to}" |
| 128 | + return |
| 129 | + fi |
| 130 | + |
| 131 | + bashunit::state::add_assertions_passed |
| 132 | +} |
| 133 | + |
| 134 | +function assert_date_within_delta() { |
| 135 | + bashunit::assert::should_skip && return 0 |
| 136 | + |
| 137 | + local expected |
| 138 | + expected="$(bashunit::date::to_epoch "$1")" |
| 139 | + local actual |
| 140 | + actual="$(bashunit::date::to_epoch "$2")" |
| 141 | + local delta="$3" |
| 142 | + |
| 143 | + local diff=$((actual - expected)) |
| 144 | + if [[ "$diff" -lt 0 ]]; then |
| 145 | + diff=$((-diff)) |
| 146 | + fi |
| 147 | + |
| 148 | + if [[ "$diff" -gt "$delta" ]]; then |
| 149 | + local test_fn |
| 150 | + test_fn="$(bashunit::helper::find_test_function_name)" |
| 151 | + local label |
| 152 | + label="$(bashunit::helper::normalize_test_function_name "$test_fn")" |
| 153 | + bashunit::assert::mark_failed |
| 154 | + bashunit::console_results::print_failed_test "${label}" "${actual}" "to be within" "${delta} seconds of ${expected}" |
| 155 | + return |
| 156 | + fi |
| 157 | + |
| 158 | + bashunit::state::add_assertions_passed |
| 159 | +} |
0 commit comments