Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
another date challenge
#2
Untested, hope I got it right. Tongue

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
Reply


Messages In This Thread
another date challenge - by Agamemnus - 12-11-2006, 11:04 PM
another date challenge - by DrV - 12-12-2006, 03:44 AM
another date challenge - by Agamemnus - 12-13-2006, 01:02 AM
another date challenge - by DrV - 12-14-2006, 12:41 AM
Re: another date challenge - by Moneo - 12-14-2006, 06:36 AM
another date challenge - by Agamemnus - 12-16-2006, 01:23 AM
another date challenge - by Antoni Gual - 12-16-2006, 01:47 AM
another date challenge - by Moneo - 12-18-2006, 08:48 AM
another date challenge - by Antoni Gual - 12-18-2006, 10:26 PM
another date challenge - by Agamemnus - 12-19-2006, 12:07 AM
another date challenge - by Antoni Gual - 12-19-2006, 02:07 AM
another date challenge - by Agamemnus - 12-25-2006, 06:13 AM
another date challenge - by Antoni Gual - 12-25-2006, 03:30 PM
another date challenge - by Moneo - 12-26-2006, 01:00 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)