MetaCardians,
Last week I submitted a version of the Julian/Gregorian/Zeller Algorithm date 
convertor (say that three time!)
Supposedly, it would return the "Day-of-the-Week" for any date....

Well, after an initial blush of success, I dug into the "reality" of the Zeller 
Algorithm and the assumptions we
make.
First, I’d made an error in the adjusted algorithm (initial blush)
Second, returning any day of the week is the easy part, but is it the "correct" day?

Without going into the long chant of the inaccuracies of both the Julian and Gregorian 
calendars (if we used the
highly accurate Toltec calendar, we would not be in this mess, Ollie!), I looked at 
the way HyperCard uses the
"dateItems" function. HC doesn’t convert to the Julian then back to the Gregorian to 
determine the day of the week.
If the date is valid,(not May 35, 1999, for example), HC probably computes the day of 
the week according to the
following*:

The day of the week 'W' (0 = Sunday) is given by

  W = ((13*M - 1) / 5 + D + Y + Y/4 + C/4 - (2*C) mod 7 + 7) mod 7;

where
C is the century (year DIV 100)
Y is the last two digits of the year (year MOD 100)
D is the day of the month
M is a special month number, where Jan and Feb are taken as
month 11 and 12 of the previous year (subtract 1 from the year)
and each division is truncating division, and cannot be combined.

(* from Alan P Barrett)

The section "mod 7 + 7) mod 7"  is to insure a positive result.

Ok, you ask, is it correct now? Well, a definite YES and NO! It’s not easy to find out 
what dates in history fall on
what day of the week. We do know that President Lincoln was shot on Friday, April 15, 
1865. (Note: We can’t convert
to seconds with any degree of accuracy either. MC converts April, 14, 1865 to 
1,003,413,469 seconds. HyperCard sez
it’s  -1,221,609,600 seconds.)

HC agrees that 4/15/1865 was a Friday. And the newly revised  Julian/Gregorian/Zeller 
Algorithm (see below) also
concurs! (Small huzzah). Everything works fine until we jump back past December 31, 
1801. Then the dateItems
function in MC smokes a little. Actually, a lot.. The month and day are sort of 
randomly created. And dates beyond
2034 are total bogus.

The seconds and dateitems function are crucial to historian, scientists, engineers and 
writers. MetaCard needs to
address this issue. Perhaps we could agree on a starting point (like 1/01/0001). That 
would be about
63,480,164,515.776001 (plus or minus) second ago....  Heck, that’s less than 2^33 
seconds.

--- (Author Message)
It’s time break out of the 2-bit mentality of the 60’s DOS world. Today’s PCs and Mac 
can handle reeeaaallly big
numbers without belching. How ‘about it, Scott?
--- (end Author Message)

The newly revised Julian/Gregorian/Zeller Algorithm. Watch for line wrap!!
---
function setTheDate pDate,theForm
  -- theForm is either “long” or “short”
  put Date2Julian(pDate) into jDate
  put Julian2Date(jDate,theForm) into jDate

  return jDate
end setTheDate

function Date2Julian pDate
  --ask pDate

  if "/" is in pDate then
    --## short date format
    set the itemDelimiter to "/"
    put number of items in pDate into itemNum

    if itemNum = 3 then
      put last word of last item of pDate into tYear

    else
      answer warning "Error: Date [" & pDate &"] format is incorrect."
      exit to metaCard
    end if

  else
    put last word of last item of pDate into tYear
  end if

  set the ItemDelimiter to ","

  convert pDate to dateItems

  put item 2 of pDate into tMonth

  put item 3 of pDate into tDay

  if tMonth < 3 then
    add 12 to tMonth
    subtract 1 from tYear
  end if

  put trunc(tYear/100) into aa
  put (2-aa+trunc(aa/4)) into bb
  put trunc(365.25*tYear) into cc
  put trunc(30.6001*(tMonth+1)) into dd

  return (bb+cc+dd+tDay+1720995)
end Date2Julian

function Julian2Date pJulianDate,theForm
  local tYear, tDay, tMonth, aa, bb, cc, dd, ee, gg
  local monthList, daysOfTheWeek, XX

  ## -- Your language may vary!
  put "January,February,March,April,May,June,July," into monthList
  put "August,September,October,November,December" after monthList
  put "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday" into daysOfTheWeek

  put trunc((pJulianDate-1867216.25)/36524.25) into aa
  put pJulianDate+1+aa-trunc(aa/4) into bb
  Put bb+1524 into cc
  put trunc((cc-122.1)/365.25) into dd
  put trunc(365.25*dd) into ee
  put trunc((cc-ee)/30.6001) into gg

  --### break into date items
  put cc-ee-trunc(30.6001*gg) into tDay
  if gg<13.5 then put gg-1 into tMonth else put gg-13 into tMonth
  if tMonth>2.5 then put dd-4716 into tYear else put dd-4715 into tYear
  put tYear into cYear

  -- ## calc the day of the week
  -- ## from Zeller's Algorithm

  put cYear div 100 into centuryYear

  put cYear mod 100 into digitYr

  put trunc(2.6 * tMonth - 5.39) into mm1
  put trunc(centuryYear/4) into mm2
  put trunc(digitYr/4) into mm3

  put abs((mm1 + mm2 + mm3 + tDay + digitYr - (2 * centuryYear))) mod 7 into dayNumber
  add 1 to dayNumber -- Sunday = 0!

  put item dayNumber of daysOfTheWeek into weekDay
  put item tMonth of monthList into thisMonth

  if theForm = "long" then
    return weekDay &", " & thisMonth && tDay & ", " & tYear

  else
    return tMonth &"/"& tDay &"/"& tYear
  end if
end Julian2Date
----

--


Ray G. Miller
-----------
Turtlelips Productions
4009 Everett Ave.
Oakland, CA 94602
MailTo:[EMAIL PROTECTED]
(V) 510.530.1971
(F) 510.482.3491



Archives: http://www.mail-archive.com/metacard@lists.runrev.com/
Info: http://www.xworlds.com/metacard/mailinglist.htm
Please send bug reports to <[EMAIL PROTECTED]>, not this list.

Reply via email to