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.

milliseconds

Help for those learning Tcl or writing their own scripts.
Post Reply
c
cannot_delete
Voice
Posts: 31
Joined: Fri Nov 24, 2006 5:31 am

milliseconds

Post by cannot_delete »

Hi,

I need help with the following problem. As I understand it, this code (being a part of the moxquizz.tcl) is responsible for calculating a duration and prettyprinting it. Now I want to add milliseconds to the equation, but I don't know how.

Code: Select all

## return a duration as a string
proc mx_duration {time} {
    variable dur [duration [expr [unixtime] - $time]]

    regsub -all "seconds" $dur [mc "seconds"] dur
    regsub -all "second" $dur [mc "second"] dur
    regsub -all "minutes" $dur [mc "minutes"] dur
    regsub -all "minute" $dur [mc "minute"] dur
    regsub -all "hours" $dur [mc "hours"] dur
    regsub -all "hour" $dur [mc "hour"] dur
    regsub -all "days" $dur [mc "days"] dur
    regsub -all "day" $dur [mc "day"] dur
    regsub -all "weeks" $dur [mc "weeks"] dur
    regsub -all "week" $dur [mc "week"] dur
    regsub -all "months" $dur [mc "months"] dur
    regsub -all "month" $dur [mc "month"] dur

    return $dur
}
[code]

Thanks for your help,

-typ-
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

The main problem is that "unixtime" does not keep track of fractions of a second. You could always use "clock clicks" if it is available on your platform; keep in mind however, that this will return a system-dependant time-measurement, often the smallest time-period your system may measure (cpu-clock or such). As of tcl8.3 there is an option "-milliseconds" that will guarantee measurements in milliseconds across platforms, which you might be able to utilize. Keep in mind that you might have to handle overflow situations in some cases, as integers in tcl are not large enough to hold the additional information compared to "unixtime". Also, keep in mind that the integer returned by "clock clicks -milliseconds" is a signed integer.

Now, keeping in mind of these issues, it should'nt be too hard to use clock-clicks in your current code. You'd obviously have to convert your millisecond time into "unixtime" time when using the "duration" command. A simple division should solve that, while a modulus would extract the millisecond offset.
NML_375
c
cannot_delete
Voice
Posts: 31
Joined: Fri Nov 24, 2006 5:31 am

Post by cannot_delete »

thx for your help. =D

-typ-
c
cannot_delete
Voice
Posts: 31
Joined: Fri Nov 24, 2006 5:31 am

Post by cannot_delete »

Here's my result:

Code: Select all

## return a duration as a string
proc mx_durationtyp {time} {


    variable dur [duration [expr [clock clicks]] - $time]]

    regsub -all "milliseconds" $dur [mc "milliseconds"] dur
    regsub -all "millisecond" $dur [mc "millisecond"] dur
    
    regsub -all "seconds" $dur [mc "seconds"] dur
    regsub -all "second" $dur [mc "second"] dur
    regsub -all "minutes" $dur [mc "minutes"] dur
    regsub -all "minute" $dur [mc "minute"] dur
    regsub -all "hours" $dur [mc "hours"] dur
    regsub -all "hour" $dur [mc "hour"] dur
    regsub -all "days" $dur [mc "days"] dur
    regsub -all "day" $dur [mc "day"] dur
    regsub -all "weeks" $dur [mc "weeks"] dur
    regsub -all "week" $dur [mc "week"] dur
    regsub -all "months" $dur [mc "months"] dur
    regsub -all "month" $dur [mc "month"] dur

    return $dur
}
I changed the time variable like this (I added a "typ" into my changes. the line above is the original version):

Code: Select all

variable timeasked [unixtime]
variable timeaskedtyp [clock clicks]

Code: Select all

proc moxquiz_pubm {nick host handle channel text} {
    global quizstate banner bannerspace
    global timeasked theq aftergame
    global timeaskedtyp theq aftergame

Code: Select all

	set duration [mx_duration $timeasked]

     
        set durationtyp [mx_durationtyp $timeaskedtyp]

Code: Select all

            mxirc_say $channel [mc "%s solved after %s and now has %s<%d>%s points (+%d) on rank %d." "[banner] [botcolor nick]$nick[botcolor txt]" $durationtyp [botcolor nick] $userarray(score) [botcolor txt] $theq(Score) [mx_get_rank_pos $nick]]
Unfortunally I can only post these sniplets. the whole script would be too long.

I forgot to tell: I didn't get it working.

thx for your help,

-typ-
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

"duration" expects the time to be in seconds, hence you'll need to take apropriate actions to convert the millisecond-based time into second-based. See my previous post on suggestions on how to do this.
NML_375
c
cannot_delete
Voice
Posts: 31
Joined: Fri Nov 24, 2006 5:31 am

Post by cannot_delete »

ok, thx.

I could use some help on how to do that.. ;)

thx for your help,

-typ-
c
cannot_delete
Voice
Posts: 31
Joined: Fri Nov 24, 2006 5:31 am

Post by cannot_delete »

please... =/
Post Reply