仅显示使用 ics 文件创建或修改的最后一小时事件
Display only the last hour event created or modified with an ics file
关于之前对查尔斯·达菲的问答,我有一个新问题。如果有创建或修改的新事件,我需要搜索 .ics 文件并每小时在 IRC 上显示。
老问题:
@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[]}
local dt=${content[]}
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)
简单的做法是提取当前日期,提取本地时间的日期,然后进行比较。
local_date() {
local tz=${tzid[]}
local dt=${content[]}
if [[ $dt = *Z ]]; then
tz=UTC
dt=${dt%Z}
fi
shift ## <- remove 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
关于之前对查尔斯·达菲的问答,我有一个新问题。如果有创建或修改的新事件,我需要搜索 .ics 文件并每小时在 IRC 上显示。
老问题:
@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[]}
local dt=${content[]}
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)
简单的做法是提取当前日期,提取本地时间的日期,然后进行比较。
local_date() {
local tz=${tzid[]}
local dt=${content[]}
if [[ $dt = *Z ]]; then
tz=UTC
dt=${dt%Z}
fi
shift ## <- remove 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