set tdate [clock format [clock seconds] -format %Y%m%d%H]
set tdate2 [clock format [clock seconds] -format %Y-%m-%d]
set starttime [clock format [clock seconds] -format %H]
set endtime ?
I am trying to figure out how to add 1 hour to the $starttime and call
it my $endtime
Thanks,
JB
You have to consider:
- %H delivers two-digit hour, where 08 and 09 may be mistaken for
octals -> use [scan]
I'd do it like this (you can leave out the [format] step if one-digit
hours are ok):
% set starttime [clock format [clock seconds] -format %H]
16
% set endtime [format %02d [expr {([scan $starttime %d]+1)%24}]]
17
I would suggest that you do something like this:
package require Tcl 8.5
set seconds [clock seconds]
set tdate [clock format $seconds -format %Y%m%d%H]
set tdate2 [clock format $seconds -format %Y-%m-%d]
set starttime [clock format $seconds -format %H]
set endtime [clock format [clock add $seconds 1 hour] -format %H]
Here is how I would do it in Tcl 8.5:
set now [clock seconds]
set tdate [clock format $now -format %Y%m%d%H]
set tdate2 [clock format $now -format %Y-%m-%d]
set starttime [clock format $now -format %H]
set endtime [clock format [clock add $now 1 hour] -format %H]
Here is how I would do it in Tcl prior to 8.5:
set now [clock seconds]
set tdate [clock format $now -format %Y%m%d%H]
set tdate2 [clock format $now -format %Y-%m-%d]
set starttime [clock format $now -format %H]
set endtime [clock format [expr {$now + (60*60)}] -format %H]
--
+------------------------------------------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
Does the OP really want to have the endtime be 00:30 today, if
the start time is 23:30 today?
Surely it's easier to take the underlying seconds-since-epoch
representation of starttime and add 3600 to it?
set now [clock seconds]
set then [expr {$now + 3600}]
set now_display [clock format $now -format <whatever>]
set then_display [clock format $then -format <whatever>]
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services
Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan...@MYCOMPANY.com
http://www.MYCOMPANY.com
The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
>
> On 2 Okt., 15:48, JB <jbju...@gmail.com> wrote:
> > I am using the following code to define the date and time in my Expect
> > script:
> >
> > set tdate [clock format [clock seconds] -format %Y%m%d%H]
> > set tdate2 [clock format [clock seconds] -format %Y-%m-%d]
> > set starttime [clock format [clock seconds] -format %H]
> >
> > set endtime =A0?
> >
> > I am trying to figure out how to add 1 hour to the $starttime and call
> > it my $endtime
>
> You have to consider:
> - %H delivers two-digit hour, where 08 and 09 may be mistaken for
> octals -> use [scan]
> I'd do it like this (you can leave out the [format] step if one-digit
> hours are ok):
>
> % set starttime [clock format [clock seconds] -format %H]
> 16
> % set endtime [format %02d [expr {([scan $starttime %d]+1)%24}]]
> 17
>
You could also do:
set endtime [clock format [expr {(60*60) + [clock seconds]}] -format %H]
This neatly avoids getting bit by possible ill-formed octal numbers. It
also properly handles the rollover from 11pm to midnight as well.
--
Robert Heller -- Get the Deepwoods Software FireFox Toolbar!
Deepwoods Software -- Linux Installation and Administration
http://www.deepsoft.com/ -- Web Hosting, with CGI and Database
hel...@deepsoft.com -- Contract Programming: C/C++, Tcl/Tk
Thanks for all of the great responses. It looks like there are several
ways of attacking it.
The format I am trying to get for the hour is actually like 07:00.
JB
Ok, then just change the format specifier.
For a full list of legal format values, read the clock man/help page.
Others have already answered the question about adding 1 hour.
I'd like to draw the attention here to the three calls of 'clock
second' which in the worst case may give you different times if the
calls happen at a full second step (imagine the first is called at
23:59:59.999999 and the next at 00:00:00.000001).
Better call "clock seconds" only once and reuse the value.
R'
Actually, if start time REALLY is simply [clock seconds] then all the
suggestions miss my favourite and most readable code:
set endtime [clock format [clock scan "1 hour"]]
Clock scan returns the end time from $now if the string is a duration.
I absolutey love this. And it makes time calculations very readable in
tcl. For example, instead of using "magic" numbers like 60*60 I prefer
to do:
expr [clock scan "1 hour"]-[clock seconds]
Or use a proc for even better readability:
proc duration {t} {
expr {[clock scan $t]-[clock seconds]}
}
duration 1hour ;# 3600
duration 10mins ;# 600
the only slightly non-intuitive result is 1day == tomorrow == seconds
to midnight