-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp2date.sh
More file actions
31 lines (26 loc) · 845 Bytes
/
timestamp2date.sh
File metadata and controls
31 lines (26 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
# If -h or --help is passed as an argument, display the usage of the script
if [[ $1 = "-h" ]] || [[ $1 = "--help" ]]; then
echo -e "Usage: ./timestamp2date.sh <input>"
echo -e "<input> a integer representing UNIX timestamp in unit of second or millisecond"
exit 0
fi
# Check if no parameters were passed
if (($# != 1)); then
echo -e "Usage: ./timestamp2date.sh <input>"
echo -e "<input> should be a integer representing UNIX timestamp in unit of second or millisecond"
exit 1
fi
# Check if parameter is a number
re="^[0-9]+([.][0-9]+)?$"
if ! [[ $1 =~ $re ]]; then
echo "Error: Not a number" >&2
exit 1
fi
timestamp=$1
if ((${#timestamp} > 10)); then
# Its a millisecond timestamp
timestamp=$(echo "$timestamp / 1000" | bc)
fi
# Convert to date
date -r $timestamp "+%Y-%m-%d %H:%M:%S"