Skip to content

Commit d90942b

Browse files
committed
closes [cd25761979]: clock format and clock add will accept now as clock value (value -now retained to compat reasons to earlier versions and tclclockmod, undocumented at the moment)
1 parent 68b1c9e commit d90942b

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

doc/clock.n

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ slowing its clock by a tiny fraction for some minutes until it is
8787
back in sync with UTC; its data model does not represent minutes that
8888
have 59 or 61 seconds.
8989
.TP
90-
\fI\-now\fR
91-
Instead of \fItimeVal\fR a non-integer option \fI\-now\fR can be used as
90+
\fI\now\fR
91+
Instead of \fItimeVal\fR a non-integer option \fI\now\fR can be used as
9292
replacement for today, which is simply interpolated to the runt-time as value
9393
of \fBclock seconds\fR. For example:
9494
.sp
95-
\fBclock format -now -f %a; # current day of the week\fR
95+
\fBclock format now -f %a; # current day of the week\fR
9696
.sp
97-
\fBclock add -now 1 month; # next month\fR
97+
\fBclock add now 1 month; # next month\fR
9898
.TP
9999
\fIunit\fR
100100
One of the words, \fBseconds\fR, \fBminutes\fR, \fBhours\fR,

generic/tclClock.c

+12-8
Original file line numberDiff line numberDiff line change
@@ -3419,16 +3419,18 @@ ClockParseFmtScnArgs(
34193419
if (opts->baseObj != NULL) {
34203420
Tcl_Obj *baseObj = opts->baseObj;
34213421

3422-
/* bypass integer recognition if looks like option "-now" */
3423-
if ((baseObj->bytes && baseObj->length == 4 && baseObj->bytes[1] == 'n')
3422+
/* bypass integer recognition if looks like "now" or "-now" */
3423+
if ((baseObj->bytes &&
3424+
((baseObj->length == 3 && baseObj->bytes[0] == 'n') ||
3425+
(baseObj->length == 4 && baseObj->bytes[1] == 'n')))
34243426
|| TclGetWideIntFromObj(NULL, baseObj, &baseVal) != TCL_OK) {
3425-
/* we accept "-now" as current date-time */
3427+
/* we accept "now" and "-now" as current date-time */
34263428
static const char *const nowOpts[] = {
3427-
"-now", NULL
3429+
"now", "-now", NULL
34283430
};
34293431
int idx;
34303432

3431-
if (Tcl_GetIndexFromObj(interp, baseObj, nowOpts, "seconds",
3433+
if (Tcl_GetIndexFromObj(NULL, baseObj, nowOpts, "seconds",
34323434
TCL_EXACT, &idx) == TCL_OK) {
34333435
goto baseNow;
34343436
}
@@ -3437,7 +3439,9 @@ ClockParseFmtScnArgs(
34373439
goto baseOverflow;
34383440
}
34393441

3440-
Tcl_AppendResult(interp, " or integer", (char *)NULL);
3442+
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
3443+
"bad seconds \"%s\": must be now or integer",
3444+
TclGetString(baseObj)));
34413445
i = baseIdx;
34423446
goto badOption;
34433447
}
@@ -3529,7 +3533,7 @@ ClockFormatObjCmd(
35293533
Tcl_Obj *const objv[]) /* Parameter values */
35303534
{
35313535
ClockClientData *dataPtr = (ClockClientData *)clientData;
3532-
static const char *syntax = "clock format clockval|-now "
3536+
static const char *syntax = "clock format clockval|now "
35333537
"?-format string? "
35343538
"?-gmt boolean? "
35353539
"?-locale LOCALE? ?-timezone ZONE?";
@@ -4363,7 +4367,7 @@ ClockAddObjCmd(
43634367
int objc, /* Parameter count */
43644368
Tcl_Obj *const objv[]) /* Parameter values */
43654369
{
4366-
static const char *syntax = "clock add clockval|-now ?number units?..."
4370+
static const char *syntax = "clock add clockval|now ?number units?..."
43674371
"?-gmt boolean? "
43684372
"?-locale LOCALE? ?-timezone ZONE?";
43694373
ClockClientData *dataPtr = (ClockClientData *)clientData;

tests/clock.test

+8-8
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ test clock-0.1 "initial: auto-loading of ensemble and stubs on demand" -setup {
283283
clock seconds; # init ensemble (but not yet stubs, loading of clock.tcl retarded)
284284
lappend ret ens:[namespace ensemble exists ::clock]
285285
lappend ret stubs:[expr {[namespace which -command ::tcl::clock::GetSystemTimeZone] ne ""}]
286-
clock format -now; # clock.tcl stubs expected
286+
clock format now; # clock.tcl stubs expected
287287
lappend ret stubs:[expr {[namespace which -command ::tcl::clock::GetSystemTimeZone] ne ""}]
288288
}
289289
} -cleanup {
@@ -298,7 +298,7 @@ test clock-0.1a "initial: safe interpreter shares clock command with parent" -se
298298
$sci eval { clock seconds }; # init ensemble (but not yet stubs, loading of clock.tcl retarded)
299299
lappend ret ens:[namespace ensemble exists ::clock]
300300
lappend ret stubs:[expr {[namespace which -command ::tcl::clock::GetSystemTimeZone] ne ""}]
301-
$sci eval { clock format -now }; # clock.tcl stubs expected
301+
$sci eval { clock format now }; # clock.tcl stubs expected
302302
lappend ret stubs:[expr {[namespace which -command ::tcl::clock::GetSystemTimeZone] ne ""}]
303303
}
304304
} -cleanup {
@@ -314,14 +314,14 @@ test clock-0.2 "initial: loading of format/locale does not overwrite interp stat
314314
if {[catch {
315315
return -level 0 -code error -errorcode {EXPERR TEST-ERROR} -errorinfo "ERROR expected error" test
316316
}]} {
317-
clock format -now -locale de; # should not overwrite error code/info
317+
clock format now -locale de; # should not overwrite error code/info
318318
list $::errorCode $::errorInfo
319319
}
320320
} -result {{EXPERR TEST-ERROR} {ERROR expected error}}
321321

322322
# Test some of the basics of [clock format]
323323

324-
set syntax "clockval|-now ?-format string? ?-gmt boolean? ?-locale LOCALE? ?-timezone ZONE?"
324+
set syntax "clockval|now ?-format string? ?-gmt boolean? ?-locale LOCALE? ?-timezone ZONE?"
325325
test clock-1.0 "clock format - wrong # args" {
326326
list [catch {clock format} msg] $msg $::errorCode
327327
} [subst {1 {wrong # args: should be "clock format $syntax"} {CLOCK wrongNumArgs}}]
@@ -332,7 +332,7 @@ test clock-1.0.1 "clock format - wrong # args (compiled ensemble with invalid sy
332332

333333
test clock-1.1 "clock format - bad time" {
334334
list [catch {clock format foo} msg opt] $msg [dict getd $opt -errorcode {}]
335-
} {1 {bad seconds "foo": must be -now or integer} {CLOCK badOption foo}}
335+
} {1 {bad seconds "foo": must be now or integer} {CLOCK badOption foo}}
336336

337337
test clock-1.2 "clock format - bad gmt val" {
338338
list [catch {clock format 0 -gmt foo} msg] $msg
@@ -367,10 +367,10 @@ test clock-1.7.1 "clock format - command abbreviations (compat regression test)"
367367
clock f 0 -g 1 -f "%Y-%m-%d"
368368
} 1970-01-01
369369

370-
test clock-1.8 "clock format -now" {
370+
test clock-1.8 "clock format now" {
371371
# give one second more for test (if on boundary of the current second):
372372
set n [clock format [clock seconds] -g 1 -f "%s"]
373-
expr {[clock format -now -g 1 -f "%s"] in [list $n [incr n]]}
373+
expr {[clock format now -g 1 -f "%s"] in [list $n [incr n]]}
374374
} 1
375375

376376
test clock-1.9 "clock arguments: option doubly present" {
@@ -18704,7 +18704,7 @@ test clock-6.8 {input of seconds} {
1870418704

1870518705
test clock-6.8b "clock scan - bad base" {
1870618706
list [catch {clock scan "" -base foo -gmt 1} msg opt] $msg [dict getd $opt -errorcode {}]
18707-
} {1 {bad seconds "foo": must be -now or integer} {CLOCK badOption foo}}
18707+
} {1 {bad seconds "foo": must be now or integer} {CLOCK badOption foo}}
1870818708

1870918709
test clock-6.9 {input of seconds - overflow} {
1871018710
list [catch {clock scan -9223372036854775809 -format %s -gmt true} result opt] $result [dict getd $opt -errorcode ""]

0 commit comments

Comments
 (0)