Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

View upcoming events on multiple Unix calendars UNIX
I've been leeching off MacOSXHints for years; this is such a great site. So I think it's about time I share some of my own work. This hint was written because I wasn't entirely happy with the output of previous hints, so I kept working at it until I came up with this solution. I wanted a way to view upcoming entries on the various Unix calendar pages -- both the stock calendars, as well as one of my own. Here's what I eventually came up with:

#!/bin/sh
calendar -f /usr/share/calendar/calendar.computer -l 0 > ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.usholiday -l 0 >> ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.birthday -l 0 >> ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.christian -l 0 >> ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.freebsd -l 0 >> ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.history -l 0 >> ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.music -l 0 >> ~/tmpcal.txt
calendar -f ~/.calendar/calendar -l 14 >> ~/tmpcal.txt

cat ~/tmpcal.txt | sort -M
rm ~/tmpcal.txt

The explanation:

Each line that begins with calendar looks up date information in a different calendar file. The -f switch precedes the full path to a calendar file (most of which are in /usr/share/calendar, but you may also have personal ones elsewhere). The -l switch precedes the number of days to look ahead. When look ahead is set to 0, then only today's date information is returned. The tail end of each calendar line directs the input to a temporary file (the >> on all but the first line amends the output to the file created in the first line). The last calendar line looks in my personal file (in my home directory), which contains personal dates of importance.

