仅显示使用ics文件创建或修改的最后一小时事件

时间:2016-05-19 23:24:40

标签: bash file icalendar

我有一个关于前一个问题和Charles Duffy答案的新问题。如果有新的事件被创建或修改,我需要搜索.ics文件并在IRC上每小时显示一次。

旧问题:Parsing ICS file with bash

@ charles-duffy的回应:

#!/bin/bash

handle_event() {
  : # put a definition of your intended logic here
}

declare -A content=( ) # define an associative array (aka map, aka hash)
declare -A tzid=( )    # another associative array for timezone info

while IFS=: read -r key value; do
  value=${value%$'\r'} # remove DOS newlines
  if [[ $key = END && $value = VEVENT ]]; then
    handle_event # defining this function is up to you; see suggestion below
    content=( )
    tzid=( )
  else
    if [[ $key = *";TZID="* ]]; then
      tzid[$key%";"*]=${key##*";TZID="}
    fi
    content[${key%";"*}]=$value
  fi
done

...其中handle_event是一个完成你关心的实际工作的函数。例如,这可能是这样的:

local_date() {
  local tz=${tzid[$1]}
  local dt=${content[$1]}
  if [[ $dt = *Z ]]; then
    tz=UTC
    dt=${dt%Z}
  fi

  # note that this requires GNU date
  date --date="TZ=\"$tz\" ${dt:0:4}-${dt:4:2}-${dt:6:2}T${dt:9:2}:${dt:11:2}"
}

handle_event() {
  if [[ "${content[LAST-MODIFIED]}" = "${content[CREATED]}" ]]; then
    echo "New Event Created"
  else
    echo "Modified Event"
  fi
  printf '%s\t' "$(local_date DTSTART)" "${content[SUMMARY]}" "${content[LOCATION]}"; echo
}

使用您给定的输入文件和上面的脚本,bash parse-ics <test.ics会产生以下输出(使用我当前的语言环境,时区和语言):

New Event Created
Sun Jun 12 15:10:00 CDT 2016    Ash vs Evil Dead Saison 1 Episode 9 & 10        OCS Choc
Modified Event
Sat Jun 11 15:35:00 CDT 2016    The Mysteries Of Laura Saison 2 Episode 1 à 4   RTS Un (Suisse)

1 个答案:

答案 0 :(得分:0)

简单的做法是提取当前日期,在本地时间提取日期,并进行比较。

local_date() {
  local tz=${tzid[$1]}
  local dt=${content[$1]}
  if [[ $dt = *Z ]]; then
    tz=UTC
    dt=${dt%Z}
  fi
  shift ## <- remove $1 from the argument list, so "$@" is all extra arguments

  if [[ $dt = *T* ]]; then
    dt="${dt:0:4}-${dt:4:2}-${dt:6:2}T${dt:9:2}:${dt:11:2}"
  else
    dt="${dt:0:4}-${dt:4:2}-${dt:6:2}"
  fi

  # note that this requires GNU date
  date --date="TZ=\"$tz\" $dt" "$@"
}

......然后:

handle_event() {

  ## return if date is not today
  if [[ "$(date +%Y%m%d)" != "$(local_date DTSTART +%Y%m%d)" ]]; then
    return
  fi

  ## otherwise, emit normal content as output
  if [[ "${content[LAST-MODIFIED]}" = "${content[CREATED]}" ]]; then
    echo "New Event Created"
  else
    echo "Modified Event"
  fi
  printf '%s\t' "$(local_date DTSTART)" "${content[SUMMARY]}" "${content[LOCATION]}"; echo
}

这是有效的,因为我们将"$@"添加到date的参数列表中,因此可以传递额外的参数,例如只包含日期而不是时间元素的格式字符串。

然后,通过比较$(date +%Y%m%d) - 今天的日期 - 和$(local_date DTSTART +%Y%m%d) - 从文件中解析的日期 - 我们可以确定日期,而不是时间,匹配。

最终输出:

Modified Event
Wed May 18 13:55:00 CDT 2016    Gotham Saison 2 Episode 13 & 14 TMC (France)
Modified Event
Wed May 18 11:55:00 CDT 2016    The Pretender Saison 1 Episode 17 & 18 (VF)     6ter
New Event Created
Wed May 18 13:55:00 CDT 2016    Extant Saison 2 Episode 7 à 9   6ter
New Event Created
Wed May 18 13:15:00 CDT 2016    Une saison au zoo Saison 5 Episode 31 (VF)      France 4 HD
Modified Event
Wed May 18 15:30:00 CDT 2016    Teen Wolf Saison 5 Episode 19   MTV
相关问题