Last active
September 28, 2021 12:30
-
-
Save agail/f42f6772bfd38cae5f6f0b2e37efbb12 to your computer and use it in GitHub Desktop.
NT epoch to Linux epoch converter
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
#!/bin/bash | |
# | |
# Most versions of Unix, for example, use January 1, 1970 (1970-01-01 00:00:00) as the epoch date | |
# Windows uses January 1, 1601 (1601-01-01 00:00:00) | |
# Macintosh systems use January 1, 1904 (1904-01-01 00:00:00) | |
# Digital Equipment Corporation's Virtual Memory System (VMS) uses November 17, 1858 (1858-11-17 00:00:00) | |
offset=$((134774*24*60*60)) # number of days from Jan 1st 1601 to Jan 1st 1970 (incl leap year days), converted to seconds | |
timestamp=$1 # windows timestamp | |
format=$2 | |
human () { | |
date -d @${unixTimestamp} '+%F %T %Z' # display that timestamp in YYYY-MM-DD HH:MM:SS TZ | |
(TZ=":Europe/Stockholm" date -d @${unixTimestamp} '+%F %T %Z') # display that timestamp in YYYY-MM-DD HH:MM:SS TZ with localized time zone | |
} | |
epoc () { | |
unixTimestamp=$((timestamp/10000000-offset)) # convert 100-nanosecond interval to second, adjust for offset | |
if [ "${format}" == "human" ]; then | |
human | |
else | |
echo linux epoc: ${unixTimestamp} | |
fi | |
} | |
if [ ! "${timestamp}" ]; then | |
echo -e "\n \e[91mWindows epoc time missing\n\e[m" | |
exit | |
fi | |
epoc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment