Created
October 27, 2021 15:42
-
-
Save TomoTom0/d01909b2a1e58455a7b0b66213daa362 to your computer and use it in GitHub Desktop.
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
# convert functions about MJD | |
function date2mjd(){ | |
if (( $# == 0 )) ; then | |
# pipe | |
args=$(cat /dev/stdin) | |
else | |
# direct arguments | |
args=$@ | |
fi | |
date_strings=($args) | |
for date_str in ${args[@]}; do | |
# the format of date_str is required "(yy)yy[-/](m)m[-/](d)d" or "Mon[-/](d)d[-/](yy)yy" | |
date_str_tmp=$(date -d ${date_str} +%Y-%m-%d 2>/dev/null) | |
if [[ "x${date_str_tmp}" == "x" ]]; then | |
continue | |
fi | |
date_list=(${date_str_tmp//-/ }) | |
y=$(echo ${date_list[0]} |sed s/^.*([0-9]+).*$/\1/) | |
m=$(echo ${date_list[1]}|sed s/^.*([0-9]+).*$/\1/) | |
m=$((10#$m)) | |
d=$(echo ${date_list[2]}|sed s/^.*([0-9]+).*$/\1/) | |
if [[ $m -le 2 ]]; then | |
y=$(($y - 1)) | |
m=$(($m + 12)) | |
fi | |
val1=$(echo "$y * 365.25 - 0.5" | bc | printf "%.0f" $(cat)) | |
val2=$(( $y / 400)) | |
val3=$(( $y / 100)) | |
val4=$(echo "($m - 2) * 30.59 - 0.5"| bc | printf "%.0f" $(cat)) | |
MJD=$(($val1 + $val2 - $val3 + $val4 + $d - 678912)) | |
echo $MJD | |
done | |
} | |
function mjd2date(){ | |
if (( $# == 0 )) ; then | |
# pipe | |
args=$(cat /dev/stdin) | |
else | |
# direct arguments | |
args=$@ | |
fi | |
date_strings=($args) | |
for mjd_str in ${args[@]}; do | |
mjd_tmp=$(echo ${mjd_str} | sed s/^.*([0-9]+).*$/\1/) | |
val_n=$(($mjd_tmp + 678881 )) | |
val_a1=$(( 4 * ($val_n + 1) /146097 + 1)) | |
val_a2=$((3 * $val_a1 / 4)) | |
val_a=$(( 4 * $val_n + 3 + 4 * $val_a2)) | |
val_b1=$(($val_a % 1461 / 4 )) | |
val_b=$((5 * $val_b1 + 2)) | |
y=$(( $val_a / 1461 )) | |
m=$(( $val_b / 153 + 3)) | |
d=$(( $val_b % 153 / 5 + 1)) | |
if [[ $m -ge 13 ]]; then | |
y=$(($y + 1)) | |
m=$(($m - 12)) | |
fi | |
echo "$y-$m-$d" | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment