About Year not knowing exact daysInMonth

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

About Year not knowing exact daysInMonth

stepharo
Hi

I started to write a little utility to generate bills.

And I need to know the number of days in a month and I was surprised that

(Month month: 2) daysInMonth returns 28 because we do not know :)

So I thought that I was obviously wrong and I should use

(Year year: 2016) daysInMonth

     > "#(31 28 31 30 31 30 31 31 30 31 30 31)"

:(

When I look at the implementation this is clear that it is wrong

daysInMonths
     ^ Date daysInMonth

totally not aware of the year.


I think that daysInMonths should use DateAndTime creating a dummy date
for each month and using asMonth daysInMonth

(DateAndTime
     year: 2016 month: 2 day: 3)
     asMonth
     daysInMonth

     > 29

Year >> daysInMonth: aMonthNumber
     "Returns the days in month for a given month"

     <expr: #(#(#Year #year: 2016) #daysInMonth: 2) result: 29>
     ^ (DateAndTime year: self year month: aMonthNumber) asMonth daysInMonth


I extended the class with some more practical methods. What do you
think? I should push them to pharo?

I was surprised that the library was not that good.

Stef


Reply | Threaded
Open this post in threaded view
|

Re: About Year not knowing exact daysInMonth

Paul DeBruicker
You could use

Month class>>#daysInMonth:forYear:


Which I think does what you want already.


stepharo wrote
Hi

I started to write a little utility to generate bills.

And I need to know the number of days in a month and I was surprised that

(Month month: 2) daysInMonth returns 28 because we do not know :)

So I thought that I was obviously wrong and I should use

(Year year: 2016) daysInMonth

     > "#(31 28 31 30 31 30 31 31 30 31 30 31)"

:(

When I look at the implementation this is clear that it is wrong

daysInMonths
     ^ Date daysInMonth

totally not aware of the year.


I think that daysInMonths should use DateAndTime creating a dummy date
for each month and using asMonth daysInMonth

(DateAndTime
     year: 2016 month: 2 day: 3)
     asMonth
     daysInMonth

     > 29



Year >> daysInMonth: aMonthNumber
     "Returns the days in month for a given month"

     <expr: #(#(#Year #year: 2016) #daysInMonth: 2) result: 29>
     ^ (DateAndTime year: self year month: aMonthNumber) asMonth daysInMonth


I extended the class with some more practical methods. What do you
think? I should push them to pharo?

I was surprised that the library was not that good.

Stef