zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: before and after glob qualifiers
@ 2015-03-16 15:03 Peter Stephenson
  2015-03-19  6:52 ` Daniel Shahaf
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2015-03-16 15:03 UTC (permalink / raw)
  To: Zsh Hackers' List

I've had these lying around...

pws

diff --git a/Doc/Zsh/calsys.yo b/Doc/Zsh/calsys.yo
index 0d7abbf..8cd4415 100644
--- a/Doc/Zsh/calsys.yo
+++ b/Doc/Zsh/calsys.yo
@@ -10,9 +10,9 @@ or future events, details of which are stored in a text file (typically
 tt(calendar) in the user's home directory).  The version provided here
 includes a mechanism for alerting the user when an event is due.
 
-In addition a function tt(age) is provided that can be used in a glob
-qualifier; it allows files to be selected based on their modification
-times.
+In addition functions tt(age), tt(before) and tt(after) are provided
+that can be used in a glob qualifier; they allow files to be selected
+based on their modification times.
 
 The format of the tt(calendar) file and the dates used there in and in
 the tt(age) function are described first, then the functions that can
@@ -122,10 +122,10 @@ enditemize()
 Here, square brackets indicate optional elements, possibly with
 alternatives.  Fractions of a second are recognised but ignored.  For
 absolute times (the normal format require by the tt(calendar) file and the
-tt(age) function) a date is mandatory but a time of day is not; the time
-returned is at the start of the date.  One variation is allowed: if
-tt(a.m.) or tt(p.m.) or one of their variants is present, an hour without a
-minute is allowed, e.g. tt(3 p.m.).
+tt(age), tt(before) and tt(after) functions) a date is mandatory but a
+time of day is not; the time returned is at the start of the date.  One
+variation is allowed: if tt(a.m.) or tt(p.m.) or one of their variants
+is present, an hour without a minute is allowed, e.g. tt(3 p.m.).
 
 Time zones are not handled, though if one is matched following a time
 specification it will be removed to allow a surrounding date to be
@@ -605,8 +605,10 @@ left in a file with the suffix tt(.old).
 enditem()
 
 subsect(Glob qualifiers)
-findex(age)
 
+startitem()
+findex(age)
+item(tt(age))(
 The function tt(age) can be autoloaded and use separately from
 the calendar system, although it uses the function tt(calendar_scandate)
 for date formatting.  It requires the tt(zsh/stat) builtin, but uses
@@ -675,6 +677,22 @@ example(print *+LPAR()e-age :file1 :file2-+RPAR())
 
 matches all files modified no earlier than tt(file1) and
 no later than tt(file2); precision here is to the nearest second.
+)
+xitem(tt(after))
+item(tt(before))(
+findex(after)
+findex(before)
+The functions tt(after) and tt(before) are simpler versions of tt(age)
+that take just one argument.  The argument parsed similarly to an argument
+of tt(age); if it is not given the variable tt(AGEREF) is consulted.
+As the names of the functions suggest, a file matches if its
+modification time is after or before the time and date specified.  If
+a time only is given the date is today.
+
+The two following examples are therefore equivalent:
+example(print *+LPAR()e-after 12:00-RPAR()
+print *+LPAR()e-after today:12:00-RPAR())
+)
 
 texinode(Calendar Styles)(Calendar Utility Functions)(Calendar System User Functions)(Calendar Function System)
 sect(Styles)
diff --git a/Functions/Calendar/after b/Functions/Calendar/after
new file mode 100644
index 0000000..7fb0166
--- /dev/null
+++ b/Functions/Calendar/after
@@ -0,0 +1,67 @@
+# Glob qualifier function, e.g
+#
+# print *(e:after 2014/08/01:)
+# print *(e-after today:12:00-)
+#
+# If named before:
+# Match files modified before a given time.
+#
+# If named after:
+# Match files modified after a given time.  Use as glob qualifier.
+# N.B.: "after" actually includes the given time as it is to second
+# precision (it would be inconvenient to exclude the first second of a date).
+# It should therefore more logically be called "from", but that's a less
+# obvious name.
+#
+# File to test is in $REPLY.
+#
+# Similar to age, but only takes at most one data, which is
+# compared directly with the current time.
+
+emulate -L zsh
+
+zmodload -F zsh/stat b:zstat
+zmodload -i zsh/parameter
+
+autoload -Uz calendar_scandate
+
+local timefmt
+local -a vals tmp
+
+[[ -e $REPLY ]] || return 1
+zstat -A vals +mtime -- $REPLY || return 1
+
+if (( $# == 1 )); then
+  if [[ $1 = :* ]]; then
+    timefmt="%Y/%m/%d:%H:%M:%S"
+    zstat -A tmp -F $timefmt +mtime -- ${1#:} || return 1
+    local AGEREF=$tmp[1]
+  else
+    local AGEREF=$1
+  fi
+fi
+
+integer mtime=$vals[1] date1 date2
+local REPLY REPLY2
+
+# allow a time only (meaning today)
+if calendar_scandate -t $AGEREF; then
+  date1=$REPLY
+
+  case $0 in
+    (after)
+    (( mtime >= date1 ))
+    ;;
+
+    (before)
+    (( mtime < date1 ))
+    ;;
+
+    (*)
+    print "$0: must be named 'after' or 'before'" >&2
+    return 1
+    ;;
+  esac
+else
+  return 1
+fi
diff --git a/Functions/Calendar/before b/Functions/Calendar/before
new file mode 100644
index 0000000..7fb0166
--- /dev/null
+++ b/Functions/Calendar/before
@@ -0,0 +1,67 @@
+# Glob qualifier function, e.g
+#
+# print *(e:after 2014/08/01:)
+# print *(e-after today:12:00-)
+#
+# If named before:
+# Match files modified before a given time.
+#
+# If named after:
+# Match files modified after a given time.  Use as glob qualifier.
+# N.B.: "after" actually includes the given time as it is to second
+# precision (it would be inconvenient to exclude the first second of a date).
+# It should therefore more logically be called "from", but that's a less
+# obvious name.
+#
+# File to test is in $REPLY.
+#
+# Similar to age, but only takes at most one data, which is
+# compared directly with the current time.
+
+emulate -L zsh
+
+zmodload -F zsh/stat b:zstat
+zmodload -i zsh/parameter
+
+autoload -Uz calendar_scandate
+
+local timefmt
+local -a vals tmp
+
+[[ -e $REPLY ]] || return 1
+zstat -A vals +mtime -- $REPLY || return 1
+
+if (( $# == 1 )); then
+  if [[ $1 = :* ]]; then
+    timefmt="%Y/%m/%d:%H:%M:%S"
+    zstat -A tmp -F $timefmt +mtime -- ${1#:} || return 1
+    local AGEREF=$tmp[1]
+  else
+    local AGEREF=$1
+  fi
+fi
+
+integer mtime=$vals[1] date1 date2
+local REPLY REPLY2
+
+# allow a time only (meaning today)
+if calendar_scandate -t $AGEREF; then
+  date1=$REPLY
+
+  case $0 in
+    (after)
+    (( mtime >= date1 ))
+    ;;
+
+    (before)
+    (( mtime < date1 ))
+    ;;
+
+    (*)
+    print "$0: must be named 'after' or 'before'" >&2
+    return 1
+    ;;
+  esac
+else
+  return 1
+fi


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: PATCH: before and after glob qualifiers
  2015-03-16 15:03 PATCH: before and after glob qualifiers Peter Stephenson
@ 2015-03-19  6:52 ` Daniel Shahaf
  2015-03-19  9:37   ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Shahaf @ 2015-03-19  6:52 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh Hackers' List

Peter Stephenson wrote on Mon, Mar 16, 2015 at 15:03:23 +0000:
> I've had these lying around...
> 
> pws

You added two identical files, presumably one of them should be be
a symlink to the other?  Otherwise they'll diverge...

Daniel

> diff --git a/Functions/Calendar/after b/Functions/Calendar/after
> new file mode 100644
> index 0000000..7fb0166
> --- /dev/null
> +++ b/Functions/Calendar/after
...
> diff --git a/Functions/Calendar/before b/Functions/Calendar/before
> new file mode 100644
> index 0000000..7fb0166
> --- /dev/null
> +++ b/Functions/Calendar/before


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: PATCH: before and after glob qualifiers
  2015-03-19  6:52 ` Daniel Shahaf
@ 2015-03-19  9:37   ` Peter Stephenson
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2015-03-19  9:37 UTC (permalink / raw)
  To: Zsh Hackers' List

On Thu, 19 Mar 2015 06:52:53 +0000
Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> Peter Stephenson wrote on Mon, Mar 16, 2015 at 15:03:23 +0000:
> > I've had these lying around...
> > 
> > pws
> 
> You added two identical files, presumably one of them should be be
> a symlink to the other?  Otherwise they'll diverge...

That was deliberate --- I don't want to start maintaining installable
files that are actually symbolic links, or maybe not on some pokey old
system.

pws


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-03-19  9:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-16 15:03 PATCH: before and after glob qualifiers Peter Stephenson
2015-03-19  6:52 ` Daniel Shahaf
2015-03-19  9:37   ` Peter Stephenson

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).