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.

Date [censored].

Old posts that have not been replied to for several years.
Locked
Z
Zygomaticum

Post by Zygomaticum »

Hi all.

I'm working on a tcl script which should clear all lines older as 5 days from a file.
The lines ar timestamped in format: DD/MM/YY hh:mm:ss <line>

i got this now:

Code: Select all

  foreach line [split $file "n"] {
    if { $line != "n" || $line != " n" || $line != " " } {
      set fdate [lindex [split $line " "] 0]
      set fday [lindex [split $fdate "/"] 0]
      set fmonth [lindex [split $fdate "/"] 1]
      set fyear [lindex [split $fdate "/"] 2]
      set day3 [checkdate $fday $fmonth $fyear $day $month $year]
      putserv "PRIVMSG $chn :$day3 test"
      if { $day3 == 1 } { putserv "PRIVMSG $chn : 1" }
      if { $day3 != 1 } { puts $file1 $line }
    }
  }
the function checkdate checks if linedate ($fday, $fmonth and $fyear, splitted earlier (checked them, they work correctly)) is 5 ore more days older as the current date. If so, it returns 1, if not, it returns 0.
It works with if's and switches (all with " before and after the values, the var's are strings).
This won't work (correctly).
I also created a proc toint, this converts a string to an int (it's a switch that returns 01 if string is "01", 02 if string is "02" and so on), and checked with simple if's (if { [toint $fday] < [expr [toint $day] +5] } { ...} and sort of xtra checks fore month and year), but it won't work.

Can someone help me plz?? :smile:

I know it's kinda hard, but i dunno why it won't work.

Greejtz, Zygo.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

First of all, have you considered using a regular expression instead of all those splits?

set x [regexp {^([0-9]+)/([0-9]+)/([0-9]+)} $line total day month year]

Then if x is 0, it didn't match; otherwise, it did, and the parts are already in $day, $month, and $year. $total is the entire thing that matches (i.e. 03/04/90).

Then, you can use:
set day [string trimleft $day 0]
etc

to get rid of leading 0's (this may be screwing up your math, since Tcl will treat the numbers as octal instead of decimal).

Also... are you aware of the "clock scan" command? You can give it a date, and it will return a number (seconds from jan 1 1970 to that date).

% clock scan "03/02/90"
636354000

% clock scan "03/03/90"
636440400

The difference in each day is 86400 seconds. So to check if it's 5 days or whatever, see if they're within 5*86400 seconds of each other.

Oh, btw, the "clock scan" date format is "mm/dd/yy(yy)".
Locked