mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-09-25 17:41:19 +02:00
- Use floatsecondsgetfn() and floatsecondssetfn() inside the int* version
of the functions (for improved accuracy). - Added getrawseconds() and setrawsecodns() to allow the code to save and restore the actual start time of the $SECONDS variable. - Changed the code that was adding in the child's elapsed time into the parent $SECONDS variable to just restore the raw time.
This commit is contained in:
parent
fd3bac933f
commit
a2365a60db
1 changed files with 25 additions and 18 deletions
43
Src/params.c
43
Src/params.c
|
@ -97,7 +97,9 @@ mod_export unsigned char bangchar;
|
|||
/**/
|
||||
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 @@ randomsetfn(Param pm, zlong v)
|
|||
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 @@ intsecondsgetfn(Param pm)
|
|||
void
|
||||
intsecondssetfn(Param pm, zlong x)
|
||||
{
|
||||
shtimer.tv_sec = time(NULL) - x;
|
||||
shtimer.tv_usec = 0;
|
||||
floatsecondssetfn(pm, (double)x);
|
||||
}
|
||||
|
||||
/**/
|
||||
|
@ -2706,8 +2707,23 @@ floatsecondssetfn(Param pm, double x)
|
|||
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 @@ scanendscope(HashNode hn, int flags)
|
|||
{
|
||||
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) ||
|
||||
|
|
Loading…
Reference in a new issue