Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Incrementing the Hour

8 views
Skip to first unread message

JB

unread,
Oct 2, 2008, 9:48:33 AM10/2/08
to
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 ?

I am trying to figure out how to add 1 hour to the $starttime and call
it my $endtime

Thanks,

JB

suchenwi

unread,
Oct 2, 2008, 10:05:24 AM10/2/08
to

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

Larry W. Virden

unread,
Oct 2, 2008, 10:20:28 AM10/2/08
to

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]

Gerald W. Lester

unread,
Oct 2, 2008, 10:26:27 AM10/2/08
to

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|
+------------------------------------------------------------------------+

Jonathan Bromley

unread,
Oct 2, 2008, 10:22:57 AM10/2/08
to

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.

Robert Heller

unread,
Oct 2, 2008, 10:45:47 AM10/2/08
to
At Thu, 2 Oct 2008 07:05:24 -0700 (PDT) suchenwi <richard.suchenw...@siemens.com> wrote:

>
> 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

JB

unread,
Oct 2, 2008, 10:51:40 AM10/2/08
to
On Oct 2, 9:26 am, "Gerald W. Lester" <Gerald.Les...@cox.net> wrote:
> JB wrote:
> > I am using the following code to define the date and time in my Expect
> > script:
>
> > set tdate [clockformat [clockseconds] -format %Y%m%d%H]
> > set tdate2 [clockformat [clockseconds] -format %Y-%m-%d]
> > set starttime [clockformat [clockseconds] -format %H]

>
> > set endtime  ?
>
> > I am trying to figure out how to add 1 hour to the $starttime and call
> > it my $endtime
>
> Here is how I would do it in Tcl 8.5:
>
> set now [clockseconds]
> set tdate [clockformat $now -format %Y%m%d%H]
> set tdate2 [clockformat $now -format %Y-%m-%d]
> set starttime [clockformat $now -format %H]
> set endtime [clockformat [clockadd $now 1 hour] -format %H]

>
> Here is how I would do it in Tcl prior to 8.5:
>
> set now [clockseconds]
> set tdate [clockformat $now -format %Y%m%d%H]
> set tdate2 [clockformat $now -format %Y-%m-%d]
> set starttime [clockformat $now -format %H]
> set endtime [clockformat [expr {$now + (60*60)}] -format %H]

>
> --
> +------------------------------------------------------------------------+
> | Gerald W. Lester                                                       |
> |"The man who fights for his ideals is the man who is alive." - Cervantes|
> +------------------------------------------------------------------------+

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

Gerald W. Lester

unread,
Oct 2, 2008, 11:15:51 AM10/2/08
to
JB wrote:
> On Oct 2, 9:26 am, "Gerald W. Lester" <Gerald.Les...@cox.net> wrote:
>> JB wrote:
>>> I am using the following code to define the date and time in my Expect
>>> script:
>>> set tdate [clockformat [clockseconds] -format %Y%m%d%H]
>>> set tdate2 [clockformat [clockseconds] -format %Y-%m-%d]
>>> set starttime [clockformat [clockseconds] -format %H]
>>> set endtime ?
>>> I am trying to figure out how to add 1 hour to the $starttime and call
>>> it my $endtime
>> Here is how I would do it in Tcl 8.5:
>>
>> set now [clockseconds]
>> set tdate [clockformat $now -format %Y%m%d%H]
>> set tdate2 [clockformat $now -format %Y-%m-%d]
>> set starttime [clockformat $now -format %H]
>> set endtime [clockformat [clockadd $now 1 hour] -format %H]
>>
>> Here is how I would do it in Tcl prior to 8.5:
>>
>> set now [clockseconds]
>> set tdate [clockformat $now -format %Y%m%d%H]
>> set tdate2 [clockformat $now -format %Y-%m-%d]
>> set starttime [clockformat $now -format %H]
>> set endtime [clockformat [expr {$now + (60*60)}] -format %H]
>
> 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.

Ok, then just change the format specifier.

For a full list of legal format values, read the clock man/help page.

Ralf Fassel

unread,
Oct 2, 2008, 11:19:21 AM10/2/08
to
* JB <jbj...@gmail.com>

| 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]

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'

sleb...@gmail.com

unread,
Oct 2, 2008, 11:43:58 AM10/2/08
to
On Oct 2, 10:51 pm, JB <jbju...@gmail.com> wrote:
> On Oct 2, 9:26 am, "Gerald W. Lester" <Gerald.Les...@cox.net> wrote:
>
> > JB wrote:
> > > I am using the following code to define the date and time in my Expect
> > > script:
>
> > > set tdate [clockformat [clockseconds] -format %Y%m%d%H]
> > > set tdate2 [clockformat [clockseconds] -format %Y-%m-%d]
> > > set starttime [clockformat [clockseconds] -format %H]
>
> > > set endtime  ?
>
> > > I am trying to figure out how to add 1 hour to the $starttime and call
> > > it my $endtime
>
> > Here is how I would do it in Tcl 8.5:
>
> > set endtime [clockformat [clockadd $now 1 hour] -format %H]
>
> 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

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

0 new messages