The line which begins with cat lists the temporary file, and then directs the output to the sort util. sort then uses the first portion of the line to sort by date (so if your information spans one or more months, it should sort properly -- I've done a limited test on this). The last line removes the temporary file.

[robg adds: I tested this, and it seems to work as described. Remember to make the script executable after saving (via chmod a+x scriptname).]
    •    
  • Currently 2.00 / 5
  You rated: 1 / 5 (4 votes cast)
 
[6,502 views]  

View upcoming events on multiple Unix calendars | 13 comments | Create New Account
Click here to return to the 'View upcoming events on multiple Unix calendars' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
View upcoming events on multiple Unix calendars
Authored by: olivesoft on May 15, '06 09:08:14AM
It's probably better to create a file that contains references to all the calendar files you want to use. You can find a sample of this in: /usr/share/calendar/calendar.all I created a copy of this in my home directory and changed the included files to the full paths of only those in which I was interested:

#ifndef _calendar_all_
#define _calendar_all_

#include "/usr/share/calendar/calendar.world"
#include "/usr/share/calendar/calendar.usholiday"

#endif /* !_calendar_all_ */
Because of this, you only need one "calendar" command that references your ".all" file:

calendar -l 0 -w 0 -f calendar.all

---
-Bob
---------------------
I tend to think of [Mac] OS X as Linux with QA and Taste.
-James Gosling, Java Architect

[ Reply to This | # ]

View upcoming events on multiple Unix calendars
Authored by: n1mie on Jun 07, '06 02:40:49PM

The problem with this method is that it assumes you want to look forward the same amount in every calendar. This is not true (for me). I like to look two weeks ahead in the file that has birthdays. Then I look 7 days ahead in several other personal calendars. In the canned files I only look at today. So your solution, while cool, isn't the one I prefer.

---
--Chip



[ Reply to This | # ]
View upcoming events on multiple Unix calendars
Authored by: ZZamboni on May 15, '06 09:37:01AM
calendar pre-processes its calendar file with cpp, which allows inclusion using C-style directives. Furthermore, it looks by default for ~/.calendar/calendar. So you can simply put this in ~/.calendar/calendar:
#include "calendar.computer"
#include "calendar.usholiday"
#include "calendar.birthday"
#include "calendar.christian"
#include "calendar.freebsd"
#include "calendar.history"
#include "calendar.music"
#include "calendar.personal"

calendar by default looks in /usr/share/calendar and ~/.calendar for files to include, so there is no need to include the full paths (you should create "calendar.personal" in ~/.calendar, even if it's empty, for the above to work).

The only thing missing is the ability to specify different search periods for different calendars...

[ Reply to This | # ]

View upcoming events on multiple Unix calendars
Authored by: ratpH1nk on May 15, '06 09:44:58AM
I like the way you gus ahve done this. I may switch, but in the meantime, this is how I have mine setup. It is in my .profile file:

echo 'Today in history:'
cat /usr/share/calendar/* | grep `date +"%m/%d"`


Gets the job done nicely.

[ Reply to This | # ]
View upcoming events on multiple Unix calendars
Authored by: oink on May 15, '06 10:22:29AM

Nice! I modified it slightly:
at /usr/share/calendar/calendar* | grep `date +"%m/%d"`

To avoid the cat directory error:
cat: /usr/share/calendar/de_DE.ISO8859-1: Is a directory
...

It will definitely go into my .profile or my .login

---
blurred visionary



[ Reply to This | # ]
View upcoming events on multiple Unix calendars
Authored by: kenahoo on May 15, '06 10:39:57AM

That's a nice hint - I do have to point out the UUOC, though; you can simplify the command to this:

grep -h `date +"%m/%d"` /usr/share/calendar/ca*

-Ken



[ Reply to This | # ]
The Ruby version
Authored by: Lectrick on May 15, '06 12:25:43PM
I made this a Ruby script to iterate through the next 7 days of holidays. (Strangely, Memorial Day is not listed??) Save as something like "calendar.rb" and make executable (chmod +x calendar.rb) and run... The reason I commented out the alternate "date" code is because I wanted a solution that didn't depend on any include/requires (even though "date" is part of the standard lib...)

#!/usr/bin/env ruby
#require 'date'
#command = "Date.today.upto(Date.today+7){|d| p d.strftime("%m/%d")}

calendars = [ "christian", "computer", "history", "usholiday" ]

0.upto(7) { |d|
for cal in calendars do
out = `grep -h "#{(Time.now+(3600*24*d)).strftime("%m/%d")}" /usr/share/calendar/calendar.#{cal}`
puts out unless out == ""
end
}

---
In /dev/null, no one can hear you scream

[ Reply to This | # ]

View upcoming events on multiple Unix calendars
Authored by: ZZamboni on May 15, '06 12:05:56PM
While this works for most cases, it fails for the more complex date specifications that calendar understands. For example:
% % grep -v '[0-9][0-9]/[0-9][0-9]' /usr/share/calendar.* | less
calendar.australia:Mar 18       Canberra Day (ACT)
   (textual month specification)
calendar.australia:8/MonFirst   Bank Holiday (ACT, NSW)
   ("first monday of the month")
calendar.christian:Easter-46    Ash Wednesday (First day of Lent)
calendar.christian:Easter-7     Palm Sunday (7 days before Easter)
   (Easter-relative dates)
It also ignores multi-line specifications:
01/28   Space Shuttle Challenger (51-L) explodes 74 seconds after liftoff
        killing Scobee, Smith, McNair, Resnick, Jarvis, Onizuka and McAuliffe,
        1986 


[ Reply to This | # ]
View upcoming events on multiple Unix calendars
Authored by: kitzkikz on May 15, '06 10:50:17AM
There's no need for a temporary file. Use shell's built-in parenthesis (also called a sub-shell). This would prevent two user's running the script at the same time and overwritting each other's results, unnecessary usage of temporary storage, and allow the shell to optimize where possible by allowing it to use a memory buffer instead of forcing it to use disk.

#!/bin/sh
(
calendar -f /usr/share/calendar/calendar.computer -l 0
calendar -f /usr/share/calendar/calendar.usholiday -l 0
calendar -f /usr/share/calendar/calendar.birthday -l 0
calendar -f /usr/share/calendar/calendar.christian -l 0
calendar -f /usr/share/calendar/calendar.freebsd -l 0
calendar -f /usr/share/calendar/calendar.history -l 0
calendar -f /usr/share/calendar/calendar.music -l 0
calendar -f ~/.calendar/calendar -l 14
) | sort -M



[ Reply to This | # ]
View upcoming events on multiple Unix calendars
Authored by: n1mie on Jun 07, '06 02:42:22PM

This is the best solution I've seen. I appreciate the education offered by all the respondents, especially this one.

Thanks, I learned a lot, even while trying to share.

---
--Chip



[ Reply to This | # ]
View upcoming events on multiple Unix calendars
Authored by: tbdavis on May 15, '06 10:52:03AM

If you want to avoid the temporary file — and you use some bash like shell — you can use process substitution. The following example demonstrates this and also makes use of the above hint about including additional calendars


sort -M <( calendar -l 0 -f ~/.calendar/calendar.see_also ) \
        <( calendar -l 14 -f ~/.calendar/calendar )

Process substitution <(command) creates a named pipe and passes the name of that named pipe as the argument where the <(...) construct is.

Note: Including the files with #include directive is much faster than processing them individually (about four times faster on my Mac), and sorting them together. Of course passing ~/tmpcal.txt as an argument to sort is also faster than using cat and piping the result, but the speedup is not that much. And using process substitution is about the same speed as piping the results from piping cat.



[ Reply to This | # ]
View upcoming events on multiple Unix calendars
Authored by: tbdavis on May 15, '06 11:00:04AM

And using the sub-shell — about which previously I was ignorant — is the fastest of all.



[ Reply to This | # ]
The influence of this command's creators is clear:
Authored by: Lectrick on May 15, '06 10:59:08AM

user$ calendar -f calendar.history

May 19 Arwen leaves Lorian to wed King Ellesar (LOTR)

O RLY?!??

---
In /dev/null, no one can hear you scream



[ Reply to This | # ]