TABLE OF CONTENTS


::pwtk::time2unit

SYNOPSIS

proc ::pwtk::time2unit {unit time} {

USAGE

   ::pwtk::time2unit UNIT TIME

PURPOSE

Convert the time to requested unit and return the result as a real number.

ARGUMENTS

Units of time ("*" implies any string, e.g., micro* matches microseconds):

SOURCE

    # in milliseconds:
    set sec   1000
    set min   [expr {60*$sec}]
    set hour  [expr {60*$min}]
    set day   [expr {24*$hour}]
    set week  [expr {7*$day}]
    set month [expr {30*$day}]
    set year  [expr {365*$day}]        
    
    # convert time to ms
    set time [expr double([time2ms -real $time])]

    switch -glob [string trim $unit -] {
        micro*        { return [expr {1000*$time}] }
        ms            { return $time }
        s - sec*      { return [expr {$time/$sec}] }
        m - min*      { return [expr {$time/$min}] }
        h - H - hour* { return [expr {$time/$hour}] }
        d - D - day*  { return [expr {$time/$day}] }
        w - W - week* { return [expr {$time/$week}] }
        M - mon*      { return [expr {$time/$month}] }
        y - Y - year* { return [expr {$time/$year}] }
        default {
            ::pwtk::error "wrount unit of time; must be one of micro, ms, s, h, h, d, w, M, or y, but got $unit" 1
        }
    }
}