Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Julian dates


  • Please log in to reply
1 reply to this topic
daveand5
  • Members
  • 20 posts
  • Last active: Feb 10 2009 06:11 PM
  • Joined: 09 Nov 2007
heres a function set and test script for working to and from julian dates
that someone else may find useful.

/*
Available Functions:
gojulian(y,m,d,pd) - converts to julian
goconvertback(JD,pd) - converts from julian
DAYWEEK(JD,par) - gets day of week from julian
Julian date is best for adding days between dates or to find number days between dates
taken from fortran script, simplified as much as possible
use SetFormat, Float ,0.0 before calling
Dave Anderson 11/24/2007
V1.0
*/
;===== converts to julian date 5 digits or 7 digits ========
;===== y=year, m=month, d=day pd =1 for 5 digits, anything else 7 digits==============================
gojulian(y,m,d,pd)
{
    if (m < 3) {
        m += 12
        y--
    }
	jdn:=-678972+d+((153*m-2)//5)+365*y+(y//4)-(y//100)+(y//400)
	if (pd=1)
		return , jdn
	else
		return, jdn+2400000
}

;==============convert from julian date ============================
;====JD= 5 or 7 digit julian date, pd =1 for yyyymmdd, 2 for MMM d, yyyy=
goconvertback(JD,pd)
{
if StrLen(JD)=7
	JD-=2400000
A:=JD+2468569, C:=JD//36524+68, F:=(a-(((146097*C)+3)//4))
g:=(4000*(F+1)//1461001), L := F-(1461*g//4)+31
J := (80*L)//2447, D := L-(2447)*J//80, B := J//11
J := J+2-(12*B), Y := (100*(C-49))+g+B
sr=0%D%
StringRight ,D,sr,2
sr=0%j%
StringRight ,J,sr,2
if (pd=2)
	{
	M:=MONTH(J)
	date=%M% %D%, %Y%
	return , %date%
	}
date=%Y%%J%%D% 
return , %date%
}

MONTH(J)
{
Monstr=OOJanFebMarAprMayJunJulAugSepOctNovDec
StringMid, M,Monstr,J*3,3
RETURN , M
}

;============ returns 3 digit weekday ============
;par is 1 = numerical day (1-7), 2 = 3 digit Abreviation (Sun-Sat)
DAYWEEK(JD,par)
{
Daystr=OOSunMonTueWedThuFriSat
if StrLen(JD)=7
	JD-=2400000
NDW:=mod(JD+2,7)+1
if (par=1)
	{
	StringMid, DW,Daystr,NDW*3,3
	return, DW
	}
RETURN , NDW
}


and the test script:

;SetFormat , Float,0.0
CYear=%A_YYYY% ;current 4 digit year 2007
CMonth=%A_MM% ;current 2 digit month ie 01
CDay=%A_DD% ;current 2 digit day ie 21
MsgBox , date being sent for conversion %CYear% %CMonth% %CDay%

;=============== get julian date in 5 digit version ===========
JDN:=gojulian(CYear,CMonth,CDay,1)
MsgBox , the julian date is %JDN%

;get Day of Week in 3 digit Abbreviation ie Mon from julian =========================
DWA:=DAYWEEK(JDN,1)
MsgBox , Day of Week Abbrev is %DWA%

;get day of week number 1-7 from julian ==============================================
DWN:=DAYWEEK(JDN,2)
MsgBox , Day of Week Number is %DWN%
JDN+=5 ; add 5 days ================================================================
;===================================convert from julian date starts here ============
;======================= sending JDN removing all right of decimal point ===========
; =========================dont need the time of day part of julian date ===========
Transform,JDNc,Round,JDN
MsgBox , Julian Date being converted %JDNc%
dateT:=goconvertback(JDNc,1) ; returns 8 digit date in YYYYMMDD format =============
MsgBox , 8 digit date = %dateT% 

dateT:=goconvertback(JDNc,2) ; returns date in MMM d, yyyyy format
MsgBox , Month day, year = %dateT% 
Exit

#Include, JulianFunctions.ahk


Hope someone finds it useful....

garry
  • Spam Officer
  • 3219 posts
  • Last active: Sep 20 2018 02:47 PM
  • Joined: 19 Apr 2005
thank you daveand5,
interesting script, here I used a script from Skan to use for google-search
( when no input calculates from yesterday ), for test just press okay
;--------------------- GREGORIAN TO JULIAN SKAN ---------------------------------
JULIAN:
 gui,7: Color, 000000

 LAC=mina OR mitag

 stringmid,DTS,A_NOW,1,8
 stringmid,THYm,A_NOW,1,6
 vanaf=%thym%01

 gui,7: Font, s10 cwhite, Verdana
 gui,7: Add, Text, x20   y10       h20 ,Search for
 gui,7: Add, Text, x20   y40       h20 ,VANAF
 gui,7: Add, Text, x20   y70       h20 ,UNTIL

 gui,7: Font , CDefault , FixedSys
 gui,7: Add, Edit, x130  y10  w350 h20  vLAC  ,%LAC%
 gui,7: Add, Edit, x130  y40  w100 h20  vVANAF ,               ;%vanaf%        ;vanaf
 gui,7: Add, Edit, x130  y70  w100 h20  vUNTIL,%DTS%           ;maybe today

 gui,7:Add,Button, x260  y70  w70  h20 gCALC,OK

       UNTIL2:=(JulianDate(%DTS%))
       DIF:=2                           ;minus 2 days
       VANAF2:=(UNTIL2-DIF)

 gui,7: Show,      x2   y2  w500 h100,JULIAN GOOGLE -2 days q=daterange:%VANAF2%-%UNTIL2% %LAC%
 return
;----------------------------------------------------------------------------------------------

 CALC:
 gui,7:submit,nohide

  if VANAF=
       {
       UNTIL2:=((JulianDate(%DTS%))+1)
       DIF:=2                         ;minus 1 days when no input
       VANAF2:=((UNTIL2-DIF)+1)
       }

  else
       {
       VANAF:=VANAF
       UNTIL:=UNTIL
       VANAF2:=((JulianDate(VANAF))+1)
       UNTIL2:=((JulianDate(UNTIL))+1)    ;plus one day
       }

    run,http://news.google.com/news?complete=1&hl=en&oe=UTF-8&um=1&ie=UTF-8&tab=in&q=daterange:%VANAF2%-%UNTIL2% `%22%LAC%`%22&scoring=n
7guiclose:
Gui,7:Destroy
exitapp
; return

;-----------------------------------------
      JulianDate( YMD="", ChkYMD=1, Rn=1 )
          {
      If ChkYMD
          FormatTime, YMD, %YMD%, yyyyMMdd

        Y := SubStr( YMD, 1,4 ) + 0
        M := SubStr( YMD, 5,2 ) + 0
        D := SubStr( YMD, 7,2 ) + 0

        If ( M<=2 )
           M := M+12, Y := Y-1
        A := 2 - Floor( Y/100 ) + Floor( Floor( Y/100 ) / 4 )
        B := Floor( 365.25  * ( Y+4716) )
        C := Floor( 30.6001 * ( M+1) )
        Return Rn ? Floor( A+B+C+D -1524.5 ) : Round( A+B+C+D -1524.5, 1 )
        }
   return


/*
;--------------------- GREGORIAN TO JULIAN SKAN ---------------------------------
;http://en.wikipedia.org/wiki/Julian_day
;http://www.astronomy.villanova.edu/links/jd.htm
;http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html
;http://home.att.net/~srschmitt/siderealtime.html
;daterange:2454405-2454412 gwbasic
;example daterange:2454300-2454302 gwbasic [for google search 2007-07-21 ]
*/

;====================== END JULIAN GOOGLE daterange ===========================