zsh-workers
 help / color / mirror / code / Atom feed
From: Wayne Davison <wayned@users.sourceforge.net>
To: Peter Stephenson <pws@csr.com>
Cc: Zsh hackers list <zsh-workers@sunsite.dk>
Subject: PATCH: my "SECONDS can be floating point" tweaks
Date: Wed, 30 Oct 2002 13:17:51 -0800	[thread overview]
Message-ID: <20021030211750.GA6296@scuzzy.blorf.net> (raw)
In-Reply-To: <22634.1035831352@csr.com>

On Mon, Oct 28, 2002 at 06:55:52PM +0000, Peter Stephenson wrote:
> This allows typeset to switch the SECONDS special parameter between
> integer and floating point

Here's a fairly simple patch that I think improves things a bit.

First, the intseconds{get,set}fn() functions are implemented by calling
the float* versions.  This makes the internal representation of the
value identical regardless of type (i.e. it ensures that shtimer.tv_usec
gets set relative to the current time, and that it is subtracted out on
get).

The second change may be more controversial.  I introduced two new
functions that allow us to get and set the "raw" (non-relative to now)
value of the SECONDS variable (though the value is returned as a
double).  I then added some code to builtin.c that saves this raw value
into the cached off u.dval, and changed the new code in params.c that
was adding local-elapsed time into the parent variable to just reload
the raw parent value.

A few other minor tweaks are also included, such as not testing the
newspecial enum against 0, enhancing the $SECONDS comment, and casting
the shtimer.* values to zlong instead of int (just in case int is too
small).

I won't commit this until I get some positive feedback about the
changes.

..wayne..

---8<------8<------8<------8<---cut here--->8------>8------>8------>8---
Index: Src/builtin.c
--- Src/builtin.c	29 Oct 2002 10:56:40 -0000	1.89
+++ Src/builtin.c	30 Oct 2002 21:08:28 -0000
@@ -1774,6 +1774,8 @@
 	    return NULL;
 	}
     }
+    else if (newspecial != NS_NONE && strcmp(pname, "SECONDS") == 0)
+	newspecial = NS_SECONDS;
 
     /*
      * A parameter will be local if
@@ -1913,8 +1915,12 @@
 	 * because we've checked for unpleasant surprises above.
 	 */
 	pm->flags = (PM_TYPE(pm->flags) | on | PM_SPECIAL) & ~off;
-	if (newspecial == NS_SECONDS)
+	if (newspecial == NS_SECONDS) {
+	    /* We save off the raw internal value of the SECONDS var */
+	    tpm->u.dval = getrawseconds();
 	    setsecondstype(pm, on, off);
+	}
+
 	/*
 	 * Final tweak: if we've turned on one of the flags with
 	 * numbers, we should use the appropriate integer.
@@ -1998,7 +2004,7 @@
 		  "BUG: parameter recreated with wrong flags");
 	    unsetparam_pm(ipm, 0, 1);
 	}
-    } else if (newspecial && !(pm->old->flags & PM_NORESTORE)) {
+    } else if (newspecial != NS_NONE && !(pm->old->flags & PM_NORESTORE)) {
 	/*
 	 * We need to use the special setting function to re-initialise
 	 * the special parameter to empty.
Index: Src/params.c
--- Src/params.c	29 Oct 2002 12:58:03 -0000	1.68
+++ Src/params.c	30 Oct 2002 21:08:29 -0000
@@ -97,7 +97,9 @@
 /**/
 unsigned char hatchar, hashchar;
  
-/* $SECONDS = time(NULL) - shtimer.tv_sec */
+/* $SECONDS = now.tv_sec - shtimer.tv_sec
+ *          + (now.tv_usec - shtimer.tv_usec) / 1000000.0
+ * (rounded to an integer if the parameter is not set to float) */
  
 /**/
 struct timeval shtimer;
@@ -2672,7 +2674,7 @@
 zlong
 intsecondsgetfn(Param pm)
 {
-    return time(NULL) - shtimer.tv_sec;
+    return (zlong)floatsecondsgetfn(pm);
 }
 
 /* Function to set value of special parameter `SECONDS' */
@@ -2681,8 +2683,7 @@
 void
 intsecondssetfn(Param pm, zlong x)
 {
-    shtimer.tv_sec = time(NULL) - x;
-    shtimer.tv_usec = 0;
+    floatsecondssetfn(pm, (double)x);
 }
 
 /**/
@@ -2706,8 +2707,23 @@
     struct timezone dummy_tz;
 
     gettimeofday(&now, &dummy_tz);
-    shtimer.tv_sec = now.tv_sec - (int)x;
-    shtimer.tv_usec = now.tv_usec - (int)((x - (double)(int)x) * 1000000.0);
+    shtimer.tv_sec = now.tv_sec - (zlong)x;
+    shtimer.tv_usec = now.tv_usec - (zlong)((x - (zlong)x) * 1000000.0);
+}
+
+/**/
+double
+getrawseconds(void)
+{
+    return (double)shtimer.tv_sec + (double)shtimer.tv_usec / 1000000.0;
+}
+
+/**/
+void
+setrawseconds(double x)
+{
+    shtimer.tv_sec = (zlong)x;
+    shtimer.tv_usec = (zlong)((x - (zlong)x) * 1000000.0);
 }
 
 /**/
@@ -3487,19 +3503,10 @@
 	    {
 		setsecondstype(pm, PM_TYPE(tpm->flags), PM_TYPE(pm->flags));
 		/*
-		 * We restore SECONDS by adding back in the elapsed
-		 * time (from the point we reset shtimer) rather
-		 * than restoring it completely, since SECONDS should
-		 * run in the calling function, too.
+		 * We restore SECONDS by restoring its raw internal value
+		 * that we cached off into tpm->u.dval.
 		 */
-		if (PM_TYPE(pm->flags) == PM_INTEGER)
-		{
-		    pm->sets.ifn(pm, pm->gets.ifn(pm) + tpm->u.val);
-		}
-		else
-		{
-		    pm->sets.ffn(pm, pm->gets.ffn(pm) + tpm->u.dval);
-		}
+		setrawseconds(tpm->u.dval);
 		tpm->flags |= PM_NORESTORE;
 	    }
 	    DPUTS(!tpm || PM_TYPE(pm->flags) != PM_TYPE(tpm->flags) ||
---8<------8<------8<------8<---cut here--->8------>8------>8------>8---


  parent reply	other threads:[~2002-10-30 21:19 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-28 18:55 PATCH: SECONDS can be floating point Peter Stephenson
2002-10-29 10:40 ` Peter Stephenson
2002-10-29 11:23   ` Peter Stephenson
2002-10-29 17:52     ` Wayne Davison
2002-10-29 18:08     ` Wayne Davison
2002-10-29 18:13       ` Peter Stephenson
2002-10-30 21:17 ` Wayne Davison [this message]
2002-10-31  5:09   ` PATCH: my "SECONDS can be floating point" tweaks Bart Schaefer
2002-10-31 18:53     ` Wayne Davison
2002-10-31 10:34   ` Peter Stephenson
2002-10-31 18:53     ` Wayne Davison

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20021030211750.GA6296@scuzzy.blorf.net \
    --to=wayned@users.sourceforge.net \
    --cc=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).