Академический Документы
Профессиональный Документы
Культура Документы
basically tech
97 The many uses of gcal
Friday 19th October, 2007
gcalis a remarkable console-based
program.
It can be used to print calendar information, much like cal, but with much more flexibility.
It can display holiday information for over 300 different countries and states.
It can display astronomical information such as sunrise, sunset, the current phase of the moon and much more.
It can also be used as a diary or personal reminder.
It can print the current date and time. (No, really! :) )
It can be used to display the start and stop of daylight saving.
I'll be providing examples of how to use gcalin all these ways. Bear in mind that this article just scratches the surface with regard to what
gcalcan actually be used for. You are limited by your imagination!
basic options
holidays
astronomical information
resource files
"low priority" resource file entries
"high priority" resource file entries
tidying up the output
fixed date list title
highlight today's date
remove repeating dates
spacing out entries
add holiday entries
more resource file tips
birthdays, anniversaries, etc
print the time
daylight saving
searching
customisation
useful links
basic options
gcalhas many
$ gcal
October 2007
Su Mo Tu We Th Fr Sa
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
Just like cal, nothing special, however you can also display the week number:
$ gcal -K
October 2007
Su Mo Tu We Th Fr Sa CW
1 2 3 4 5 6 39
7 8 9 10 11 12 13 40
14 15 16 17 18 19 20 41
21 22 23 24 25 26 27 42
28 29 30 31
43
Or, instead of the day of the month, how about the day of the year?
$ gcal -j
October 2007
Sun Mon Tue Wed Thu Fri Sat
274 275 276 277 278 279
280 281 282 283 284 285 286
287 288 289 290 291 292 293
294 295 296 297 298 299 300
301 302 303 304
October 2007
Mon
Tue
Wed
Thu
Fri
Sat
1(274) 2(275) 3(276) 4(277) 5(278) 6(279)
http://www.basicallytech.com/blog/archive/97/The-many-uses-of-gcal/
1/9
3/6/2016
$ gcal .
2007
September
Su Mo Tu We Th Fr Sa
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
October
Su Mo Tu We Th Fr Sa
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
November
Su Mo Tu We Th Fr Sa
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
You can also use the options used to show week numbers and day of the year too. (Apply the option before the full stop, e.g. gcal -K .)
When displaying years (equivalent to cal -y), you can choose the layout. The default is four rows (blocks) of three months, but you can
change this with the -bflag, e.g:
$ gcal -b 6 2007
2007
January
Su Mo Tu We Th Fr Sa
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
February
Su Mo Tu We Th Fr Sa
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
...
(output trimmed to save space)
...
September
Su Mo Tu We Th Fr Sa
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
October
Su Mo Tu We Th Fr Sa
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
November
Su Mo Tu We Th Fr Sa
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
December
Su Mo Tu We Th Fr Sa
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
holidays
To display the holidays for England, Wales and the Isle of Man for the whole of the current year, use:
$ gcal -q GB_EN -n
Eternal holiday list:
New Year's Day (GB_EN)
St David's Day (GB_EN)
Mother's Day (GB_EN)
Mary's Annunciation Day (GB_EN)
Good Friday (GB_EN)
Beginning of Financial Year (GB_EN)
Easter Sunday (GB_EN)
Easter Monday (GB_EN)
Swallow Day (GB_EN)
St George's Day (GB_EN)
May Day (GB_EN)
Liberation Day (GB_EN)
Bank Holiday (GB_EN)
The Queen's Birthday (GB_EN)
Father's Day (GB_EN)
Tynwald Day (GB_EN)
Grotto Day (GB_EN)
Bank Holiday (GB_EN)
St Edward's Day (GB_EN)
Halloween (GB_EN)
Guy Fawke's Day (GB_EN)
Christmas Eve (GB_EN)
Christmas Day (GB_EN)
http://www.basicallytech.com/blog/archive/97/The-many-uses-of-gcal/
3/6/2016
Note that you can combine lists, so gcal -q GB_EN --chinese-holidays -nwould output a combined list (in proper date order) of UK
holidays and Chinese festivals.
There are a bewildering amount of calendar options.
astronomical information
gcaleven
offers the ability to check sunrise, sunset, moonrise, moonset, even the current phase of the moon, and more, much, much more.
I'll demonstrate just a couple of these options, otherwise this article will never end. I'll also introduce a vital component of serious gcaluse, a
gcalresource file.
You'll need to know your location in latitude and longitude, plus your height above sea level in meters. You might have to google it. With the
latitude and longitude, I have used degrees with decimal degrees as opposed to degrees, arcminutes, and arcseconds.
The default gcal resource file location is ~/.gcalrc, but this is a little limiting. Instead, create a directory named ~/.gcaland we'll put all our
resource files in there.
After that, create a resource file named ~/.gcal/astronomicaland edit it as follows.
; ~/.gcal/astronomical
;
;my latitude 51.7537N
;my longitude -0.4748E
;my height above sea level 60m (guesstimate)
0 Sunrise is at %25o+51.75-000.47+60
0 Sunset is at %25s+51.75-000.47+60
0 Moon phase %25O
0 %25Z
;
http://www.basicallytech.com/blog/archive/97/The-many-uses-of-gcal/
3/9
3/6/2016
(
(
(
(
(
(
(
(
(
(
(
@@@@@@@
@@@@@@@@@@
@@@@@@@@@@@@
@@@@@@@@@@@@@
@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@
@@@@@@@@@@@@@
@@@@@@@@@@@@@
@@@@@@@@@@
@@@@@@@
(
Moon phase 43%25+
Sunrise is at 07:29
Sunset is at 18:01
Within ~/.gcal/astronomical, the lines beginning with a semicolon are comments, and the lines beginning with a 0are actions for gcal. 0
is shorthand for 00000000which stands for "run this action every day of every month of every year", in the format yyyymmdd. The "action" is
the part of the entry which follows the 0; it may be just a text entry to be printed out, such as "check your e-mails", or it may contain more
complex calulations for gcalto run, such as determining when sunrise is.
These functions may or may not be useful to you, however bear in mind that gcalwas not designed as a high precision astronomical
calculation tool.
resource files
For serious use of gcal, you need to use resource files.
Using a resource file to flag dates which repeat is "mostly easy". :) If you have a look at some of the examples below, you'll soon pick up how
to easily set up both unique and repeat events. However, you should be aware that not all resource file entries are equal!
There are different 'levels' of resource file entry, which can be viewed in more detail in the coding scheme tables from the online
documentation. These different levels allow you some flexibility in prioritising resource file entries, depending on how you use gcal.
What these differing means of making resource file entries allow you to do is to make entries which are either printed all the time, or only on
certain occasions. For example, if you have a weekly event, such as a regular meeting, you may not want to be reminded of it every time you
consult your diary. The way you enter the reminder into your resource file combined with the way you use gcalwill allow you to just be
reminded of your regular meeting on the day it occurs, or maybe also the day before. I know this sounds confusing, so let me provide some
examples to explain this further.
"low priority" resource file entries
For want of a better term, I'm going to call these "low priority" resource file entries. In other words, these diary entries will only appear if gcal
is invoked in a certain way, plus you have specify the resource file(s) you are querying (with the -fflag as we'll see later).
Create a new resource file, which we'll call ~/.gcal/low. (This is as good a place as any to note that I've had issues with gcalresource file
names which contain an underscore. Don't use underscores in your gcalresource file names.)
; ~/.gcal/low
;
; comments begin with a semicolon
;
0 Print this message every day of every month of every year (low)
00000000 Something else which happens every day (low)
;
000000Thu Every Thursday (low)
000010Fri Every Friday in October (low)
200700Thu Every Thursday in 2007 (low)
200710Fri Every Friday in October 2007 (low)
;
The syntax is fairly simple. Dates are entered in a YYYYMMDD format, with a zeroed field representing all available years/months/days.
Therefore 00000000represents every day of every month of every year, with a single 0being shorthand for the same.
"high priority" resource file entries
Now we'll create a seperate resource file for "high priority" entries. These diary entries will appear no matter how you invoke gcal, as long as
you specify the resource file(s) concerned. You can effectively query as many resource files as you want.
; ~/.gcal/high
;
0*d1#999 Every day of every year (high)
0*d1Fri#99Fri.7 %25i0000Oct#0000Nov Every Friday in October and November (high)
0*d1Thu#99Thu.7 %25i0000Oct#0000Nov Every Thursday in October and November (high)
000010thu3 3rd Thursday in Oct (high)
20071019 19th October 2007 (high)
;
The syntax here here is slightly more complex. 0*d1#999literally means "days 1-999 of every year" which is effectively, every day of every
year.
0*d1Fri#99Fri.7literally
means "The first Friday to the 99th Friday of every year" which is effectively, every Friday of every year. I'm not
sure about the reasoning behind the .7(forcing every seven days, maybe?), but it is required (you can try it without :) ).
http://www.basicallytech.com/blog/archive/97/The-many-uses-of-gcal/
4/9
3/6/2016
%25i0000Oct#0000Novqualifies the period acted upon by restricting the start year to any year, and the start month to Oct, and
any year, and the end month to Nov. You can read more about inclusive date periods in the gcalonline documentation.
The -cflag makes gcaldisplay the fixed date messages for today.
And now tomorrow's entries (by way of comparison):
$ gcal -ct -f /home/rob/.gcal/low+/home/rob/.gcal/high -u --date-format="gb"
Fixed date list:
Fri,
Fri,
Fri,
Fri,
Fri,
Fri,
Fri,
The -ctflag makes gcaldisplay the fixed date messages for tomorrow. You can check for today and tomorrow with the flag -cdt.
Now we'll check a week ahead.
$ gcal -cdl@t7 -f /home/rob/.gcal/low+/home/rob/.gcal/high -u --date-format="gb"
Fixed date list:
Thu,
Thu,
Thu,
Fri,
Fri,
Fri,
Sat,
Sun,
Mon,
Tue,
Wed,
Thu,
Thu,
The -cdl@txxflag makes gcaldisplay the fixed date messages for today (-d), and a list of the fixed date messages starting from tomorrow
(l@t), and ending xxdays after today's date. There seems to be a limit of 83 days on lists of this sort.
You'll notice that in list mode (l@), where lists of periods are generated, seven days in this case, only the entries from ~/.gcal/highhave been
printed.
While it's possible to force gcalto print sequential single day entries without using list mode (you'd have to use a forloop), it's not really the
way gcalwas intended to be used, and is slower since you would be running multiple instances of gcal. Plus you're removing the option of
prioritising resource file entries, just for the sake of slightly easier resource file syntax.
To remove Fixed date list:completely, place an xflag after the cflag, something like this:
$ gcal -cxdl@t7 -f /home/rob/.gcal/low+/home/rob/.gcal/high -u --date-format="gb"
5/9
3/6/2016
The parts I have in red, %251and %252determine where the highlighting begins and ends.
We can change the highlighting sequence with the -Hflag. The filed which follows -Hconsists of 4 parts, seperated by colons. These are:
actual_day_start:actual_day_end:holiday_start:holiday_end. I don't feel a particular need to highlight holidays, and I prefer
bold/highlight to reverse video:
-H '^[[1m:^[[0m:^[[0m:^[[0m'
ANSI sequences work fine. Note that ^[represents an ANSI escape sequence. Ctrl-v Escshould create this in bash or vim if you're writing
a script for gcal.
So now if we add the relevant flags to our gcalcommand line, we get something which is becoming increasingly unwieldy:
gcal -cdl@t7 -f /home/rob/.gcal/low+/home/rob/.gcal/high -u --heading-text="%253%25>1*K , %25>02&*D %25U %25Y
%254" --date-format='%251%25<3#K, %25>2&*D %25<3#U %25>04*Y%252' -H '^[[1m:^[[0m:^[[0m:^[[0m'
their purpose now. They aren't particularly intuitive names for resource files. The following
examples will be using two new resource files, ~/.gcal/regular, for regularly repeating events such as birthdays, anniversaries and the like,
and ~/.gcal/diaryfor one-off events such as appointments and other reminders.
In addition, having different levels of resource file entry can be confusing. Thankfully, my life isn't that complex that I can really make use of it,
so for the remaining examples, I'll be using "high" priority examples only. If you're really interested in the different coding schemes for
resource file entries, and which flags applied to gcalwill display them, there is a section in the online gcaldocumentation which explains this
in more detail.
Since you aren't restricted (within reason) as to the number of resource files you can query at any one time, you could create extra resource
files, such as one for work or business-related matters, one (or more) for your getting things done reminders, one for friends, etc. You are
limited by your imagination, and the number of resource files you actually want to maintain.
In the following examples, the command which I'll be running will be:
$ gcal -cdeol@t14 -f /home/rob/.gcal/diary+/home/rob/.gcal/regular -u -g -q GB_EN --heading-text="%251%25>1*K ,
%25>02&*D %25U %25Y %252" --date-format='%251%25<3#K, %25>2&*D %25<3#U %25>04*Y%252' -H
'^[[1m:^[[0m:^[[0m:^[[0m'
http://www.basicallytech.com/blog/archive/97/The-many-uses-of-gcal/
6/9
3/6/2016
(I've found that two spaces are required after the birth year entry. ymmv.)
And I'll get the following output:
Thursday, 18th October 2007
Sun, 21st Oct 2007: Fred's 43rd birthday
Wed, 31st Oct 2007: Halloween (GB_EN)
Which produces:
Thursday, 18th October 2007
Fri, 19th Oct 2007: 11:00 Bicycle to be repaired. Time now is 21:57
Sun, 21st Oct 2007: Fred's 43rd birthday
Wed, 31st Oct 2007: Halloween (GB_EN)
daylight saving
Daylight saving is easy to flag up using gcal:
; ~/.gcal/regular
;
; birthdays
00001021 Fred's %25>1&*B1964 birthday
;
; GMT/BST
; BST starts the last Sunday in March
000003Sun9 British Summer Time starts (clocks go forward)
; GMT starts the last Sunday in October
000010Sun9 British Summer Time ends (clocks go back)
which provides:
Thursday, 18th October 2007
Fri, 19th Oct 2007: 11:00 Bicycle to be repaired. Time now is 22:04
Sun, 21st Oct 2007: Fred's 43rd birthday
Sun, 28th Oct 2007: British Summer Time ends (clocks go back)
Wed, 31st Oct 2007: Halloween (GB_EN)
I understand that the EU has standardised it's daylight saving times, so the above should work throughout the EU. Visit Merlyn - Summer Time
- J R Stockton for advice on calculating daylight saving for North America and other countries.
searching
You can also search your resource files for entries. To do so, use the -I <search_pattern>flag. To make the search case-insensitive, also
use the --ignore-caseflag. For instance, if I wanted to search for "bicycle" (how convenient) in the next 14 days' entries:
$ gcal -cdeol@t14 -f /home/rob/.gcal/diary+/home/rob/.gcal/regular -u -g -q GB_EN --heading-text="%251%25>1*K ,
http://www.basicallytech.com/blog/archive/97/The-many-uses-of-gcal/
7/9
3/6/2016
This search facility will work for holiday entries too, so you could search for "halloween".
customisation
I do like to tinker with my scripts, customising the output (you've got to love sed!), and when I run my personal gcaldiary script using the two
resource files we've created above (~/.gcal/diaryand ~/.gcal/regular), I get the following (I prefer to use a term with a dark
background):
Thursday, 18th October 2007
Tomorrow
Sun 21/10/2007
Sun 28/10/2007
Wed 31/10/2007
Halloween (GB_EN)
This script is provided under GPLv3. Its current incarnation (it changes every now and again) is available, for those who are interested, here.
It's a little messy but you're welcome to have a look/borrow/use.
Currently, the script is invoked from ~/.bashrcthe first time I open a terminal (i.e. opening subsequent terminals do not run the script), and
each time I check my mail.
I hope this has been a helpful introduction to gcal. It's really just touching the surface of the program, there's so much depth to it. If you have
your own tips, or different ways of using gcal, (particularly in a "getting things done" environment) I'd be interested to hear about them.
Useful links
gcal 3.00 documentation
Merlyn - Summer Time - J R Stockton (used to calculate BST/GMT/DST dates)
Home
command line open source tech tips
Sat 5 Mar 2016
Links
Homepage
Blog front page
Recent entries
125 GCHQ has probably spied on you
Wed 30 Sep 2015
124 UK surveillance worse than anything Orwell could have foreseen
Tue 25 Aug 2015
123 Proposed EU Data Protection amendments taken word for word from US Lobbyists
Tue 12 Feb 2013
122 Refuse to be terrorised
Sun 29 Jul 2012
121 Battle for the Internet
Wed 18 Apr 2012
120 PCI Lawsuit
Tue 17 Jan 2012
119 30 online sources for Linux/Unix documentation
http://www.basicallytech.com/blog/archive/97/The-many-uses-of-gcal/
8/9
3/6/2016
http://www.basicallytech.com/blog/archive/97/The-many-uses-of-gcal/
9/9