This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

duraiton help [SOLVED]

Help for those learning Tcl or writing their own scripts.
Post Reply
S
Slightz
Voice
Posts: 3
Joined: Tue May 16, 2006 4:34 am

duraiton help [SOLVED]

Post by Slightz »

Hello, yes i know this has been asked and i searched for "duration" but can't fiure out im a newbie to TCL and is a hard language i did not code tthsi script is just part of it but what im tryign to do is instead of showing

Code: Select all

05/23/2006 7:45:PM
i wan't somethign like this

Code: Select all

2 weeks 1 day 3 hours 4 mins 30 secs 05/23
something liek this i been looking at this pice of the code but cant get it

Code: Select all

proc check:stamp { timestamp } {
 regsub -- {[nN][oO][wW]} $timestamp "" timestamp
 if { $timestamp == "" || $timestamp == "\-" } { return [unixtime] } else { return $timestamp}
}


proc unx2nps { timestamp } {
 if {[regexp "\[^\\d\]" $timestamp]} { error "unx2nps: parameter is not a valid unix timestamp" }
 if {$timestamp > "2000000000"} { error "unx2nps: parameter is too large" }
 return [clock format $timestamp -format "%m/%d/%Y %l:%M:%p"]
}
i was tryign to set something like

set blah [duration $unx2nps]

but then the script does nto output the info

any help would be thanksfull :)

Thank You
Last edited by Slightz on Mon May 29, 2006 4:38 am, edited 1 time in total.
User avatar
Alchera
Revered One
Posts: 3344
Joined: Mon Aug 11, 2003 12:42 pm
Location: Ballarat Victoria, Australia
Contact:

Post by Alchera »

From tcl-commands.doc:
duration <seconds>
Returns: the number of seconds converted into years, weeks, days, hours,
minutes, and seconds. 804600 seconds is turned into 1 week 2 days
7 hours 30 minutes.
You may also find the clock manual page of use.
Add [SOLVED] to the thread title if your issue has been.
Search | FAQ | RTM
S
Slightz
Voice
Posts: 3
Joined: Tue May 16, 2006 4:34 am

Post by Slightz »

i haved... read and just got to change the format from 24 horus to PM AM but i dont get it how can i tweak that pice of code or how can i do it

can you help me out
n
noname
Voice
Posts: 5
Joined: Sat May 27, 2006 4:28 pm

Post by noname »

Code: Select all

set prestamp_ago [duration [expr [unixtime] - $prestamp]]; # e.g. 4 years 37 weeks 2 days 18 hours 48 minutes 58 seconds
set prestamp_day [clock format $prestamp -format "%m/%d"]; # e.g. 05/27


but duration isnt the best method imho... noone wants to calc 1y 33weeks too days... so i here is my code, which i'm usin...

Code: Select all

### dupecheck
[...]
    set nothetime [unixtime]
    set tim_ago [no:dupe:ago $nothetime $tim]; #e.g. 20d 7h 13m 16s
    set tim_day [no:dupe:day $tim]; # e.g. 05/07
[...]


### dupe tim ago
proc no:dupe:ago { unixtime prestamp } {
 if {[regexp "\[^\\d\]" $unixtime] != "1" && [regexp "\[^\\d\]" $prestamp] != "1" && $unixtime < "2000000000" && $prestamp < "2000000000" && $unixtime != "0" && $prestamp != "0"} { 
  set timdiff [expr $unixtime - $prestamp] 
  set d [expr {$timdiff/86400}]
  set h [expr {($timdiff%86400)/3600}]
  set m [expr {(($timdiff%86400)%3600)/60}]
  set s [expr {(($timdiff%86400)%3600)%60}]
  append ds $d "d"
  append hs $h "h"
  append ms $m "m"
  append ss $s "s"
  if { $d > 0 } {
   append dupeago $ds " " $hs " " $ms " " $ss
  } elseif { $h > 0 } {
   append dupeago $hs " " $ms " " $ss
  } elseif { $m > 0 } {
   append dupeago $ms " " $ss
  } else {
   append dupeago $ss
  }
  return $dupeago
 }
}



### dupe day ago
proc no:dupe:day { prestamp } {
 if {[regexp "\[^\\d\]" $prestamp] != "1" && $prestamp < "2000000000" && $prestamp != "0" && $prestamp != "0"} { 
  return  [clock format $prestamp -format "%m/%d"]
 }
}


btw: a new version will b published in a few days -.^
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

If you dont want duration to show more than 2 units, you could cut off the rest by using:
lrange [duration $d] 0 3
this should cut '1 week 2 days 7 hours 30 minutes' to '1 week 2 days'. No need to write an own proc about it :D.

PS: why use a regexp "\[^\\d\]" if you can use a non regexp expression like (though this matchs rather {^\s*\d+\s*$}):
![string is integer -strict $timestamp]
Differences: your regexp continues if string is zero. 'string is' is valid when the value is trailed or lead by whitespace characters and therefore more accurate for TCL to determine, if a following integer operation will succeed.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
n
noname
Voice
Posts: 5
Joined: Sat May 27, 2006 4:28 pm

Post by noname »

De Kus wrote:If you dont want duration to show more than 2 units, you could cut off the rest by using:
lrange [duration $d] 0 3
this should cut '1 week 2 days 7 hours 30 minutes' to '1 week 2 days'. No need to write an own proc about it :D.
i just wanted to have days - no years or weeks
it should cut '1 week 2 days 7 hours 30 minutes' to '9 days 7 hours 30 minutes' ;)

De Kus wrote: PS: why use a regexp "\[^\\d\]" if you can use a non regexp expression like (though this matchs rather {^\s*\d+\s*$}):
![string is integer -strict $timestamp]
Differences: your regexp continues if string is zero. 'string is' is valid when the value is trailed or lead by whitespace characters and therefore more accurate for TCL to determine, if a following integer operation will succeed.
tnx a lot - mayb 'string is' was a bit to easy -.^
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

try clock scan
XplaiN but think of me as stupid
n
noname
Voice
Posts: 5
Joined: Sat May 27, 2006 4:28 pm

Post by noname »

Ofloo wrote:try clock scan
try sentence
S
Slightz
Voice
Posts: 3
Joined: Tue May 16, 2006 4:34 am

Post by Slightz »

thanks noname that works perfectly does that i needed
Post Reply