• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Days and hours
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Days and hours


  • Subject: Re: Days and hours
  • From: "Mark J. Reed" <email@hidden>
  • Date: Mon, 17 Oct 2005 10:28:37 -0400

The formula for "number of occurrences of weekday Foo in month Bar" is really straightforward.   I've been trying to actually implement it ever since I first got Bernard's email and I keep tripping over AppleScript quirks, which is why that last message about "weekday as integer" might have come off a little more harshly than was deserved.

Given a month with  a total of D days (presumably between 28 and 31 inclusive, but the formula works for hypothetical calendars where that's not true) whose first day of the week starts on F (F=0 for Sunday, 1 for Monday,...,6 for Saturday), the number of occurrences of weekday W (again, 0=Sunday through 6=Saturday) is given by this formula:


floor(D/7) + floor( (D mod 7 + 1) / ((W - F) mod 7 + 2) )

The above formula requires a modulus function that always returns a result in the range 0...divisor-1, even in the face of negative numbers; -3 mod 7 should yield 4, not -3.  Virtually every programming language on the planet has a "mod" operator that gets this wrong, and AppleScript is no exception - that, at least, I was ready for.  The lack of a floor function I found somewhat surprising, but OK, I can write that, too.

on floor(x)
    if x div 1 is x then return x  
    if (x < 0) then
        return (x - 1) div 1
    else
        return x div 1
    end if
end floor

on modulo(x, y)
    return x - y * (floor(x / y))
end modulo

So the above function, once you have the values to plug into it, is this:

on countWeekdays(D, F, W)
    return floor(D/7) + floor( modulo(D,7) + 1 ) / ( modulo(W - F, 7) + 2) )
end countWeekdays

Getting D is pretty straightforward:

on isLeapYear(aYear)
    if modulo(aYear,    4) > 0 then return false
    if modulo(aYear, 100) > 0 then return true
    if modulo(aYear, 400) > 0 then return false
    return true
end isLeapYear

on daysInMonth(aDate)
    set numDays to item (month of aDate) of {31,28,31,30,31,30,31,31,30,31,30,31}
    if (month of aDate is February) and isLeapYear(year of aDate) then set numDays to numDays + 1
    return numDays
end daysInMonth

Getting F and W was where I got stymied, since I was dumbstruck at the notion that while "January" can be used as a number, "Sunday" can't.  I figured I must have been doing something stupid.  Glad to see that wasn't actually the case, but I'm still dumbstruck. :)

--
Mark J. Reed <email@hidden>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Days and hours
      • From: "Mark J. Reed" <email@hidden>
References: 
 >Re: Days and hours (From: "Nigel Garvey" <email@hidden>)

  • Prev by Date: Re: Mounting and Dismounting local or server volumes on Tiger
  • Next by Date: Re: Days and hours
  • Previous by thread: Re: Days and hours
  • Next by thread: Re: Days and hours
  • Index(es):
    • Date
    • Thread