Skip to content

Instantly share code, notes, and snippets.

@darshitpp
Last active May 12, 2021 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darshitpp/224c9a48ecbcbdbab994fee2e43f0466 to your computer and use it in GitHub Desktop.
Save darshitpp/224c9a48ecbcbdbab994fee2e43f0466 to your computer and use it in GitHub Desktop.
Useful Bash/ZSH Aliases and functions to get Epoch timestamps using Python 2.7 and Python 3
# Add the following to your ~/.bashrc or ~/.zshrc
# prints current epoch in milliseconds
# >epoch
# >1616738907653
alias epoch='python -c "from time import time; print int(round(time() * 1000))"'
# prints current epoch in human readable date in UTC timezone
# >utc
# >2021-03-26 06:08:27.653000
alias utc='python -c "from time import time; import datetime; print datetime.datetime.utcfromtimestamp(int(round(time() * 1000))/ 1000.0)"'
# prints specified epoch in human readable date in System timezone
# >epoch_to_date 1616738907653
# >2021-03-26 11:38:27.653000
epoch_to_date() {
python -c "import datetime; print datetime.datetime.fromtimestamp($1 / 1000.0)"
}
# prints current epoch in human readable date in UTC timezone
# >epoch_to_utc 1616738907653
# >2021-03-26 06:08:27.653000
epoch_to_utc() {
python -c "import datetime; print datetime.datetime.utcfromtimestamp($1 / 1000.0)"
}
# Python 3 version, courtesy https://twitter.com/arion_miles
# prints current epoch in milliseconds
# >epoch
# >1616738907653
alias epoch='python3 -c "from time import time; print(round(time() * 1000))"'
# prints current epoch in human readable date in UTC timezone
# >utc
# >2021-03-26 06:08:27.653000
alias utc='python3 -c "from time import time; import datetime; print(datetime.datetime.utcfromtimestamp(round(time() * 1000)/ 1000.0))"'
# prints specified epoch in human readable date in System timezone
# >epoch_to_date 1616738907653
# >2021-03-26 11:38:27.653000
epoch_to_date() {
python3 -c "import datetime; print(datetime.datetime.fromtimestamp($1 / 1000.0))"
}
# prints current epoch in human readable date in UTC timezone
# >epoch_to_utc 1616738907653
# >2021-03-26 06:08:27.653000
epoch_to_utc() {
python3 -c "import datetime; print(datetime.datetime.utcfromtimestamp($1 / 1000.0))"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment