Make numberFormat even better AND Cognitive Load

Bob Sneidar bobsneidar at iotecdigital.com
Thu Apr 27 18:42:51 EDT 2017


Yup. I do that a lot. Where I have an issue is dividing up a block of time into multiple segments, where the minutes may not divide evenly between all the segments. So I produced this:

function timeTable pStartTime, pStartLunch, pEndLunch, pEndTime, pItemCount
   set the itemDelimiter to ":"
   
   -- convert all times to minutes
   put item 2 of pStartTime + (item 1 of pStartTime *60) into tStartTimeMinutes
   put item 2 of pStartLunch + (item 1 of pStartLunch *60) into tStartLunchMinutes
   put item 2 of pEndLunch + (item 1 of pEndLunch *60) into tEndLunchMinutes
   put item 2 of pEndTime + (item 1 of pEndTime *60) into tEndTimeMinutes
   put tEndLunchMinutes - tStartLunchMinutes into tLunchMinutes
   put tEndTimeMinutes - tStartTimeMinutes - tLunchMinutes into tTotalMinutes
   
   -- calculate the minutes per item with remainder
   put tTotalMinutes div pItemCount into tItemMinutes
   put tTotalMinutes mod pItemCount into tModMinutes
   
   -- if there was no lunch times provided, make some up
   if pStartLunch is empty then
      put tStartTimeMinutes + tItemMinutes into tStartLunchMinutes
      put tStartLunchMinutes into tEndLunchMinutes
   end if
   
   repeat with counter = 1 to pItemCount
      put tStartTimeMinutes into aTimeTable [counter] [1]
      put tStartTimeMinutes + tItemMinutes into tNextTimeMinutes
      
      -- distribute spare minutes
      if tModMinutes >0 then
         add 1 to tNextTimeMinutes
         subtract 1 from tModMinutes
      end if
      
      if tLunchMinutes > 0 then
         if tNextTimeMinutes >= tStartLunchMinutes then
            put tStartLunchMinutes into aTimeTable [counter] [2]
            put tEndLunchMinutes into aTimeTable [counter] [3]
            add tLunchMinutes to tNextTimeMinutes
            put 0 into tLunchMinutes
         else
            put 0 into aTimeTable [counter] [2]
            put 0 into aTimeTable [counter] [3]
         end if
      else
         put 0 into aTimeTable [counter] [2]
         put 0 into aTimeTable [counter] [3]
      end if
      
      put tNextTimeMinutes into aTimeTable [counter] [4]
      
      -- calculate total minutes for this record
      put (aTimeTable [counter] [4] - aTimeTable [counter] [1]) - \
            (aTimeTable [counter] [3] - aTimeTable [counter] [2]) \
            into aTimeTable [counter] [5]
      put tNextTimeMinutes into tStartTimeMinutes
   end repeat
   
   -- convert minutes back to seconds
   put the keys of aTimeTable into tTableKeys
   sort lines of tTableKeys numeric ascending
   
   repeat for each line tKey in tTableKeys
      put aTimeTable [tKey] into aTimeRecord
      put the keys of aTimeRecord into aRecordKeys
      repeat for each line tRecordKey in aRecordKeys
         put aTimeRecord [tRecordKey] into tTime
         if tTime = 0 then
            put empty into aTimeTable [tKey] [tRecordKey]
         else
            put tTime div 60 & ":" & tTime mod 60 into tTime
            put formatTime(tTime, "sql time") into tTime
            put char 1 to 5 of tTime into aTimeTable [tKey] [tRecordKey]
         end if
      end repeat
   end repeat
   
   return aTimeTable
end timeTable

function formatTime theTime, theFormat
   /*
   accepts any valid time and returns the form of the time specified in the second parameter.
   The valid formats are:
   sql time: hh:mm:ss (Note: combining sql date from the formatDate() function with the
   sql time will produce a valid SQL date time type). 
   short time: LC short time format
   abbreviated time: LC abbr time format (same as short time)
   long time: LC long time format
   seconds: the number of seconds since the prior midnight
   military: the military time 00:00 - 23:59
   */
   if theTime is empty then return empty
   
   set the numberformat to "00"
   switch theFormat
      case "sql time"
         convert theTime to dateitems
         put (item 4 of theTime +0) & ":" & \
               (item 5 of theTime +0) & ":" & \
               (item 6 of theTime +0) into theTime
         break
      case "short time"
         convert theTime to short time
         break
      case "abbreviated time"
         convert theTime to abbreviated time
         break
      case "long time"
         convert theTime to long time
         break
      case "seconds"
         convert theTime to seconds
         break
      case "military"
         set the itemdelimiter to ":"
         
         if theTime contains "PM" then
            add 12 to item 1 of theTime
         end if
         
         put word 1 of item 2 of theTime into item 2 of theTime
         break
   end switch
   
   return theTime
end formatTime


> On Apr 27, 2017, at 13:27 , Mike Kerner via use-livecode <use-livecode at lists.runrev.com> wrote:
> 
> On the topic of keeping LC stupid-simple, here's of one of those ways that
> LC makes ugly math fun:
> #<compute last date of previous month>
> 
> put the short date into theDate
> convert theDate to dateItems
> put 0 into item 3 of theDate
> convert theDate to short date
> put theDate





More information about the use-livecode mailing list