Tcl Source Code

Artifact [dd0be456db]
Login

Artifact dd0be456dbe53cdd9e79e3ef6af9b6bd715c67cb:

Attachment "clock.patch" to ticket [991742ffff] added by rmax 2004-07-16 00:12:35.
--- doc/clock.n	24 May 2004 23:31:42 -0000	1.17
+++ doc/clock.n	15 Jul 2004 17:06:08 -0000
@@ -35,27 +35,16 @@
 time value as a system-dependent integer value.  The unit of the value
 is system-dependent but should be the highest resolution clock available
 on the system such as a CPU cycle counter. 
-.TP
+
 .VS 8.5
 If the \fIoption\fR argument is \fB\-milliseconds\fR, then the value is
 guaranteed to be an approximate count of milliseconds returned as
 a wide integer; the rule should
 always hold that \fBclock clicks -milliseconds\fR divided by 1000 is the
 same as \fBclock seconds\fR.
-.TP
-It the \fIoption\fR argument is \fB-microseconds\fR, then the value is
-guaranteed to be an approximate count of microseconds returned as a wide
-integer; the rule should hold that \fBclock clicks -microseconds\fR
-divided by 1000 is the same as \fBclock clicks -milliseconds\fR.
-.TP
-On some hardware, the counts of milliseconds and microseconds may diverge
-from the system clock for short periods; the reason is that they can
-be derived from different sources, and a complex procedure is required
-to calibrate them.  Moreover, Tcl makes an effort never to have the clock
-leap forward nor appear to run backward, preferring instead to slow or
-speed up the clock frequency slightly until it's back in synchronization.
-For this reason, most Tcl programmers need never worry about such
-phenomena as leap seconds.
+
+The \fB-milliseconds\fR option is deprecated and may be removed in a
+future version. Use \fBclock milliseconds\fR instead.
 .VE 8.5
 .TP
 \fBclock format \fIclockValue\fR ?\fB\-format \fIstring\fR? ?\fB\-gmt \fIboolean\fR?
@@ -275,6 +264,28 @@
 unit of the value is seconds, allowing it to be used for relative time
 calculations.  The value is usually defined as total elapsed time from
 an ``epoch''.  You shouldn't assume the value of the epoch.
+.TP
+\fBclock milliseconds\fR
+.TP
+\fBclock microseconds\fR
+.VS 8.5
+Return the current date and time as a wide integer counting in
+milliseconds or microseconds repectively.
+
+The rule should always hold that \fBclock milliseconds\fR divided by
+1000 and \fBclock microseconds\fR divided by one million is the same
+as \fBclock seconds\fR.
+
+On some hardware, the counts of milliseconds and microseconds may
+diverge from the system clock for short periods; the reason is that
+they can be derived from different sources, and a complex procedure is
+required to calibrate them.  Moreover, Tcl makes an effort never to
+have the clock leap forward nor appear to run backward, preferring
+instead to slow or speed up the clock frequency slightly until it's
+back in synchronization.  For this reason, most Tcl programmers need
+never worry about such phenomena as leap seconds.
+.VE 8.5
+.TP
 .SH EXAMPLE
 Print out the current date and time, first using the default format
 and then using an ISO 8601 format:
Index: generic/tclClock.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclClock.c,v
retrieving revision 1.28
diff -u -r1.28 tclClock.c
--- generic/tclClock.c	14 May 2004 21:43:28 -0000	1.28
+++ generic/tclClock.c	15 Jul 2004 17:06:08 -0000
@@ -68,10 +68,12 @@
     Tcl_Time now;		/* Current time */
 
     static CONST char *switches[] = {
-	"clicks", "format", "scan", "seconds", (char *) NULL
+	"clicks", "format", "microseconds", "milliseconds",
+	"scan", "seconds", (char *) NULL
     };
     enum command {
-	COMMAND_CLICKS, COMMAND_FORMAT, COMMAND_SCAN, COMMAND_SECONDS
+	COMMAND_CLICKS, COMMAND_FORMAT, COMMAND_MICROSECONDS,
+	COMMAND_MILLISECONDS, COMMAND_SCAN, COMMAND_SECONDS
     };
     static CONST char *clicksSwitches[] = {
 	"-milliseconds", "-microseconds", (char*) NULL
@@ -123,6 +125,26 @@
 	    return TCL_OK;
 	}
 
