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.

[Help] Calculate time [Solved]

Help for those learning Tcl or writing their own scripts.
Post Reply
w
w00f
Halfop
Posts: 49
Joined: Wed Oct 04, 2006 6:50 pm

[Help] Calculate time [Solved]

Post by w00f »

hi there.

i need to know if there are any kind of way to calculate the time since i've added something that i'm searching for and the time when i trigger a certain command.

[23:54:41] (@w00f) !adefine w00f test!
[23:54:43] (@w00f) !define w00f
[23:54:44] (@b0T) w00f: test! :: added at 10/04/06 23:54:41

what i want is to convert that time (10/04/06 23:54:41 , i save the time when i add the definition) to something like this:

[23:54:44] (@b0T) w00f: test! :: added 3s ago.

a month later

[23:54:44] (@b0T) w00f: test! :: added 1m 3s ago.
or
[23:54:44] (@b0T) w00f: test! :: added 4w 3s ago.
or 4w 0d 0h 0m 3s ago

something like that would be great.


tnks in advance.
Last edited by w00f on Sat Oct 21, 2006 5:08 pm, edited 1 time in total.
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Convert the time to EPOCH time, with clock scan:

set mytime [clock scan "10/04/06 23:54:41"]
set nowtime [clock scan [strftime "%m/%d/%y %T"]]

then use the 'duration' command to diff the times:

set difftime [duration [expr $nowtime - $mytime]]

Hmm now that I look at my date.tcl script, you can simplify the difftime by just using [unixtime] to get the current time:

set difftime [duration [expr [unixtime] - $mytime]]

The date.tcl script I (re)did has a whole lot of examples of manipulating dates/times and checking for leap years and valid input, reading/writing to files, and so forth and so on. It's in the tcl archive if you want to look at it.
w
w00f
Halfop
Posts: 49
Joined: Wed Oct 04, 2006 6:50 pm

Post by w00f »

Thanks man , works great.

i'll take a look at date.tcl , could be usefull ;D
m
metroid
Owner
Posts: 771
Joined: Wed Jun 16, 2004 2:46 am

Post by metroid »

Sorry but why would you use clock scan on the current time if [clock seconds] or [unixtime] is available?

That just seems like you're making TCL do something that it doesn't have to.
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

rosc2112 wrote: Hmm now that I look at my date.tcl script, you can simplify the difftime by just using [unixtime] to get the current time:

set difftime [duration [expr [unixtime] - $mytime]]
w
w00f
Halfop
Posts: 49
Joined: Wed Oct 04, 2006 6:50 pm

Post by w00f »

I've started to play with dates and stuff like that but in one the codes i've got a problem...i saw date.tcl but i was a little confused about how i can convert a date(10/01/06 09:20 for example[month/day/year Hours:minutes) to "clock second" format.

i've tried some codes but none of them seems to work as i want.
need help =X
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

clock seconds
Return the current date and time as a system-dependent integer value. The unit of the value is seconds, allowing it to be used for relative time calculations. The value is usually defined as total elapsed time from an ``epoch''. You shouldn't assume the value of the epoch.

So the [clock seconds] command will produce the current EPOCH time:
set now [clock seconds]

If you're wanting to convert a specific date and time into the EPOCH time, you use:
set mytime [clock scan "10/04/06 23:54:41"]

check the clock manpage for the formats for the date, the command is particular about the format (eg, month day,<comma required> year <time format>)

Date:
The acceptable formats are
mm/dd/yy (10/08/06)
monthname dd, yy (october 08, 06 or october 08, 2006)
dd monthname yy (08 october 06 or 08 october 2006)
day, dd monthname yy (sun, 08 october 06)
CCyymmdd (20061008 or 061008 )
CCyy-mm-dd (2006-10-08 or 06-10-08 )
dd-monthname-CCyy (08-october-2006 or 08-october-06)

The default year is the current year. If the year is less than 100, we
treat the years 00-68 as 2000-2068 and the years 69-99 as 1969-1999. Not all platforms can represent the years 38-70, so an error may result if these years are used.

Time:
A time of day, which is of the form:
hh:mm:ss
meridian zone
hhmm meridian zone
If no meridian is specified, hh is interpreted on a 24-hour clock.

Also note that the date/time have to be within double-quotes " "
w
w00f
Halfop
Posts: 49
Joined: Wed Oct 04, 2006 6:50 pm

Post by w00f »

tnks man (one more time)
I have everything to work now.

u've been a great help ;D
Post Reply