Re: Date conversions

new topic     » goto parent     » topic index » view thread      » older message » newer message

From: Irv
>Does anyone have a routine for converting dates in the
>"Day of our lord Bill Gates" format, i.e. days counted
>starting with 01/01/1980, into normal MM/DD/YYYY format?

     I tried it and came up with the following, which seems to work okay (at
least on the test set).  But if you're going to be dealing with years beyond
2080 you'd need to expand the table.  And if you're going to be dealing with
'negative' years before 1980, you'd probably have to redo the whole thing...


--- BGDates ---

--year lookup table
sequence ey, em
integer x
ey = {}
x = 0

for y = 1980 to 2080 do
   if not remainder( y, 4 ) then
      if not remainder( y, 100 ) then
         --century
         if not remainder( y, 400 ) then
            --leap century
            x = x + 366
         else
            x = x + 365
         end if
      else
         --leap year
         x = x + 366
      end if
   else
      x = x + 365
   end if
   ey = ey & x
end for


--month lookup table
em = {31,28,31,30,31,30,31,31,30,31,30,31}


global function BG_to_mmddyyyy( atom BGDay )
   integer year, month, day, x
   --atom BGDay

   year = 1980
   month = 1
   day = 1

   x = 1
   while BGDay > ey[x] do
      x = x + 1
   end while
   x = x - 1

   if x then
      year = year + x
      BGDay = BGDay - ey[x]
   end if

   em[2] = 28
   if not remainder( year, 4 ) then
      if not remainder( year, 100 ) then
         --century
         if not remainder( year, 400 ) then
            --leap year
            em[2] = 29
         end if
      else
         --leap year
         em[2] = 29
      end if
   end if


   for c = 1 to 12 do
      if BGDay > em[c] then
         BGDay = BGDay - em[c]
         month = month + 1
      else
         exit
      end if
   end for

   day = BGDay

   return( {month, day, year} )
end function


--===== TEST CODE =====--

sequence testdates
testdates =
{ 1,
  2,
  32,
  367,
  731,
  7061,
  7062,
  7063,
  7092,
  7305,
  7306
}

for c = 1 to length( testdates ) do
   printf( 1, "%02d/%02d/%4d\n", BG_to_mmddyyyy( testdates[c] ) )
end for

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu