suj
suj

Reputation: 529

TCL Clock command

The below shown clock manipulation works well in Linux machine which has TCL 8.5. But when I use the same on the SunOS which has the TCL8.4 , I am getting the error "bad switch "-format": must be -base or -gmt".

For some reason, I am not able to upgrade my TCL 8.4 to 8.5 in SunOS.

How can I make it work in TCL 8.4 as well?

The commands are given below and what I am trying to achieve through these commands is to advance the system date by one more day.

$today contains the value "2012 06 15 14 39 20"

set today [clock scan $today -format "%Y %m %d %H %M %S"]      
set tomorrow [clock add $today 86600 seconds]
set victim [clock format $tomorrow -format "%a"]
set tomorrow [clock format $tomorrow -format "%m%d%H%M"]
send "sudo date $tomorrow\r"

Upvotes: 1

Views: 7291

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137807

Jackson's answer is the core of what the issue is: 8.5 added a lot of features to clock and your code is dependent on them. However, he doesn't quite identify the best method for getting the target time.

# Multiple assignment from list
foreach {YY MM DD HH mm ss} $today break
# Get the date tomorrow in one funky step
set tomorrow [clock scan "$YY$MM$DD $HH$mm$ss + 86600 seconds"]

# Rest is the same as before
set victim [clock format $tomorrow -format "%a"]
set tomorrow [clock format $tomorrow -format "%m%d%H%M"]
send "sudo date $tomorrow\r"

(You are aware that a day is never 86600 seconds long? That's 200 seconds longer than the average length…)

Upvotes: 5

Jackson
Jackson

Reputation: 5657

As you have found out the clock command changed between Tcl 8.4 and 8.5. In 8.4 the clock scan command only recognized a number of standard formats. So you need to convert your $today value into one of these formats, see here for details.

One possible way is

regexp {(\d\d\d\d) (\d\d) (\d\d) (\d\d) (\d\d) (\d\d)} $today all YY MM DD HH mm ss
set reformatToday "$YY$MM$DD $HH$mm$ss"
set today [clock scan $reformatToday]

Tcl 8.5 will also work with this free form scan code; however this feature is deprecated in Tcl versions later than 8.4.

Upvotes: 2

Related Questions