Tim,

You might consider a full-fledged framework for it like Chalten:
https://github.com/ba-st/chalten

I don't know how it handles timezones, because the age calculation
might get trickier if you consider also the time of birth and you're
querying it from a different timezone ;-)

Regards!

Esteban A. Maringolo


2017-07-21 6:09 GMT-03:00 K K Subbu <kksubbu...@gmail.com>:
> I suppose no one needed it that badly ;-). Age is the difference in years
> except when the given date's julian is less than self's julian when it is
> less by 1, so the code can be simplified to:
>
> ^aDate year - self year -
>  (aDate dayOfYear < self dayOfYear) ifTrue: [ 1 ] ifFalse: [ 0 ])
>
> BTW, the code has an off-by-one error. +1 should be dropped in first age
> assignment.
>
> Regards .. Subbu
>
>
> On Friday 21 July 2017 12:55 PM, Tim Mackinnon wrote:
>>
>> Yes that's a good solution, however I'm really surprised that with the
>> plethora of dates, durations, timespans (we are well served with a very rich
>> time and date domain) that this isn't already in the image?
>>
>> It seems very surprising. In fact the group I was working with (other
>> language programmers) kept looking and looking for something as they felt
>> certain they had overlooked it given all those promising classes.
>>
>> I'm curious why it's not in there? And would we also expect to find its
>> equivalent on a timespan (maybe delegating back to this date method? This
>> assumes that timespan can be fixed as it loses precision unexpectedly).
>>
>> Thanks for the reply.
>>
>> Tim
>>
>>
>>
>> Sent from my iPhone
>>
>>> On 21 Jul 2017, at 06:41, K K Subbu <kksubbu...@gmail.com> wrote:
>>>
>>> Date>>ageOn: aDate
>>> " assert: (self <= aDate)
>>> | age |
>>>
>>> age = aDate year - self year + 1.
>>> aDate monthIndex < self monthIndex ifTrue: [ ^age - 1 ].
>>> (aDate monthIndex = self monthIndex and: [aDate dayOfMonth < self
>>> dayOfMonth]) ifTrue: [ ^age - 1 ].
>>> ^age
>>>
>>> HTH .. Subbu
>>>
>>>> On Friday 21 July 2017 05:28 AM, Tim Mackinnon wrote:
>>>> Hi - I just ran a great MobProgramming session with Smalltalk for the
>>>> XProLo group - and there was lots of great feedback and questions, however
>>>> one of them really got me thinking…
>>>> We did a little exercise to create a Person class with name, dob - and
>>>> then we TDD’d an age method… which seems simple on the surface but it gets
>>>> to an interesting point - what about leap years? How old is someone on 
>>>> those
>>>> years?
>>>> I thought that our plethora of Date/Duration classes might handle this -
>>>> but I couldn’t spot something obvious and was wondering if someone had a
>>>> neat answer.
>>>> Essentially if you try:
>>>> age
>>>> ^Date today - self dob
>>>> You get a Duration,
>>>> But there isn’t :
>>>> ^(Date today - self dob) asYears
>>>> There is is asDay
>>>> ^(Date today - self dob) asDays
>>>> But then can you really
>>>> ^(Date today - self dob) asDays / 365) truncated
>>>> But what about leap years… so
>>>> ^(Date today - self dob) asDays / 365.25) truncated
>>>> It all feels a bit inelegant and I suspect there is a better Smalltalk
>>>> way that is eluding me? Any suggestions?
>>>> Tim
>>>
>>>
>>>
>>
>>
>
>

Reply via email to