April 26, 2024, 12:42:55 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Date calulations

Started by Egil, February 22, 2011, 02:33:51 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Egil

Suddenly I realized that I needed to do date calulations. And now I am very grateful to Graham (GWS) for producing high accuracy Julian Day Number routines last year over at: http://www.codercreations.com/forums/index.php?topic=3872.0
His JDN routines will be the heart of most of my calulations.
This morning I made routines for calulating Easter and WeekNumbers.  The Weeknumber routine returns negative numbers for weeks belonging to last year, e.g. -52 and -53 for the respective weeks last year, and -1 for the first week next year.

That works just fine for me, but I wonder if there is a standard method for presenting weeknumbers for last or next years?

Regards,
Egil


Here is my weeknumber code:
'
' week_number.cba   By Egil-2011
' Using algoritHm from:  http://www.tondering.dk/claus/cal/node8.html
'

DECLARE IsoWeekNumber(year:int,month:int,day:int)

'---------------- Test program --------------------------------------------
openconsole
def yr,mm,dy,wn:int

print" ENTER empty date to Quit":print

do
input" Enter Year:  ", yr
input" Enter Month: ", mm
input" Enter Day:   ", dy
if dy = 0 then goto FINISHED
wn = IsoWeekNumber(yr,mm,dy)

print "Week Number: " + str$(wn)

print
until inkey$=chr$(27)

FINISHED:
closeconsole
end
'---------------- End of Test program -------------------------------------


'
'Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨
' Find ISO Weeknumbers (New week start mondays)
' Input  : Year, Month and Day as integers
' Returns: WeekNumber as integer
'
' Note:
' If number returned is negative:
'  -52 or -53 WeekNumber was last year. (Year-1)
' -1 WeekNumber is next year. (Year+1)
'
SUB IsoWeekNumber(year:int,month:int,day:int)

def a, b, c, d, e, f, g, n s,:int

if month < 3 :' For dates in January and February
a = year-1
b = int(a/4) - int(a/100) + int(a/400)
c = int((a-1)/4) - int((a-1)/100) + int((a-1)/400)
s = b - c
e = 0
f = day-1 + 31*(month-1)
endif

if month > 2 :'For dates in March through December
a = year
b = int(a/4) - int(a/100) + int(a/400)
c = int((a-1)/4) - int((a-1)/100) + int((a-1)/400)
s = b - c
e = s + 1
f = day + ((153*(month-3)+2)/5) + 58 + s
endif

' For all months:
g = (a+b) % 7
d = (f+g-e) % 7
n = f + 3 - d

if n < 0 then return -(53 - int((g-s)/5)) :' last week LAST YEAR!!!
if n > (364 + s) then return -1 :' week 1 NEXT YEAR!!!

RETURN int(n/7) + 1

Support Amateur Radio  -  Have a ham  for dinner!

Egil

And here is my code for calulating Easter. I hope you all noticed that the algorithm was found on the web pages for a BBC radio theater play!!! ;D ;D ;D

Have fun!

Egil

'
' easter.cba
' By Egil 2011
'
DECLARE Easter(Year:int)

'---------------- Test program --------------------------------------------
openconsole
def yr:int
print" Press ENTER (only) to quit"

do
input" Enter Year: ", yr
if yr = 0 then goto FINISHED
print "Easterday: " + Easter(yr)
until inkey$=chr$(27)

FINISHED:
closeconsole
end
'---------------- End of Test program -------------------------------------

'
'Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨Ã,¨
' Calculate date of Easter Day - (valid from 1700 to 2299)
' Algorithm from:  http://www.bbc.co.uk/dna/h2g2/A653267
'
SUB Easter(Year:int)
def a,b,c,d,e,f,g,h,i,j,k,m,n,Month,Day:int
def ret:string

a = Year % 19
b = int(Year/100)
c = Year % 100
d = int(b/4)
f = int(c/4)
g = c % 4

h = int((b+8)/25)
i = int((b-h+1)/3)
j = ((19*a) + b - d - i + 15) % 30
k = (32 + (2*e) + (2*f) - j - g) % 7
m = int((a+(11*j)+(22*k))/451)
n = j + k - (7*m) + 114

Month = int(n/31)
Day = (n%31) + 1

RETURN using("0####", Year) + "-" + using("0##", Month) + "-" + using("0##", Day)

Support Amateur Radio  -  Have a ham  for dinner!