Quantcast
Channel: sleeplessbeastie's notes
Viewing all articles
Browse latest Browse all 770

How to convert date to Julian Day Number using shell script

$
0
0

I was wondering how to calculate Julian Day Number as I will need it to determine the sunrise and sunset times. Algorithms are extensively described so I will just point you to the two most useful pages – Julian day/Wikipedia and Julian Day Numbers/UT.

After a short while I wrote a shell script to convert between Gregorian calendar date and Julian Day Number.

#!/bin/sh
# convert Gregorian calendar date to Julian Day Number 
# convert Julian Day Number to Gregorian calendar date 
#
# algorithm source:
# http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html
# 
# examples:
# $ ./script.sh 15 5 2013
# 2456428
# $ ./script.sh 2456428
# 15/5/2013
#
# contact:
# milosz@sleeplessbeastie.eu
#
 
 
# gtojdn
# convert Gregorian calendar date to Julian Day Number
#
# parameters:
# day
# month
# year
# 
# example:
# gtojdn 15 5 2013
#
# notes:
# algorithm is simplified 
# as it will return 2456428 instead of 2456427.5
#
gtojdn() {
  if [ $2 -le 2 ]; then
    y=$(($3 - 1))
    m=$(($2 + 12))
  else
    y=$3
    m=$2
  fi
  d=$1
 
  x=$(echo "2 - $y / 100 + $y / 400" | bc)
  x=$(echo "($x + 365.25 * ($y + 4716))/1" | bc) 
  x=$(echo "($x + 30.6001 * ($m + 1))/1" | bc)
 
  echo $(echo "($x + $d - 1524.5 + 0.5)/1" | bc) # added 0.5                                                  
}
 
 
# jdntog
# convert Julian Day Number to Gregorian calendar
#
# parameters:
# jdn
#
# example:
# jdntog 2456428
#
# notes:
# algorithm is simplified
# as you need to use 2456428 as a parameter 
# instead of 2456427.5
#
jdntog() {
  z=$(echo "($1)/1" | bc) # /1 to get integer
                          # original algorithm:  z=$(echo "$1+0.5" | bc)
  w=$(echo "(($z - 1867216.25)/36524.25)/1" | bc)
  x=$(echo "$w / 4" | bc)
  a=$(echo "$z + 1 + $w - $x" | bc)
  b=$(echo "$a + 1524" | bc)
  c=$(echo "(($b - 122.1) / 365.25)/1" | bc)
  d=$(echo "(365.25 * $c)/1" | bc)
  e=$(echo "(($b - $d) / 30.6001)/1" | bc)
  f=$(echo "(30.6001 * $e)/1" | bc)
 
  md=$(echo "$b - $d - $f" | bc)
  if [ $e -le 12 ]; then
    m=$(echo "$e - 1" | bc)
  else
    m=$(echo "$e - 13" | bc)
  fi
 
  if [ $m -le 2 ]; then
    y=$(echo "$c - 4715" | bc)
  else
    y=$(echo "$c - 4716" | bc)
  fi
 
  echo "$md/$m/$y"
}
 
 
#
# process the command-line arguments
#
if [ "$#" -eq 1 ]; then
  jdntog $1
elif [ "$#" -eq 3 ]; then
  gtojdn $1 $2 $3
else
  d=`date +%d`
  m=`date +%m`
  y=`date +%Y`
  gtojdn $d $m $y
fi

Viewing all articles
Browse latest Browse all 770

Trending Articles