Skip to content

Commit 321c541

Browse files
committed
closes tcl-core [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)
back-porting from https://core.tcl-lang.org/tcl/ci/f602e32fc3
1 parent 6f178cf commit 321c541

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

doc/clock.n

Lines changed: 4 additions & 4 deletions
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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3432,28 +3432,26 @@ ClockParseFmtScnArgs(
34323432
/* Base (by scan or add) or clock value (by format) */
34333433

34343434
if (opts->baseObj != NULL) {
3435-
register Tcl_Obj *baseObj = opts->baseObj;
3436-
/* bypass integer recognition if looks like option "-now" */
3437-
if (
3438-
(baseObj->length == 4 && baseObj->bytes && *(baseObj->bytes+1) == 'n') ||
3439-
TclGetWideIntFromObj(NULL, baseObj, &baseVal) != TCL_OK
3440-
) {
3441-
3442-
/* we accept "-now" as current date-time */
3435+
Tcl_Obj *baseObj = opts->baseObj;
3436+
3437+
/* bypass integer recognition if looks like "now" or "-now" */
3438+
if ((baseObj->bytes &&
3439+
((baseObj->length == 3 && baseObj->bytes[0] == 'n') ||
3440+
(baseObj->length == 4 && baseObj->bytes[1] == 'n')))
3441+
|| TclGetWideIntFromObj(NULL, baseObj, &baseVal) != TCL_OK) {
3442+
/* we accept "now" and "-now" as current date-time */
34433443
static const char *const nowOpts[] = {
3444-
"-now", NULL
3444+
"now", "-now", NULL
34453445
};
34463446
int idx;
3447-
if (Tcl_GetIndexFromObj(NULL, baseObj, nowOpts, "seconds or -now",
3448-
TCL_EXACT, &idx) == TCL_OK
3449-
) {
3447+
if (Tcl_GetIndexFromObj(NULL, baseObj, nowOpts, "seconds",
3448+
TCL_EXACT, &idx) == TCL_OK) {
34503449
goto baseNow;
34513450
}
34523451

34533452
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
3454-
"expected integer but got \"%s\"",
3455-
Tcl_GetString(baseObj)));
3456-
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INTEGER", NULL);
3453+
"bad seconds \"%s\": must be now or integer",
3454+
TclGetString(baseObj)));
34573455
i = 1;
34583456
goto badOption;
34593457
}
@@ -3550,9 +3548,8 @@ ClockFormatObjCmd(
35503548
int objc, /* Parameter count */
35513549
Tcl_Obj *const objv[]) /* Parameter values */
35523550
{
3553-
ClockClientData *dataPtr = clientData;
3554-
3555-
static const char *syntax = "clock format clockval|-now "
3551+
ClockClientData *dataPtr = (ClockClientData *)clientData;
3552+
static const char *syntax = "clock format clockval|now "
35563553
"?-format string? "
35573554
"?-gmt boolean? "
35583555
"?-locale LOCALE? ?-timezone ZONE?";
@@ -4398,7 +4395,7 @@ ClockAddObjCmd(
43984395
int objc, /* Parameter count */
43994396
Tcl_Obj *const objv[]) /* Parameter values */
44004397
{
4401-
static const char *syntax = "clock add clockval|-now ?number units?..."
4398+
static const char *syntax = "clock add clockval|now ?number units?..."
44024399
"?-gmt boolean? "
44034400
"?-locale LOCALE? ?-timezone ZONE?";
44044401
ClockClientData *dataPtr = clientData;

tests/clock.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ test clock-0.1 "initial: auto-loading of ensemble and stubs on demand" no_tclclo
284284
clock seconds; # init ensemble (but not yet stubs, loading of clock.tcl retarded)
285285
lappend ret ens:[namespace ensemble exists ::clock]
286286
lappend ret stubs:[expr {[namespace which -command ::tcl::clock::GetSystemTimeZone] ne ""}]
287-
clock format -now; # clock.tcl stubs expected
287+
clock format now; # clock.tcl stubs expected
288288
lappend ret stubs:[expr {[namespace which -command ::tcl::clock::GetSystemTimeZone] ne ""}]
289289
}]
290290
interp delete $i
@@ -300,14 +300,14 @@ test clock-0.2 "initial: loading of format/locale does not overwrite interp stat
300300
if {[catch {
301301
return -level 0 -code error -errorcode {EXPERR TEST-ERROR} -errorinfo "ERROR expected error" test
302302
}]} {
303-
clock format -now -locale de; # should not overwrite error code/info
303+
clock format now -locale de; # should not overwrite error code/info
304304
list $::errorCode $::errorInfo
305305
}
306306
} -result {{EXPERR TEST-ERROR} {ERROR expected error}}
307307

308308
# Test some of the basics of [clock format]
309309

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

319319
test clock-1.1 "clock format - bad time" {
320320
list [catch {clock format foo} msg] $msg
321-
} {1 {expected integer but got "foo"}}
321+
} {1 {bad seconds "foo": must be now or integer}}
322322

323323
test clock-1.2 "clock format - bad gmt val" {
324324
list [catch {clock format 0 -gmt foo} msg] $msg
@@ -353,10 +353,10 @@ test clock-1.7.1 "clock format - command abbreviations (compat regression test)"
353353
clock f 0 -g 1 -f "%Y-%m-%d"
354354
} 1970-01-01
355355

356-
test clock-1.8 "clock format -now" {
356+
test clock-1.8 "clock format now" {
357357
# give one second more for test (if on boundary of the current second):
358358
set n [clock format [clock seconds] -g 1 -f "%s"]
359-
expr {[clock format -now -g 1 -f "%s"] in [list $n [incr n]]}
359+
expr {[clock format now -g 1 -f "%s"] in [list $n [incr n]]}
360360
} 1
361361

362362
test clock-1.9 "clock arguments: option doubly present" {

0 commit comments

Comments
 (0)