about summary refs log tree commit diff
path: root/Functions/Calendar/age
blob: d47f975adb9932671c90e1dc9367e0a62fa7770c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Match the age of a file, for use as a glob qualifer.  Can
# take one or two arguments, which can be supplied by one of two
# ways (always the same for both arguments):
#
#   print *(e:age 2006/10/04 2006/10/09:)
#
# Match all files modified between the start of those dates.
#
#   print *(e:age 2006/10/04:)
#
# Match all files modified on that date.  If the second argument is
# omitted it is taken to be exactly 24 hours after the first argument
# (even if the first argument contains a time).
#
#   print *(e-age 2006/10/04:10:15 2006/10/04:10:45-)
#
# Supply times.  All the time and formats handled by calendar_scandate
# are allowed, but whitespace must be quoted to ensure age receives
# the correct arguments.
#
#   AGEREF1=2006/10/04:10:15
#   AGEREF2=2006/10/04:10:45
#   print *(+age)
#
# The same example using the other form of argument passing.  The
# dates stay in effect until unset, but will be overridden if
# any argument is passed in the first format.

emulate -L zsh

zmodload -F zsh/stat b:zstat
zmodload -i zsh/parameter

autoload -U calendar_scandate

local -a vals

[[ -e $REPLY ]] || return 1
zstat -A vals +mtime $REPLY || return 1

if (( $# >= 1 )); then
  local AGEREF=$1
  # if 1 argument given, never use globally defined AGEREF2
  local AGEREF2=$2
fi

integer mtime=$vals[1] date1 date2
local REPLY

# allow a time only (meaning today)
if calendar_scandate -t $AGEREF; then
  date1=$REPLY

  if [[ -n $AGEREF2 ]]; then
    if [[ $AGEREF2 = +* ]]; then
      calendar_scandate -rt $AGEREF2[2,-1] || return 1
      (( date2 = date1 + REPLY ))
    else
      calendar_scandate -t $AGEREF2 || return 1
      date2=$REPLY
    fi
  else
    (( date2 = date1 + 24 * 60 * 60 ))
  fi

  (( date1 <= mtime && mtime <= date2 ))
else
  return 1
fi