-
-
Save mcwalrus/2b42dfc392dc581a874849d592ab5ba4 to your computer and use it in GitHub Desktop.
Unix datetime converters
This file contains hidden or 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
| #!/bin/bash | |
| unix_to_datetime() { | |
| if command -v gdate >/dev/null 2>&1; then | |
| gdate -u -d "@$timestamp" +"%Y-%m-%d %H:%M:%S" | |
| elif [[ "$OSTYPE" == "darwin"* ]]; then | |
| date -u -r "$timestamp" +"%Y-%m-%d %H:%M:%S" | |
| else | |
| date -u -d "@$timestamp" +"%Y-%m-%d %H:%M:%S" | |
| fi | |
| } | |
| datetime_to_unix() { | |
| local datetime="$1" | |
| if [[ "$OSTYPE" == "darwin"* ]]; then | |
| date -j -u -f "%Y-%m-%d %H:%M:%S" "$datetime" +"%s" | |
| else | |
| date -d -u "$datetime" +"%s" | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment