Version 4 of Number of business days

Updated 2006-01-24 10:54:23

davidw - I'm trying to (efficiently) calculate the arrival date of a package given a start date and number of business days it is supposed to take to arrive. I think this might be of interest to others. Here's my first cut at it (I have some suspicions that I missed something...):

    proc add_days {start days} {
        set 1day [expr {3600 * 24}]

        set end [expr {$start + [days2seconds $days]}]

        set diff [expr {$end - $start}]

        set weeks [expr {$diff / ($1day * 7)}]

        set remainder [expr {($diff - $weeks * $1day * 7) / $1day}]

        set startdow [clock format $start -format "%u"]
        set enddow   [clock format $end -format "%u"]

        set add 0
        incr add [expr {$weeks * 2}]
        incr add [expr {$startdow > $enddow ? 2 : 0}]
        incr add [expr {$startdow == 6 ? 2 : 0}]
        incr add [expr {$startdow == 7 ? 1 : 0}]

        set realend [expr {$end + $add * $1day}]
        set realenddow   [clock format $realend -format "%u"]

        set add [expr {$realenddow == 6 ? 2 : 0}]
        incr add [expr {$realenddow == 7 ? 2 : 0}]
        puts "ordered on [clock format $start] and should take $days days"
        puts "arrival on [clock format [expr {$realend + $add * $1day}]]"
    }

Please feel free to hack at the code in-line rather than tacking on new versions, unless they are radically different approaches - like the simple one of simply starting on the start day and going through each day and either counting it or not depending on whether it is a weekend day.

TODO: holidays, perhaps adjust it to make weekend days configurable (depending on the country, weekend days might vary).

If the Italian postal system gets its hands on the package, double the time, approximately;-)


Category Date and Time