Created
December 6, 2022 00:52
-
-
Save dazza-codes/0aba34c4bcd673a7a0e9e65778162664 to your computer and use it in GitHub Desktop.
Bash date manipulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
hour_trunc12 () { | |
hr=$1 | |
echo "12 * ($hr / 12)" | bc | |
} | |
# # 00 hours | |
# echo $(hour_trunc12 0) #> 0 | |
# echo $(hour_trunc12 11) #> 0 | |
# | |
# # 12 hours | |
# echo $(hour_trunc12 12) #> 12 | |
# echo $(hour_trunc12 23) #> 12 | |
datetime=$(date --utc +%Y-%m-%dT%H:%M:%S+00:00) | |
echo $datetime | |
date_str=$(date --utc --date "${datetime}" +%Y-%m-%d) | |
echo $date_str | |
hr=$(date --utc --date "${datetime}" +%H) | |
hr=$(hour_trunc12 $hr) | |
echo $hr | |
date_hour=$(date --utc --date "${date_str} + ${hr} hours" +%Y-%m-%dT%H:%M:%S+00:00) | |
echo $date_hour |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment