-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutctime
More file actions
executable file
·30 lines (26 loc) · 886 Bytes
/
utctime
File metadata and controls
executable file
·30 lines (26 loc) · 886 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
#! /usr/bin/env bash
show_help() {
printf "Usage: utctime [-r|--reverse] <TIME_HH:MM>\n"
printf "Convert UTC to local time (default) or local time to UTC with -r/--reverse.\n"
exit 0
}
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_help
fi
if [ -z "$2" ] && [ "$1" != "-r" ] && [ "$1" != "--reverse" ]; then
# If no options are passed, assume first argument is UTC time
input_time="$1"
conversion="UTC to Local"
converted_time=$(date -d "$input_time UTC" +"%H:%M %Z")
elif [ "$1" = "-r" ] || [ "$1" = "--reverse" ]; then
# Convert local time to UTC
input_time="$2"
conversion="Local to UTC"
# Explicitly set the input time as local time
converted_time=$(date -d "TZ=\"$(date +%Z)\" $input_time" -u +"%H:%M UTC")
else
show_help
fi
echo "$conversion:"
echo "Input Time: $input_time"
echo "Converted Time: $converted_time"