I have a new question about this previous question and answer to Charles Duffy. I need to search the .ics file and display every hour on IRC if there is a new event that is created or modified.
The old question: Parsing ICS file with bash
The response of @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
...where handle_event
is a function that does the actual work you care about. For instance, that might look like this:
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
}
With your given input file and the above script, bash parse-ics <test.ics
yields the following output (with my current locale, timezone and language):
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)
The simple thing to do is to extract the current date, extract the date in localtime, and compare.
...and then:
This works because we're adding
"$@"
to the argument list fordate
, so extra arguments such as a format string with only date and not time elements can be passed through.Then, by comparing
$(date +%Y%m%d)
-- today's date -- and$(local_date DTSTART +%Y%m%d)
-- the date parsed from the file -- we can determine if the dates, but not the times, match.Final output: