{"id":8929,"date":"2010-10-19T15:08:23","date_gmt":"2010-10-19T15:08:23","guid":{"rendered":"http:\/\/scienceblogs.com\/gregladen\/2010\/10\/19\/hacking-a-google-calendar-cli\/"},"modified":"2010-10-19T15:08:23","modified_gmt":"2010-10-19T15:08:23","slug":"hacking-a-google-calendar-cli","status":"publish","type":"post","link":"https:\/\/gregladen.com\/blog\/2010\/10\/19\/hacking-a-google-calendar-cli\/","title":{"rendered":"Hacking a Google Calendar CLI Tool"},"content":{"rendered":"<p>I wrote earlier of the very useful command line utility called gcalcli (short for GoogleCALendarCommandLineInterface).  <a href=\"http:\/\/scienceblogs.com\/gregladen\/2010\/10\/using_google_calendar_from_the.php\">Click here to read that post.<\/a>  One of the options is called &#8220;agenda&#8221; which spits out, by default, the next five days of calendar entries.  If you would prefer a different range of time than five days, then you can specify two dates and the utility will give you that set of entries.<\/p>\n<p>But I find that to be a bit of a pain, typing in the dates to start and end the list, when I generally want a quick and dirty &#8220;next several days.&#8221; That, I suppose, is why there is a default of five days.  But five days is not a good default.  If it is Wednesday, I want to see what I&#8217;ve got on my calendar for the rest of the current week, as well as what is on my calendar for ALL of next week.  For me, a better default would be 14 days.  With 14 days, you always get a full look at the present week and the next week, plus, sometimes, a bit after that.<\/p>\n<p>One can imagine writing a bash script that figures out what day it is, and then adds enough days that you get an agenda for the remainder of the present week plus all of the next week, but no more days than that. Such a script could make use of bash&#8217;s ability to manage and manipulate dates as they occur in our periodic system, with 24 hours in a day, 7 days in a week, work days being a subset of five of those days, etc. etc.<\/p>\n<p>One could also imagine writing a bash script to poke one&#8217;s eyes out with hot soldering irons.<\/p>\n<p>A simpler solution is this:<br \/>\n<!--more--><br \/>\nPick a number of days that you like, and add that to the current day, and use those two parameters to invoke the agenda command, and put all that in a script.  The problem with this is still figuring out the second day.  How do you add, say, 14 days to the present day?  If the date utility in bash gives us something like 10\/19\/10, how do you add 14 to that?  19 plus 14 is 33.  There is no day 33.  So you&#8217;d have to increment the month.  So the month would become 11.  One could use a modulo function if available, but some months are 30 days long, some 31 days long, and one is sometimes 28 days long but sometimes 29 days long.  And then, what if you wanted fourteen days starting on December 20th?  Then, the month goes back to 1, and the year is incremented.  Holy crap!<\/p>\n<p>Software like spreadsheets and other financial applications do date math.  It is interesting to note that such applications didn&#8217;t originally do date math, and when they first did it, they did it poorly, and for years (and as far as I know this is ongoing) major screw ups in spreadsheet code often have to do with date math.  Date math, as a matter of fact, is the bane of computers and those who work with them.  (Remember the year 2000 fiasco?)<\/p>\n<p>But there is a way to do this easily, and this is in fact the way that most date math is probably done at some level in spreadsheets and other apps.  Convert everything into the number of seconds since some arbitrary historical moment in time.  This is a bit like how radiocarbon dating works. When you see a date like &#8220;1,000 years before present&#8221; that year is 950 AD, because &#8220;Present&#8221; = 1950.<\/p>\n<p>For this purpose, Linux uses Unix Time, also known as Posix time.  In this system, time started at midnight ZULU time in the morning of January 1st, 1970.  Everything before that is prehistory.  Approximately one and a quarter billion seconds have passed since then.<\/p>\n<p>So the solution is easy:  Convert the present time to Unix time.  Then, add the number of seconds that is 14 days into the future (1209600 seconds) to that number to obtain the second time.  Then, since gcalcli does not read Linux time (shame on the coders! shame on the coders!) convert the Unix time back to esoteric time (with the slashes) and feed that to the gcalcli command.  I originally did this as a one liner but I&#8217;ve since parsed it out into individual lines and put it in a bash script I call &#8220;agenda.&#8221;  Here it is:<\/p>\n<p><code><br \/>\n#\/bin\/bash<\/p>\n<p>nownum=<code>date +%s<\/code><br \/>\nthennum=$((nownum+1209600))<br \/>\nnow=<code>date --date=@$nownum +%D<\/code><br \/>\nthen=<code>date --date=@$thennum +%D<\/code><br \/>\necho Agenda for $now through $then<br \/>\ngcalcli --nc agenda $now $then<\/p>\n<p><\/code><\/p>\n<p>Let&#8217;s break it down.<\/p>\n<p>First, the shebang line, and this may vary across systems:<\/p>\n<p><code><\/p>\n<p>#\/bin\/bash<\/p>\n<p><\/code><\/p>\n<p>Then we make some numbers.  nownum is the current date in Unix format, using the &#8220;date&#8221; command and specifying a &#8220;format&#8221; code of %s, which is seconds since the start of the Unixverse.  Then, 14 days of seconds are added to this to make a second Unix time number:<\/p>\n<p><code><\/p>\n<p>nownum=<code>date +%s<\/code><br \/>\nthennum=$((nownum+1209600))<\/p>\n<p><\/code><\/p>\n<p>Then, these two values, &#8220;nownum&#8221; and &#8220;thennum&#8221; are converted, using the esoteric &#8211;date switch of the date command, into strings &#8220;now&#8221; and &#8220;then&#8221; which are in standard date format:<\/p>\n<p><code><\/p>\n<p>now=<code>date --date=@$nownum +%D<\/code><br \/>\nthen=<code>date --date=@$thennum +%D<\/code><\/p>\n<p><\/code><\/p>\n<p>These two numbers are then printed out as part of a sentence reminding the user what today&#8217;s date is and what the end of the 14 day period is:<\/p>\n<p><code><\/p>\n<p>echo Agenda for $now through $then<\/p>\n<p><\/code><\/p>\n<p>And finally, gcalcli is invoked using $now and $then as parameters to the agenda subcommand.  I also threw in &#8211;nc which suppresses color coding.<\/p>\n<p><code><\/p>\n<p>gcalcli --nc agenda $now $then<\/p>\n<p><\/code><\/p>\n<p>The reason I suppress color coding is so I can do this at the command line<\/p>\n<p>agenda > ~\/agenda.txt<br \/>\nalpine-> (c)ompose -> amanda -> ctrl-r -> ~\/Desktop\/agenda.txt -> ctrl-X -> Y<\/p>\n<p>that slams the current agenda into a text file, opens a mail program, composes a message to Amanda, slams the contents of the agenda file into the email and sends it off.<\/p>\n<p>Hey, wait, what am I doing? I should add a line that creates agenda.txt every time I request the agenda from Google.<\/p>\n<p>tapity-tap-tap tappity-tap<\/p>\n<p>Done.<\/p>\n<p><code><\/p>\n<p>#\/bin\/bash<\/p>\n<p>nownum=<code>date +%s<\/code><br \/>\nthennum=$((nownum+1209600))<br \/>\nnow=<code>date --date=@$nownum +%D<\/code><br \/>\nthen=<code>date --date=@$thennum +%D<\/code><br \/>\necho Agenda for $now through $then<br \/>\ngcalcli --nc agenda $now $then > ~\/Desktop\/agenda.txt<br \/>\ncat ~\/Desktop\/agenda.txt<br \/>\n<\/code><\/p>\n<p>This post is part of the cli series. <a href=\"http:\/\/scienceblogs.com\/gregladen\/command_line_interface_cli\/\">The full list is here.<\/a><\/p>\n<p>If you found this post useful, please consider using the social networking tools below to spread it around!  Thank you<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wrote earlier of the very useful command line utility called gcalcli (short for GoogleCALendarCommandLineInterface). Click here to read that post. One of the options is called &#8220;agenda&#8221; which spits out, by default, the next five days of calendar entries. If you would prefer a different range of time than five days, then you can &hellip; <a href=\"https:\/\/gregladen.com\/blog\/2010\/10\/19\/hacking-a-google-calendar-cli\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Hacking a Google Calendar CLI Tool<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[4278,4585,4586,866,4587,57],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p5fhV1-2k1","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/posts\/8929"}],"collection":[{"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/comments?post=8929"}],"version-history":[{"count":0,"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/posts\/8929\/revisions"}],"wp:attachment":[{"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/media?parent=8929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/categories?post=8929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/tags?post=8929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}