How to calculate someone's age elegantly? Does Duration work?

18 messages
Open this post in threaded view
|

How to calculate someone's age elegantly? Does Duration work?

 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
Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

 Hmmm to compound my woes ) I notice that Timespan is broken - as the following fails: testEndDateIsPreserved         | startDate endDate |         startDate := Date year: 2000 month: 2 day: 28.    endDate := Date year: 2011 month: 2 day: 27.               self assert: (Timespan starting: startDate ending: endDate) end equals: endDate. > On 21 Jul 2017, at 00:58, Tim Mackinnon <[hidden email]> 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
Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

 Actually that simplification doesn’t work - someone born on 1/Mar/2000 should be 11 on 1/Mar/2011 and this answers 10 (the previous version did work).This does make me think its tricky enough to put this in the core - I may submit it along with the unit tests I borrowed from Stack Overflow (the shame)…Its a good problem though. Thanks for discussing it with me.TimOn 21 Jul 2017, at 10:09, K K Subbu <[hidden email]> wrote:^aDate year - self year -(aDate dayOfYear < self dayOfYear) ifTrue: [ 1 ] ifFalse: [ 0 ])
Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

 On Friday 21 July 2017 03:01 PM, Tim Mackinnon wrote: > Actually that simplification doesn’t work - someone born on 1/Mar/2000 > should be 11 on 1/Mar/2011 and this answers 10 (the previous version did > work). You're right. The simplification does not take into account leap days. Mea culpa. Original code (dropping +1) is readable and simple. Regards .. Subbu > This does make me think its tricky enough to put this in the core - I > may submit it along with the unit tests I borrowed from Stack Overflow > (the shame)… > > Its a good problem though. Thanks for discussing it with me. > > Tim > >> On 21 Jul 2017, at 10:09, K K Subbu <[hidden email] >> > wrote: >> >> ^aDate year - self year - >> (aDate dayOfYear < self dayOfYear) ifTrue: [ 1 ] ifFalse: [ 0 ]) >
Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

 I remember this was discussed before in the mailing list, not in the context of age calculation, but regarding the offset/duration vs calendar based dates handling. Pharo's core date classes are "incremental", which isn't suited for calendar sensitive calculations. Java core class for those calculations is the java.util.Calendar class which is "field" based. See http://forum.world.st/Interesting-Date-Time-Thread-on-Squeak-Dev-tp4778652p4778970.htmlEsteban A. Maringolo 2017-07-21 12:16 GMT-03:00 Tim Mackinnon <[hidden email]>: > I hadn’t heard of that library - looks like one to keep in the back of my mind for more serious date related work. > > I do think that age or ageOn: should be in the standard core particularly when we have so many date and duration related classes - but I’m not sure what the feeling is about adding methods. Certainly the group I saw learning smalltalk were very surprised it wasn’t there particularly when they saw the rich models we do have. > > I am also wondering if the issue with Timespan is concerning - I was surprised that putting a start and end date left me something that didn’t answer my end date presumably down to rounding when it’s converted down to a duration (making me wonder if its better to keep a start and end date and calculate the duration on the fly). >> On 21 Jul 2017, at 13:58, Esteban A. Maringolo <[hidden email]> wrote: >> You might consider a full-fledged framework for it like Chalten: >> https://github.com/ba-st/chalten
Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

 In reply to this post by Tim Mackinnon Tim Mackinnon wrote I am also wondering if the issue with Timespan is concerning - I was surprised that putting a start and end date left me something that didn’t answer my end date presumably down to rounding when it’s converted down to a duration (making me wonder if its better to keep a start and end date and calculate the duration on the fly). Tim The Timespans are half open intervals. (https://en.wikipedia.org/wiki/Interval_(mathematics)) Friday July 21st ends at 11:59:59.99999999999... etc etc etc ...9999PM and the 22nd starts at 12:00AM If Timespans were closed intervals like you expect then we'd be out of whack with our corner of the universe.  That extra clock tick adds complexity.   But I add a method to my image called #displayEnd because I also want them to print 3PM-4PM and not 3PM-3:59:59.999999999PM and use that in my print statements and rendering for Seaside.   displayEnd         ^ start + duration
Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

 In reply to this post by K K Subbu As an aside - there is another way of doing it - but I’m not sure if its clearer (or more efficient): ^ ((aDate year * 10000 + (aDate monthIndex * 100) + aDate dayOfMonth                 - (self year * 10000 + (self monthIndex * 100) + self dayOfMonth)) / 10000) truncated > On 21 Jul 2017, at 12:54, K K Subbu <[hidden email]> wrote: > > On Friday 21 July 2017 03:01 PM, Tim Mackinnon wrote: >> Actually that simplification doesn’t work - someone born on 1/Mar/2000 should be 11 on 1/Mar/2011 and this answers 10 (the previous version did work). > > You're right. The simplification does not take into account leap days. Mea culpa. Original code (dropping +1) is readable and simple. > > Regards .. Subbu > >> This does make me think its tricky enough to put this in the core - I may submit it along with the unit tests I borrowed from Stack Overflow (the shame)… >> Its a good problem though. Thanks for discussing it with me. >> Tim >>> On 21 Jul 2017, at 10:09, K K Subbu <[hidden email] > wrote: >>> >>> ^aDate year - self year - >>> (aDate dayOfYear < self dayOfYear) ifTrue: [ 1 ] ifFalse: [ 0 ]) > >
Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

 In reply to this post by Paul DeBruicker Paul - this is very helpful, gosh you learn a lot from what seemed like simple question. Your suggestion along with Subbu’s gives an easy workable solution - I also wonder if the method should be in the image along with the comment explaining half open intervals like you have. Do you think there is another name for that end method - displayEnd sounds like it actively displays - I wonder if boundedEnd or completeEnd or finiteEnd might be correct alternatives (or is displayEnd a decent convention that everyone understands?) I really have to learn how to submit pull requests (although it seems rather complicated at the moment while the full git integration process is being sorted out). Thanks everyone Tim > On 21 Jul 2017, at 16:40, Paul DeBruicker <[hidden email]> wrote: > > Tim Mackinnon wrote >> I am also wondering if the issue with Timespan is concerning - I was >> surprised that putting a start and end date left me something that didn’t >> answer my end date presumably down to rounding when it’s converted down to >> a duration (making me wonder if its better to keep a start and end date >> and calculate the duration on the fly). >> >> Tim > > > The Timespans are half open intervals. > (https://en.wikipedia.org/wiki/Interval_(mathematics)) > > Friday July 21st ends at 11:59:59.99999999999... etc etc etc ...9999PM and > the 22nd starts at 12:00AM > > > If Timespans were closed intervals like you expect then we'd be out of whack > with our corner of the universe.  That extra clock tick adds complexity.   > > But I add a method to my image called #displayEnd because I also want them > to print 3PM-4PM and not 3PM-3:59:59.999999999PM and use that in my print > statements and rendering for Seaside.   > > displayEnd > ^ start + duration > > > > > > > > > -- > View this message in context: http://forum.world.st/How-to-calculate-someone-s-age-elegantly-Does-Duration-work-tp4955990p4956114.html> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. >
Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

 Using https://github.com/ba-st/Chalten the solution was something like this:Person>>ageOn: aDate  | difference |  difference := (aDate year distanceFrom: self dateOfBirth year).  ^(aDate dayOfMonth < self dateOfBirth dayOfMonth)    ifTrue: [difference - TimeUnits year unitaryMeasurement]    ifFalse: [difference]Test for thattestAge| john |john := Person namedFirst: 'John' last: 'Doe' born: March fourteenth , 1963.self assert: (john ageOn: March thirteenth , 2013) equals: (TimeUnits year with: 49); assert: (john ageOn: March fourteenth , 2013) equals: (TimeUnits year with: 50); assert: (john ageOn: March fifteenth , 2013) equals: (TimeUnits year with: 50) On 21 July 2017 at 14:25, Tim Mackinnon wrote:Paul - this is very helpful, gosh you learn a lot from what seemed like simple question. Your suggestion along with Subbu’s gives an easy workable solution - I also wonder if the method should be in the image along with the comment explaining half open intervals like you have. Do you think there is another name for that end method - displayEnd sounds like it actively displays - I wonder if boundedEnd or completeEnd or finiteEnd might be correct alternatives (or is displayEnd a decent convention that everyone understands?) I really have to learn how to submit pull requests (although it seems rather complicated at the moment while the full git integration process is being sorted out). Thanks everyone Tim > On 21 Jul 2017, at 16:40, Paul DeBruicker <[hidden email]> wrote: > > Tim Mackinnon wrote >> I am also wondering if the issue with Timespan is concerning - I was >> surprised that putting a start and end date left me something that didn’t >> answer my end date presumably down to rounding when it’s converted down to >> a duration (making me wonder if its better to keep a start and end date >> and calculate the duration on the fly). >> >> Tim > > > The Timespans are half open intervals. > (https://en.wikipedia.org/wiki/Interval_(mathematics)) > > Friday July 21st ends at 11:59:59.99999999999... etc etc etc ...9999PM and > the 22nd starts at 12:00AM > > > If Timespans were closed intervals like you expect then we'd be out of whack > with our corner of the universe.  That extra clock tick adds complexity. > > But I add a method to my image called #displayEnd because I also want them > to print 3PM-4PM and not 3PM-3:59:59.999999999PM and use that in my print > statements and rendering for Seaside. > > displayEnd >       ^ start + duration > > > > > > > > > -- > View this message in context: http://forum.world.st/How-to-calculate-someone-s-age-elegantly-Does-Duration-work-tp4955990p4956114.html > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. >
Open this post in threaded view
|

Re: How to calculate someone's age elegantly? Does Duration work?

Open this post in threaded view
|