[Seaside-dev] finding out the number of days in a month
James Foster
Smalltalk at JGFoster.net
Thu Feb 14 11:09:54 UTC 2008
On Feb 14, 2008, at 3:50 AM, Paolo Bonzini wrote:
> On Feb 14, 2008, at 1:05 AM, Philippe Marschall wrote:
>
>> Does anybody have a better idea than or problems with:
>> Date class>>#daysInMonthNumber:forYear:
>
> If you don't need it very often, you can do something like
>
> d := Date newDay: 28 monthNumber: m year: y.
> d := d addDays: 4.
> d := d subtractDays: d dayOfMonth.
> ^d dayOfMonth
I believe that the following (which works on GemStone/S) is ANSI
compliant (which should be preferred for portability):
d := DateAndTime year: y month: m day: 28 hour: 0 minute: 0 second: 0.
d := d + (Duration days: 4 hours: 0 minutes: 0 seconds: 0).
d := d - (Duration days: d dayOfMonth hours: 0 minutes: 0 seconds: 0).
^d dayOfMonth
> Finally, the extension method could be #daysInMonth on the instance
> side, to be used like this:
>
> (Date newDay: 1 monthNumber: m year: y) daysInMonth
From an ANSI point-of-view, I agree that an instance method in
DateAndTime is preferable to a DateAndTime factory method. The method
#daysInMonth is a query about the current instance's month something
like #monthName or #isLeapYear.
James Foster
More information about the seaside-dev
mailing list