+	case COMMAND_MICROSECONDS: /* microseconds */
+	    if (objc != 2) {
+		Tcl_WrongNumArgs(interp, 2, objv, NULL);
+		return TCL_ERROR;
+	    }
+	    Tcl_GetTime(&now);
+	    Tcl_SetWideIntObj(resultPtr,
+			      ((Tcl_WideInt) now.sec * 1000000 + now.usec));
+	    return TCL_OK;
+
+	case COMMAND_MILLISECONDS: /* milliseconds */
+	    if (objc != 2) {
+		Tcl_WrongNumArgs(interp, 2, objv, NULL);
+		return TCL_ERROR;
+	    }
+	    Tcl_GetTime(&now);
+	    Tcl_SetWideIntObj(resultPtr,
+			      ((Tcl_WideInt) now.sec * 1000 + now.usec / 1000));
+	    return TCL_OK;
+
 	case COMMAND_FORMAT:			/* format */
 	    if ((objc < 3) || (objc > 7)) {
 		wrongFmtArgs:
Index: tests/clock.test
===================================================================
RCS file: /cvsroot/tcl/tcl/tests/clock.test,v
retrieving revision 1.34
diff -u -r1.34 clock.test
--- tests/clock.test	10 Jul 2004 21:26:10 -0000	1.34
+++ tests/clock.test	15 Jul 2004 17:06:09 -0000
@@ -41,7 +41,7 @@
 } {1 {wrong # args: should be "clock option ?arg ...?"}}
 test clock-1.2 {clock tests} {
     list [catch {clock foo} msg] $msg
-} {1 {bad option "foo": must be clicks, format, scan, or seconds}}
+} {1 {bad option "foo": must be clicks, format, microseconds, milliseconds, scan, or seconds}}
 
 # clock clicks
 test clock-2.1 {clock clicks tests} {
@@ -709,6 +709,80 @@
     clock scan "1600" -gmt true -base $base
 } 1078848000
 
+###############################################
+# [clock milliseconds] and [clock microseconds]
+
+test clock-12.1 {milliseconds with excess argument} {
+    list [catch {clock milliseconds foo} error] $error
+} {1 {wrong # args: should be "clock milliseconds"}}
+
+test clock-12.2 {clock milliseconds timing test} {
+    set start [clock milli]
+    after 10
+    set end [clock milli]
+    # 60 msecs seems to be the max time slice under Windows 95/98
+    expr {($end > $start) && (($end - $start) <= 60)}
+} {1}
+
+test clock-12.3 {clock microseconds timing test} {
+    set start [clock micro]
+    after 10
+    set end [clock micro]
+    # 60 msecs seems to be the max time slice under Windows 95/98
+    expr {($end > $start) && (($end - $start) <= 60000)}
+} {1}
+
+test clock-12.3 {clock clicks test, millis align with seconds} {
+    set t1 [clock seconds]
+    while { 1 } {
+	set t2 [clock clicks -millis]
+	set t3 [clock seconds]
+	if { $t3 == $t1 } break
+	set t1 $t3
+    }
+    expr { $t2 / 1000 == $t3 }
+} {1}
+
+test clock-12.4 {clock micros align with seconds} {
+    set t1 [clock seconds]
+    while { 1 } {
+	set t2 [clock micros]
+	set t3 [clock seconds]
+	if { $t3 == $t1 } break
+	set t1 $t3
+    }
+    expr { $t2 / 1000000 == $t3 }
+} {1}
+
+test clock-12.5 {clock millis align with micros} {
+    set t1 [clock millis]
+    set i 0
+    while { $i < 1000 } {
+	set t2 [clock micros]
+	set t3 [clock millis]
+	if { $t3 == $t1 } break
+	set t1 $t3
+        incr i
+    }
+    if { $i >= 1000 } {
+        set result {can't test millis/micros alignment, loop running too slow}
+    } else {
+        set result [expr { $t2 / 1000 == $t3 }]
+    }
+    set result
+} {1}
+
+test clock-12.6 {clock milliseconds tests} {
+    expr {[clock milliseconds]+1}
+    concat {}
+} {}
+
+test clock-12.7 {clock microseconds tests} {
+    expr {[clock microseconds]+1}
+    concat {}
+} {}
+
+
 if { [info exists oldlctime] } {
     set env(LC_TIME) $oldlctime
 } else {