lib/timestamp.sh

717 B · 29 lines · bash

1#!/usr/bin/env bash
2
3brt_timestamp() {
4 local input="${1:-now}"
5
6 case "$input" in
7 now)
8 brt_info "Current time:"
9 date -u +"%Y-%m-%d %H:%M:%S UTC (unix: $(date +%s))"
10 ;;
11 *)
12 if [[ "$input" =~ ^[0-9]+$ ]]; then
13 brt_info "Unix timestamp ${input}:"
14 date -r "$input" 2>/dev/null || date -d "@${input}" 2>/dev/null || {
15 brt_error "Invalid timestamp: ${input}"
16 exit 1
17 }
18 else
19 brt_info "Parsing date '${input}':"
20 date -j -f "%Y-%m-%d %H:%M:%S" "$input" +%s 2>/dev/null || \
21 date -d "$input" +%s 2>/dev/null || {
22 brt_error "Could not parse date: ${input}"
23 exit 1
24 }
25 fi
26 ;;
27 esac
28}
29