Re: calculate time span
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: calculate time span

From: Lisa Pearlson <no@spam.plz>
Date: Mon Feb 27 2006 - 21:20:23 CET

Uhmm, retval is an unused variable.. I forgot to remove it.
Are there any improvements to be made?

Who is the core maintainer of the clock command?

Lisa

"Lisa Pearlson" <no@spam.plz> wrote in message
news:44035e58$0$8238$e4fe514c@dreader30.news.xs4all.nl...
> This is what I needed, this is what I implemented, .. if I missed
> something, please let me know:
>
> proc wholeunits {val} {
> return [expr {[format {%.0f} [expr { ($val < 0) ? ceil($val) :
> floor($val) }]]}]
> }
>
> proc timespan {{option} {from} {to {}}} {
> if {$to eq {}} {
> set t1 [clock scan now]
> set t2 [clock scan $from]
> } else {
> set t1 [clock scan $from]
> set t2 [clock scan $to]
> }
>
> set diffseconds [expr {$t2 - $t1}]
>
> set retval {}
> switch -- $option {
> years {
> return [expr {[clock format $t2 -format {%Y}] -
> [clock format $t1 -format {%Y}]}]
> }
> months {
> set years [expr {[clock format $t2 -format {%Y}] -
> [clock format $t1 -format {%Y}]}]
> set months [expr {[clock format $t2 -format {%m}] -
> [clock format $t1 -format {%m}]}]
> return [expr { ($years * 12) + $months}]
> }
> weeks {
> return [wholeunits [expr {$diffseconds /
> 604800.0}]]
> }
> days {
> return [wholeunits [expr {$diffseconds / 86400.0}]]
> }
> hours {
> return [wholeunits [expr {$diffseconds / 3600.0}]]
> }
> minutes {
> return [wholeunits [expr {$diffseconds / 60.0}]]
> }
> seconds {
> return $diffseconds
> }
> }
>
> return -code error -errorinfo "unknown option $option" {}
> }
>
> Lisa
>
> "Lisa Pearlson" <no@spam.plz> wrote in message
> news:4401eac5$0$22451$e4fe514c@dreader16.news.xs4all.nl...
>> Getting number of months would be 'whole months', days would be 'whole
>> days', etc.
>>
>> That means (assuming you have a function 'timespan' like I'd want):
>>
>> timespan days {2006-01-01 12:00:00} {2006-02-01 12:00:00}
>> result: 31
>>
>> timespan days {2006-02-01 12:00:00} {2006-03-01 12:00:00}
>> result: 28 (but on a leap year it would be 29)
>>
>> set t [timespan expr { {2006-02-01 12:00:00} + {28 days} + {5 seconds} }]
>> clock format $t -format "%Y:%m:%d %H:%M%S"
>> result: 2006-03-01 12:00:05
>>
>> set t [timespan expr { {2006-03-01 12:00:00} + {28 days} + {5 seconds} }]
>> clock format $t -format "%Y:%m:%d %H:%M%S"
>> result: 2006-03-28 12:00:05
>>
>> Lisa
>>
>> "Roy Terry" <royterry@earthlink.net> wrote in message
>> news:UH_Lf.734$6I.636@newsread3.news.pas.earthlink.net...
>>> "Lisa Pearlson" <no@spam.plz> wrote in message
>>> news:43ff5fe9$0$8260$e4fe514c@dreader30.news.xs4all.nl...
>>>> Hi,
>>>>
>>>> I have 2 dates, formatted like "yyyy-mm-dd hh:mm:ss".
>>>>
>>>> I wish to know the difference between the two, in seconds, hours, days,
>>>> months and years.
>>>>
>>>> I looked at clock function.. I can use [clock scan $dt] on these days
>>>> and
>>>> get platform specific seconds or something.
>>>
>>> Actually it will give you a universal time coordinate.
>>>
>>> I can subtract these dates from
>>>> one another (larger from the smaller), and format this into a date. But
>>> this
>>>> will give me the time offset from epoch.
>>> I don't think so. If you subtract the two values you will get nothing
>>> but a pure count of seconds.
>>>
>>> And as such it doesn't take into
>>>> account the actual dates of which I am wanting to know the difference
>>> off..
>>> Not correct. It is pure seconds that give you the exact difference.
>>>
>>>> This is important if I want to know the difference in these two dates
>>>> in
>>>> number of months, since not all months are created equal.
>>>
>>> How would you define a difference in months? Would
>>> it only include whole months?
>>>>
>>>> There ought to be some type of timespan function:
>>>>
>>>> set td1 {2006-01-01 12:00:00}
>>>> set td2 {2006-02-24 20:00:00}
>>>>
>>>> set months [timespan months $t1 $t2]
>>>> set seconds [timespan seconds $t1 $t2]
>>>>
>>>> Can something like this be accomplished in TCL?
>>> I think so depending on your
>>> defiinition month differences.
>>>
>>>
>>>
>>>>
>>>> Lisa
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Received on Sun Apr 30 02:17:07 2006