{"id":708,"date":"2016-03-21T00:47:31","date_gmt":"2016-03-21T00:47:31","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=708"},"modified":"2016-07-06T14:02:02","modified_gmt":"2016-07-06T14:02:02","slug":"command-line-fun-julian-day-number","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/03\/21\/command-line-fun-julian-day-number\/","title":{"rendered":"command line fun &#8211; julian day number"},"content":{"rendered":"<p>I keep getting into a situation where I need to do some simple date calculations.\u00a0 This is not a problem using a language like Java or when using a language that lets you add additional modules like Perl.\u00a0 Unfortunately, some of the tasks I get at work don&#8217;t allow me the luxury of choosing which technologies are available to me.<\/p>\n<p>Yet the algorithms themselves are usually pretty well defined and can be recreated in a different language if needed by.\u00a0 In this particular case, the problems of calculating using dates is not unique and has been solved a long time a ago by astronomers.\u00a0 The solution was to start counting at zero at the beginning of the calendar and each day following day is increased by an additional value.<\/p>\n<blockquote>\n<h6>The <b>Julian Day Number<\/b> (<b>JDN<\/b>) is the integer assigned to a whole solar day in the Julian day count starting from noon <a title=\"Greenwich Mean Time\" href=\"https:\/\/en.wikipedia.org\/wiki\/Greenwich_Mean_Time\">Greenwich Mean Time<\/a>, with Julian day number 0 assigned to the day starting at noon on January 1, <a class=\"mw-redirect\" title=\"4713 BC\" href=\"https:\/\/en.wikipedia.org\/wiki\/4713_BC\">4713\u00a0BC<\/a>, <a title=\"Proleptic Julian calendar\" href=\"https:\/\/en.wikipedia.org\/wiki\/Proleptic_Julian_calendar\">proleptic Julian calendar<\/a><\/h6>\n<\/blockquote>\n<p>This is all described on the wikipedia webpage, but more interesting than knowing how astronomers thought about their problems is all of their calculations that allow you to convert your normal Gregorian date into a Juliane Day Number.<\/p>\n<p>Once the date is converted, you can then do simple math between the two dates to find the difference in days.\u00a0 It is a simple matter of adding a integer to see what the date is after that calculation or even what day of the week that day is.<\/p>\n<p>A bash script that can do date calculations may not necessarily be the most efficient technology decision, but it is portable and when not needed for high volume processing may be right solution for a particular job.<\/p>\n<p>Besides, it is fun and all that is necessary is the ability to perform a few calculations, I have a small explanation of both <a href=\"http:\/\/xxx\">floating point and integer math in bash<\/a> in another article.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\n\r\n# https:\/\/en.wikipedia.org\/wiki\/Julian_day#Converting_Julian_or_Gregorian_calendar_date_to_Julian_day_number\r\n\r\n# div = floor(dividend \/ divisor)\r\ndiv()\r\n{\r\n\u00a0 echo $(( $1 \/ $2 ))\r\n}\r\n# quotient = dividend \/ divisor\r\nmodulo()\r\n{\r\n\u00a0 echo $(($1 % $2))\r\n}\r\n\r\n# the lowest whole number\r\nfloor()\r\n{\r\n\u00a0 X=$1\r\n\u00a0 echo ${X%.*}\r\n}\r\n\r\n#####################################################################################################\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\r\n# a = floor ((14 - month)\/12)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\r\n# y = year + 4800 - a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\r\n# m = month + 12a - 3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0 aa\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 bb\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 dd\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ee\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ff\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 gg\u00a0\u00a0\u00a0\u00a0 #\r\n#JDN = day + floor ((153m + 2) \/ 5) + 365y + floor (y\/4) - floor (y\/100) + floor (y\/400) - 32045\u00a0\u00a0\u00a0 #\r\n#####################################################################################################\r\n\r\ngetJulian()\r\n{\r\n\u00a0 YEAR=$1\r\n\u00a0 MONTH=$2\r\n\u00a0 DAY=$3\r\n\r\n\u00a0 a=`echo \"(14 - $MONTH) \/ 12\" | bc -l`\r\n\u00a0 a=`echo 0${a}`\r\n\u00a0 a=`floor $a`\r\n\u00a0 y=`echo $YEAR + 4800 - $a | bc -l`\r\n\u00a0 m=`echo \"$MONTH + 12 * $a - 3\" | bc -l`\r\n\u00a0 #echo a $a\r\n\u00a0 #echo y $y\r\n\u00a0 #echo m $m\r\n\r\n\r\n\u00a0 aa=$DAY\r\n\u00a0 #echo aa $aa\r\n\r\n\u00a0 bb=`echo \"(( 153 * $m + 2) \/ 5)\" | bc -l `\r\n\u00a0 bb=`floor $bb`\r\n\u00a0 #echo bb $bb\r\n\r\n\u00a0 cc=`echo \"365 * $y\" | bc -l`\r\n\u00a0 #echo cc $cc\r\n\r\n\u00a0 dd=`echo \"$y \/ 4\" | bc -l`\r\n\u00a0 dd=`floor $dd`\r\n\u00a0 #echo dd $dd\r\n\r\n\u00a0 ee=`echo \"$y \/ 100\" | bc -l`\r\n\u00a0 ee=`floor $ee`\r\n\u00a0 #echo ee $ee\r\n\r\n\u00a0 ff=`echo \"$y \/ 400\" | bc -l`\r\n\u00a0 ff=`floor $ff`\r\n\u00a0 #echo ff $ff\r\n\r\n\u00a0 gg=32045\r\n\u00a0 #echo gg $gg\r\n\r\n\u00a0 JDN=`echo \"$aa + $bb + $cc + $dd - $ee + $ff - $gg\" | bc -l`\r\n\u00a0 #echo JDN $JDN\r\n\u00a0 echo $JDN\r\n}\r\n\r\nday=1\r\nmonth=1\r\nyear=2000\r\nval=`getJulian $year $month $day`\r\necho $val\r\necho .\r\n\r\nfor i in 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25\r\ndo\r\n  val=`getJulian $year $month $i`\r\n  echo $val\r\ndone<\/code><\/pre>\n<\/div>\n<p>This is not the only formula to do the conversion from a Gregorian date to a Juliane day number.<\/p>\n<p>Depending on the sources of your algorithm not all Julian day numbers will be compatible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I keep getting into a situation where I need to do some simple date calculations.\u00a0 This is not a problem using a language like Java or when using a language that lets you add additional modules like Perl.\u00a0 Unfortunately, some &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/03\/21\/command-line-fun-julian-day-number\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,20],"tags":[17,39],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/708"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=708"}],"version-history":[{"count":13,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/708\/revisions"}],"predecessor-version":[{"id":817,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/708\/revisions\/817"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}