Dates, Times, Durations

Michael Latta lattam at mac.com
Thu Jan 20 20:03:13 UTC 2005


If you wanted to be more general (not count weekends, holidays, etc.) I 
would create a Schedule class that knows when work is happening.  This 
schedule would then do the computation of work hours in a period.

Rather than testing each hour of the period, break the time into 3 
portions.  Hours that begin the period that are a partial work day, 
those that are full work days, and those that end the period in a work 
day.  Do the below for the first and last parts of the period, and do 
something more efficient for the full work days.

Michael


On Jan 20, 2005, at 11:43 AM, Jason Rogers wrote:

> On Thursday 20 January 2005 13:13, Jason Rogers wrote:
>> I would like to calculate the number of work hours between two dates 
>> with
>> the basis of a workday being 9:00 to 17:00.  Before reinventing the 
>> wheel,
>> I wanted to check to see if anyone has already done this before and if
>> there is (by some great chance) support for it a base 3.8 image.
>
> This is what I came up with.  It seems to work with my tests, but it's 
> not the
> most elegant.  Any suggestions?
>
> asNumberOfWorkDays
>     | hours |
>     hours := 0.
>     start
>         to: end
>         by: (Duration hours: 1.0)
>         do: [:hour |
>             (hour asDuration hours > 8 and: [hour asDuration hours < 
> 17])
>                 ifTrue: [hours := hours + 1.]
>         ].
>     ^ hours / 8
>
> -- 
> Jason Rogers
>
> "I am crucified with Christ: nevertheless I live; yet not I,
> but Christ liveth in me: and the life which I now live in
> the flesh I live by the faith of the Son of God, who loved
> me, and gave himself for me."
>     Galatians 2:20
>




More information about the Squeak-dev mailing list