Skip to content

Commit

Permalink
closes tcl-core [cd25761979]: clock format and clock add will acc…
Browse files Browse the repository at this point in the history
…ept `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
  • Loading branch information
sebres committed Jul 28, 2024
1 parent 6f178cf commit 321c541
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
8 changes: 4 additions & 4 deletions doc/clock.n
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ slowing its clock by a tiny fraction for some minutes until it is
back in sync with UTC; its data model does not represent minutes that
have 59 or 61 seconds.
.TP
\fI\-now\fR
Instead of \fItimeVal\fR a non-integer option \fI\-now\fR can be used as
\fI\now\fR
Instead of \fItimeVal\fR a non-integer option \fI\now\fR can be used as
replacement for today, which is simply interpolated to the runt-time as value
of \fBclock seconds\fR. For example:
.sp
\fBclock format -now -f %a; # current day of the week\fR
\fBclock format now -f %a; # current day of the week\fR
.sp
\fBclock add -now 1 month; # next month\fR
\fBclock add now 1 month; # next month\fR
.TP
\fIunit\fR
One of the words, \fBseconds\fR, \fBminutes\fR, \fBhours\fR,
Expand Down
35 changes: 16 additions & 19 deletions generic/tclClock.c
Original file line number Diff line number Diff line change
Expand Up @@ -3432,28 +3432,26 @@ ClockParseFmtScnArgs(
/* Base (by scan or add) or clock value (by format) */

if (opts->baseObj != NULL) {
register Tcl_Obj *baseObj = opts->baseObj;
/* bypass integer recognition if looks like option "-now" */
if (
(baseObj->length == 4 && baseObj->bytes && *(baseObj->bytes+1) == 'n') ||
TclGetWideIntFromObj(NULL, baseObj, &baseVal) != TCL_OK
) {

/* we accept "-now" as current date-time */
Tcl_Obj *baseObj = opts->baseObj;

/* bypass integer recognition if looks like "now" or "-now" */
if ((baseObj->bytes &&
((baseObj->length == 3 && baseObj->bytes[0] == 'n') ||
(baseObj->length == 4 && baseObj->bytes[1] == 'n')))
|| TclGetWideIntFromObj(NULL, baseObj, &baseVal) != TCL_OK) {
/* we accept "now" and "-now" as current date-time */
static const char *const nowOpts[] = {
"-now", NULL
"now", "-now", NULL
};
int idx;
if (Tcl_GetIndexFromObj(NULL, baseObj, nowOpts, "seconds or -now",
TCL_EXACT, &idx) == TCL_OK
) {
if (Tcl_GetIndexFromObj(NULL, baseObj, nowOpts, "seconds",
TCL_EXACT, &idx) == TCL_OK) {
goto baseNow;
}

Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"expected integer but got \"%s\"",
Tcl_GetString(baseObj)));
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INTEGER", NULL);
"bad seconds \"%s\": must be now or integer",
TclGetString(baseObj)));
i = 1;
goto badOption;
}
Expand Down Expand Up @@ -3550,9 +3548,8 @@ ClockFormatObjCmd(
int objc, /* Parameter count */
Tcl_Obj *const objv[]) /* Parameter values */
{
ClockClientData *dataPtr = clientData;

static const char *syntax = "clock format clockval|-now "
ClockClientData *dataPtr = (ClockClientData *)clientData;
static const char *syntax = "clock format clockval|now "
"?-format string? "
"?-gmt boolean? "
"?-locale LOCALE? ?-timezone ZONE?";
Expand Down Expand Up @@ -4398,7 +4395,7 @@ ClockAddObjCmd(
int objc, /* Parameter count */
Tcl_Obj *const objv[]) /* Parameter values */
{
static const char *syntax = "clock add clockval|-now ?number units?..."
static const char *syntax = "clock add clockval|now ?number units?..."
"?-gmt boolean? "
"?-locale LOCALE? ?-timezone ZONE?";
ClockClientData *dataPtr = clientData;
Expand Down
12 changes: 6 additions & 6 deletions tests/clock.test
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ test clock-0.1 "initial: auto-loading of ensemble and stubs on demand" no_tclclo
clock seconds; # init ensemble (but not yet stubs, loading of clock.tcl retarded)
lappend ret ens:[namespace ensemble exists ::clock]
lappend ret stubs:[expr {[namespace which -command ::tcl::clock::GetSystemTimeZone] ne ""}]
clock format -now; # clock.tcl stubs expected
clock format now; # clock.tcl stubs expected
lappend ret stubs:[expr {[namespace which -command ::tcl::clock::GetSystemTimeZone] ne ""}]
}]
interp delete $i
Expand All @@ -300,14 +300,14 @@ test clock-0.2 "initial: loading of format/locale does not overwrite interp stat
if {[catch {
return -level 0 -code error -errorcode {EXPERR TEST-ERROR} -errorinfo "ERROR expected error" test
}]} {
clock format -now -locale de; # should not overwrite error code/info
clock format now -locale de; # should not overwrite error code/info
list $::errorCode $::errorInfo
}
} -result {{EXPERR TEST-ERROR} {ERROR expected error}}

# Test some of the basics of [clock format]

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

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

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

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

test clock-1.9 "clock arguments: option doubly present" {
Expand Down

0 comments on commit 321c541

Please sign in to comment.