newbie
newbie

Reputation: 1033

tcl clock function manipulation

I am in a bit of dilemma here handling the tcl clock function

Here is my code:

set old_date 0

if{ "[clock format [clock scan $old_date] -format {%d %b}] != "[clock format [clock scan $event_date] -format {%d %b}]}
{
    if{$old_date !=0}
    {   
        set myTest($value) $old_date;
        #some other stuff
    }
}
set old_date $event_date

It works(should) in most cases. But my problem is [clock format [clock scan $old_date] -format {%d %b}] returns today's date if $old_date=0.

I know it's probably returning the right value but I do not want it return today's date if the value is zero. It kind of messes my comparison logic. I can probably check for an if condition but Is there something i can do with the clock function?

Upvotes: 0

Views: 563

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137807

The problem is that you're doing (in effect) [clock scan 0]. The clock scan command will, if all else fails, fall back to trying to parse whatever you throw into it. With a 0, it ends up as deciding it refers to 00:00:00 on the current date. I don't know if this is a correct parse of that string, but it's arguably not wrong: it's a truly horribly denormalized time.

What would make sense is keeping a timestamp in old_date; that's the kind of value that clock scan returns. Since you're looking for a date-level granularity, let's pick a standard time of the day to represent that day (midday, GMT). Now, we can use the -base and -gmt options to make everything work in our conversion code (put into a little procedure for convenience):

proc dateStamp {date} {
    clock scan "12:00:00" -base [clock scan $date -gmt 1] -gmt 1
}

Given that, we can now rewrite the rest of your code:

set old_date 0

if {$old_date != [dateStamp $event_date] && $old_date != 0} {
    set myTest($value) [clock format $old_date -format "%d %b"]
    #some other stuff
}
set old_date [dateStamp $event_date]

Upvotes: 4

Related Questions