12-12-2006, 03:44 AM
Untested, hope I got it right.
Weeks are assumed to start on Sunday; day of year is zero-based.
Compiles with FreeBASIC 0.17b (current CVS version)... no guarantees it'll work anywhere else.
Weeks are assumed to start on Sunday; day of year is zero-based.
Compiles with FreeBASIC 0.17b (current CVS version)... no guarantees it'll work anywhere else.
Code:
declare function isleapyear(byval i as integer) as integer
declare function monthlen(byval m as integer, byval leap as integer) as integer
declare function monthname(byval m as integer) as string
declare sub yeardaytomonth(byval i as integer, byval leap as integer, byref m as integer, byref d as integer)
declare sub yeardaytoweek(byval i as integer, byref w as integer, byref d as integer)
declare function weekdayname(byval d as integer) as string
dim y as integer, i as integer, leap as integer, m as integer, w as integer, d as integer
do
input "Year (-1 to quit): ", y
if y < 0 then end
retry:
input "Day of year (-1 to quit): ", i
if i < 0 then end
if i > 364 then goto retry
leap = isleapyear(y)
yeardaytomonth(i, leap, m, d)
print d + 1 & " " & monthname(m)
yeardaytoweek(i, w, d)
print weekdayname(d) & " of week " & w
loop
function isleapyear(byval i as integer) as integer
isleapyear = 0
if i mod 4 = 0 then
if i mod 100 = 0 then
if i mod 400 = 0 then
isleapyear = 1
end if
else
isleapyear = 1
end if
end if
end function
function monthlen(byval m as integer, byval leap as integer) as integer
static monthlentb(0 to 11) as integer = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if (leap <> 0) and (m = 1) then
return 29
else
return monthlentb(m)
end if
end function
function monthname(byval m as integer) as string
static monthnametb(0 to 11) as zstring ptr = {@"Jan", @"Feb", @"Mar", @"Apr", @"May", @"Jun", _
@"Jul", @"Aug", @"Sep", @"Oct", @"Nov", @"Dec"}
return *monthnametb(m)
end function
sub yeardaytomonth(byval i as integer, byval leap as integer, byref m as integer, byref d as integer)
static monthstarttb(0 to 12) as integer = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}
dim n as integer, add as integer = 0
for n = 0 to 11
if (i >= (monthstarttb(n) + add)) and (i < (monthstarttb(n + 1) + add)) then exit for
if n = 1 then add = 1
next n
m = n
d = i - monthstarttb(n)
end sub
sub yeardaytoweek(byval i as integer, byref w as integer, byref d as integer)
w = i \ 7
d = i mod 7
end sub
function weekdayname(byval d as integer) as string
static daynametb(0 to 6) as zstring ptr = {@"Sun", @"Mon", @"Tue", @"Wed", @"Thu", @"Fri", @"Sat"}
return *daynametb(d)
end function