From bbbaed2b5395e0a646dc618cd8c6bdd7dde25c81 Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Wed, 10 Aug 2011 11:31:18 +0000 Subject: 29663: add $EPOCHREALTIME to zsh/datetime --- Doc/Zsh/mod_datetime.yo | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'Doc/Zsh/mod_datetime.yo') diff --git a/Doc/Zsh/mod_datetime.yo b/Doc/Zsh/mod_datetime.yo index 145d4a181..514c43037 100644 --- a/Doc/Zsh/mod_datetime.yo +++ b/Doc/Zsh/mod_datetime.yo @@ -30,9 +30,17 @@ in seconds if tt(-r) is given) to var(scalar) instead of printing it. ) enditem() -The tt(zsh/datetime) module makes available one parameter: +The tt(zsh/datetime) module makes available several parameters: startitem() +vindex(EPOCHREALTIME) +item(tt(EPOCHREALTIME))( +A floating point value representing the number of seconds since +the epoch. The notional accuracy is to nanoseconds if the +tt(clock_gettime) call is available and to microseconds otherwise, +but in practice the range of double precision floating point and +shell scheduling latencies may be significant effects. +) vindex(EPOCHSECONDS) item(tt(EPOCHSECONDS))( An integer value representing the number of seconds since the -- cgit v1.2.3 From 8cbd5100022d651054b9edbbb79b38c8a442ec51 Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Thu, 11 Aug 2011 18:45:04 +0000 Subject: 29674: add $epochtime to datetime --- Doc/Zsh/mod_datetime.yo | 17 ++++++++++++++++- Src/Modules/datetime.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) (limited to 'Doc/Zsh/mod_datetime.yo') diff --git a/Doc/Zsh/mod_datetime.yo b/Doc/Zsh/mod_datetime.yo index 514c43037..619067698 100644 --- a/Doc/Zsh/mod_datetime.yo +++ b/Doc/Zsh/mod_datetime.yo @@ -30,7 +30,8 @@ in seconds if tt(-r) is given) to var(scalar) instead of printing it. ) enditem() -The tt(zsh/datetime) module makes available several parameters: +The tt(zsh/datetime) module makes available several parameters; +all are readonly: startitem() vindex(EPOCHREALTIME) @@ -45,5 +46,19 @@ vindex(EPOCHSECONDS) item(tt(EPOCHSECONDS))( An integer value representing the number of seconds since the epoch. +) +vindex(epochtime) +item(tt(epochtime))( +An array value containing the number of seconds since the epoch +in the first element and the remainder of the time since the epoch +in nanoseconds in the second element. To ensure the two elements +are consistent the array should be copied or otherwise referenced +as a single substitution before the values are used. The following +idiom may be used: + +example(for secs nsecs in $epochtime; do + ... +done) + ) enditem() diff --git a/Src/Modules/datetime.c b/Src/Modules/datetime.c index 1f2b7e81c..98bcd7d65 100644 --- a/Src/Modules/datetime.c +++ b/Src/Modules/datetime.c @@ -173,6 +173,45 @@ getcurrentrealtime(UNUSED(Param pm)) #endif } +static char ** +getcurrenttime(UNUSED(Param pm)) +{ + char **arr; + char buf[DIGBUFSIZE]; + +#ifdef HAVE_CLOCK_GETTIME + struct timespec now; + + if (clock_gettime(CLOCK_REALTIME, &now) < 0) { + zwarn("EPOCHREALTIME: unable to retrieve time: %e", errno); + return NULL; + } + + arr = (char **)zhalloc(3 * sizeof(*arr)); + sprintf(buf, "%ld", (long)now.tv_sec); + arr[0] = dupstring(buf); + sprintf(buf, "%ld", now.tv_nsec); + arr[1] = dupstring(buf); + arr[2] = NULL; + + return arr; +#else + struct timeval now; + struct timezone dummy_tz; + + gettimeofday(&now, &dummy_tz); + + arr = (char **)zhalloc(3 * sizeof(*arr)); + sprintf(buf, "%ld", (long)now.tv_sec); + arr[0] = dupstring(buf); + sprintf(buf, "%ld", (long)now.tv_usec * 1000); + arr[1] = dupstring(buf); + arr[2] = NULL; + + return arr; +#endif +} + static struct builtin bintab[] = { BUILTIN("strftime", 0, bin_strftime, 2, 2, 0, "qrs:", NULL), }; @@ -183,11 +222,16 @@ static const struct gsu_integer epochseconds_gsu = static const struct gsu_float epochrealtime_gsu = { getcurrentrealtime, NULL, stdunsetfn }; +static const struct gsu_array epochtime_gsu = +{ getcurrenttime, NULL, stdunsetfn }; + static struct paramdef patab[] = { SPECIALPMDEF("EPOCHSECONDS", PM_INTEGER|PM_READONLY, &epochseconds_gsu, NULL, NULL), SPECIALPMDEF("EPOCHREALTIME", PM_FFLOAT|PM_READONLY, - &epochrealtime_gsu, NULL, NULL) + &epochrealtime_gsu, NULL, NULL), + SPECIALPMDEF("epochtime", PM_ARRAY|PM_READONLY, + &epochtime_gsu, NULL, NULL) }; static struct features module_features = { -- cgit v1.2.3